Add the create modal, a route, a handler to create a article.
This commit is contained in:
@@ -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))))))))
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user