Add the create modal, a route, a handler to create a article.

This commit is contained in:
2024-04-10 12:46:09 +03:00
parent 657d58d15f
commit 9177e42fb0
3 changed files with 73 additions and 3 deletions
+20
View File
@@ -98,3 +98,23 @@
(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))))))))
+10
View File
@@ -1,6 +1,7 @@
(ns blog.server
(:require ["express" :as express]
["express-handlebars" :refer [engine]]
["body-parser" :as body-parser]
[blog.db :as db]
[blog.env :as env]
[blog.handlers :as handlers]))
@@ -28,6 +29,12 @@
(defn set-routes
"Sets the routes for server."
[]
;; need to parse request body for forms.
(. app use (.urlencoded body-parser
(-> {:extended false}
(clj->js))))
(. app get "/"
(handlers/index-page-handler-factory client))
(fn [req res]
@@ -35,6 +42,9 @@
(. app get "/admin"
(handlers/admin-panel-handler-factory client))
(. app post "/htmx/articles/"
(handlers/htmx-create-article-handler-factory client))
(. app get "/htmx/articles/:id"
(handlers/htmx-get-article-handler-factory client))