You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.6 KiB

(ns blog.handlers
(:require [cljs.core.async :refer [go, take!, put!, <!, >!, chan]]
[cljs.core.async.interop :refer-macros [<p!]]
[blog.db :as db]))
(defn index-page-handler-factory
[client]
(fn [req res]
(let [channel (chan)
id (.-id (.-params req))]
(go
(try
(let [articles (<! (db/get-articles-briefly client channel))]
(res/status 200)
(.render res "index"
(-> {:articles articles}
(cljs.core/clj->js))))
(catch js/Error err
(res/status 500)
(res/send (ex-message err))))))))
(defn admin-panel-handler-factory
[client]
(fn [req res]
(let [channel (chan)
id (.-id (.-params req))]
(go
(try
(let [articles (<! (db/get-articles-briefly client channel))]
(res/status 200)
(.render res "admin_panel"
(-> {:articles articles}
(cljs.core/clj->js))))
(catch js/Error err
(res/status 500)
(res/send (ex-message err))))))))
(defn htmx-get-article-handler-factory
[client]
(fn [req res]
(let [channel (chan)
id (.-id (.-params req))]
(go
(try
(let [article (<! (db/get-article client id channel))]
(if (empty? article)
(do
(res/status 404)
(res/send (ex-message "404")))
(do
(res/status 200)
(.render res "article_full"
(-> {:article article :layout false}
(cljs.core/clj->js))))))
(catch js/Error err
(res/status 500)
(res/send (ex-message err))))))))
(defn htmx-get-article-row-handler-factory
[client]
(fn [req res]
(let [channel (chan)
id (.-id (.-params req))]
(go
(try
(let [article (<! (db/get-article client id channel))]
(if (empty? article)
(do
(res/status 404)
(res/send (ex-message "404")))
(do
(res/status 200)
(.render res "article_row"
(-> {:article article :layout false }
(cljs.core/clj->js))))))
(catch js/Error err
(res/status 500)
(res/send (ex-message err))))))))
(defn htmx-get-article-preview-content-handler-factory
[client]
(fn [req res]
(let [channel (chan)
id (.-id (.-params req))]
(go
(try
(let [article (<! (db/get-article client id channel))]
(if (empty? article)
(do
(res/status 404)
(res/send (ex-message "404")))
(do
(res/status 200)
(.render res "admin_article_preview_content"
(-> {:article article :layout false }
(cljs.core/clj->js))))))
(catch js/Error err
(res/status 500)
(res/send (ex-message err))))))))
(defn htmx-delete-article-row-handler-factory
[client]
(fn [req res]
(let [channel (chan)
id (.-id (.-params req))]
(go
(try
(let [id (<! (db/delete-article client id channel))]
(res/status 200)
(.send res ""))
(catch js/Error err
(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]
(let [channel (chan)
title (.-title (.-body req))
content (.-content (.-body req))]
(go
(try
(let [id (<! (db/insert-article client title content channel))]
(if (nil? id)
(do
(res/status 404)
(res/send (ex-message "404")))
(do
(res/status 200)
(.redirect res (str "/htmx/admin/rows/article/" id)))))
(catch js/Error err
(res/status 500)
(res/send (ex-message err))))))))