summaryrefslogtreecommitdiff
path: root/bin/serve.fnl
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-09-27 17:05:55 +0600
committerunwox <me@unwox.com>2024-09-27 17:05:55 +0600
commit3214bfe479f1f77d0554d3e8fd56bc5d75180d57 (patch)
treeeff81f0a345e5bc20983e5b2aebfda58cb2eb7c0 /bin/serve.fnl
parent2b8f8c31932f95f3460a4e8794a47bedfb6514c7 (diff)
display random products on main page
Diffstat (limited to 'bin/serve.fnl')
-rw-r--r--bin/serve.fnl54
1 files changed, 38 insertions, 16 deletions
diff --git a/bin/serve.fnl b/bin/serve.fnl
index 0368bed..67c3d7d 100644
--- a/bin/serve.fnl
+++ b/bin/serve.fnl
@@ -26,7 +26,7 @@
"чабань" "доска"})
(fn unescape [s]
- (assert (= (type s) :string))
+ (assert (= (type s) :string) "s must be string")
(pick-values 1
(-> s
(string.gsub "&lt;" "<")
@@ -89,7 +89,7 @@
""))
(fn base-template [query sort page total ...]
- (local paginator (paginator-template query page 32 total))
+ (local paginator (paginator-template query page 48 total))
[:html {:lang "en"}
[:head {}
@@ -99,18 +99,37 @@
[:body {}
[:div {:class "container"}
[:div {:class "content"}
- [:aside {:class "aside"}
- [:div {:class "aside-content"}
- [:a {:href "/" :style "display: block;"}
- [:img {:class "logo" :src "static/logo.svg" :alt "Логотип meicha.ru"}]]
- [:form {:class "form"}
- [:input {:type :search :name :query :value query
+ [:aside {:class "aside"}
+ [:div {:class "aside-content"}
+ [:a {:href "/" :style "display: block;"}
+ [:img {:class "logo" :src "static/logo.svg" :alt "Логотип meicha.ru"}]]
+ [:form {:class "form"}
+ [:input {:type :search :name :query :value query
:autofocus true :placeholder "enter search query"}]
- [:button {:type :submit} "Искать"]]
- paginator]]
- [:section {}
- [:div {:class "list"} ...]
- [:footer {} paginator]]]]]])
+ [:button {:type :submit} "Искать"]]
+ paginator]]
+ [:section {}
+ [:div {:class "list"} ...]
+ [:footer {} paginator]]]]]])
+
+(fn random-products [limit]
+ (assert (< 0 limit) "limit must be > 0")
+ (luna.db.query*
+ db
+ "SELECT id,
+ site,
+ title,
+ description,
+ image,
+ url,
+ price,
+ weight,
+ price_per AS \"price-per\",
+ year
+ FROM products
+ ORDER BY RANDOM()
+ LIMIT ?"
+ [limit]))
(fn query-products [page query sorters]
(local query
@@ -148,8 +167,8 @@
WHERE search.`table` = 'products'
AND search.name MATCH ?
ORDER BY rank
- LIMIT 32 OFFSET ?"
- [query (* (- page 1) 32)])
+ LIMIT 48 OFFSET ?"
+ [query (* (- page 1) 48)])
:total (if (< 0 (# total))
(. total 1 1)
0)})
@@ -160,7 +179,10 @@
page (or (tonumber query.page) 1)
search (or query.query "")
sort "ASC"
- {: results : total} (query-products page search sort)]
+ {: results : total}
+ (if (~= "" search)
+ (query-products page search sort)
+ {:total 48 :results (random-products 48)})]
(values
200 headers
(html.render