Add the search for articles on the index page.

This commit is contained in:
2024-04-11 21:18:44 +03:00
parent e464e9d70f
commit 2f82f419d7
6 changed files with 76 additions and 6 deletions
+18
View File
@@ -46,6 +46,24 @@
(catch js/Error err (js/console.log (ex-cause err)))))
channel)
(defn search-articles-briefly
"Search all articles by titles and contents and returns via channel. It gets and returns the same channel. The content will be brief (<=60 characters)."
[client search channel]
(go
(try
(let [res (<p! (.query client
(str "select id, title, "
"substr(content, 0, 60) content, "
"length(content) content_length, "
"created from articles "
"where title like $1 "
"or content like $1 "
"order by created desc")
(clj->js [(str "%" search "%")])))]
(>! channel (.-rows res)))
(catch js/Error err (js/console.log (ex-cause err)))))
channel)
(defn get-article
"Reads an article with the id and returns via channel. It gets and returns the same channel. Empty collection if not found."
[client id channel]
+17
View File
@@ -112,6 +112,23 @@
(res/status 500)
(res/send (ex-message err))))))))
(defn htmx-search-articles-handler-factory
[client]
(fn [req res]
(let [channel (chan)
search (.-search (.-body req))]
(go
(try
(let [articles (<! (db/search-articles-briefly client search channel))]
(res/status 200)
(.render res "articles_briefly"
(-> {:articles articles
:layout false}
(cljs.core/clj->js))))
(catch js/Error err
(res/status 500)
(res/send (ex-message err))))))))
(defn htmx-create-article-handler-factory
[client]
(fn [req res]
+3
View File
@@ -70,6 +70,9 @@
(. app get "/admin"
(handlers/admin-panel-handler-factory client))
(. app post "/htmx/search/articles/"
(handlers/htmx-search-articles-handler-factory client))
(. app post "/htmx/articles/"
(handlers/htmx-create-article-handler-factory client))