Add functions to get an article or articles with the brief content.

main
KKlochko 1 year ago
parent d8d8340aa2
commit 72b9e604ca

@ -26,7 +26,21 @@
[client channel] [client channel]
(go (go
(try (try
(let [res (<p! (. client query "select * from articles"))] (let [res (<p! (. client query "select id, title, content, created from articles"))]
(>! channel (.-rows res)))
(catch js/Error err (js/console.log (ex-cause err)))))
channel)
(defn get-articles-briefly
"Reads all articles and returns via channel. It gets and returns the same channel. The content will be brief (<=60 characters)."
[client channel]
(go
(try
(let [res (<p! (. client query
(str "select id, title,"
"substr(content, 0, 60) content,"
"length(content) content_length,"
"created from articles")))]
(>! channel (.-rows res))) (>! channel (.-rows res)))
(catch js/Error err (js/console.log (ex-cause err))))) (catch js/Error err (js/console.log (ex-cause err)))))
channel) channel)
@ -37,7 +51,25 @@
(go (go
(try (try
(let [res (<p! (.query client (let [res (<p! (.query client
"select * from articles where id=$1" "select id, title, content, created from articles where id=$1"
(clj->js [id])))]
(>! channel (-> (.-rows res)
(js->clj :keywordize-keys true)
(first))))
(catch js/Error err (js/console.log (ex-cause err)))))
channel)
(defn get-article-briefly
"Reads an article with the id and returns via channel. It gets and returns the same channel. Empty collection if not found. The content will be brief (<=60 characters)."
[client id 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 id=$1")
(clj->js [id])))] (clj->js [id])))]
(>! channel (-> (.-rows res) (>! channel (-> (.-rows res)
(js->clj :keywordize-keys true) (js->clj :keywordize-keys true)

Loading…
Cancel
Save