@ -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,7 @@
(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])))]
(clj->js [id])))]
(>! channel (-> (.-rows res)
(>! channel (-> (.-rows res)
(js->clj :keywordize-keys true)
(js->clj :keywordize-keys true)
@ -45,6 +59,24 @@
(catch js/Error err (js/console.log (ex-cause err)))))
(catch js/Error err (js/console.log (ex-cause err)))))
channel)
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])))]
(>! channel (-> (.-rows res)
(js->clj :keywordize-keys true)
(first))))
(catch js/Error err (js/console.log (ex-cause err)))))
channel)
(defn insert-article
(defn insert-article
"Insert an article and return the id via channel. It gets and returns the same channel."
"Insert an article and return the id via channel. It gets and returns the same channel."
[client title content channel]
[client title content channel]