diff --git a/src/main/blog/db.cljs b/src/main/blog/db.cljs index c58b0bb..2f34baa 100644 --- a/src/main/blog/db.cljs +++ b/src/main/blog/db.cljs @@ -1,6 +1,6 @@ (ns blog.db (:require ["pg" :refer [Client]] - [cljs.core.async :refer [go, take!, put!, >!, chan]] + [cljs.core.async :refer [go, take!, put!, !, chan]] [cljs.core.async.interop :refer-macros [
js js->clj]] [blog.env :as env])) @@ -17,10 +17,6 @@ [client] (. client connect)) -(defn create-tables - [client] - (. client query "CREATE TABLE IF NOT EXISTS articles (id SERIAL PRIMARY KEY, title VARCHAR(255) NOT NULL, content TEXT NOT NULL, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP);")) - (defn get-articles "Reads all articles and returns via channel. It gets and returns the same channel." [client channel] @@ -142,3 +138,69 @@ (catch js/Error err (js/console.log (ex-cause err))))) channel) +(defn get-about + "Reads the about article. It gets and returns the same channel. Empty collection if not found." + [client channel] + (go + (try + (let [res (
(.-rows res) + (js->clj :keywordize-keys true) + (first))] + (>! channel (or about []))) + (catch js/Error err + (do + (>! channel nil) + (js/console.log (ex-cause err)))))) + channel) + +(defn insert-about + "Insert the about article and return the id via channel. It gets and returns the same channel." + [client channel] + (go + (try + (let [res (! channel (-> (.-rows res) + (js->clj :keywordize-keys true) + (first) + (:id)))) + (catch js/Error err (js/console.log (ex-cause err))))) + channel) + +(defn insert-about-if-not-exist + "Insert the about article if not exist and return the id via channel. It gets and returns the same channel." + [client] + (go + (let [channel (chan)] + (try + (let [about (js [content])))] + (>! channel (-> 1))) + (catch js/Error err (js/console.log (ex-cause err))))) + channel) + +(defn create-tables + [client] + (. client query "CREATE TABLE IF NOT EXISTS articles (id SERIAL PRIMARY KEY, title VARCHAR(255) NOT NULL, content TEXT NOT NULL, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP);") + (. client query "CREATE TABLE IF NOT EXISTS about (id SERIAL PRIMARY KEY, content TEXT NOT NULL, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP);") + (insert-about-if-not-exist client)) +