summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/chef/pages/admin.clj21
-rw-r--r--src/chef/pages/admin/api.clj13
-rw-r--r--src/chef/pages/home.clj12
-rw-r--r--src/chef/routes.clj12
-rw-r--r--src/chef/utils.clj3
5 files changed, 41 insertions, 20 deletions
diff --git a/src/chef/pages/admin.clj b/src/chef/pages/admin.clj
index 9aaade3..fb6eeee 100644
--- a/src/chef/pages/admin.clj
+++ b/src/chef/pages/admin.clj
@@ -17,15 +17,20 @@
[:input {:type :text :placeholder "Frage"
:style {:display :inline-block
:width :auto}}] ; TODO: only show when category has children
- [:img {:src "/static/icons/plus.svg" :height "30em"
- :style {:vertical-align :middle
- :margin-left "1em"}}]]]
+ [:img {:src "/static/icons/plus.svg" :height "30em"
+ :style {:vertical-align :middle
+ :margin-left "1em"}
+ :hx-post "/api/admin/create-category"
+ :hx-swap "none"}]]]
[:h2 "Rezepte"]
[:i "Coming soon..."]))
(defn handler [req]
- (cutils/auth-only req
- (-> (render)
- html/html
- str
- ruresp/response)))
+ (let [access-token (get-in req [:oauth2/access-tokens :auth])
+ resp (-> (render)
+ html/html
+ str
+ ruresp/response)]
+ (if (some? access-token)
+ (assoc resp :session (assoc (:session req) :oauth-token access-token))
+ (cutils/auth-only req resp))))
diff --git a/src/chef/pages/admin/api.clj b/src/chef/pages/admin/api.clj
new file mode 100644
index 0000000..3e2ccbd
--- /dev/null
+++ b/src/chef/pages/admin/api.clj
@@ -0,0 +1,13 @@
+(ns chef.pages.admin.api
+ (:require [chef.utils :as cutils]
+ [chef.database :as cdb]
+ [next.jdbc :as jdbc]
+ [honey.sql :as sql]
+ [ring.util.response :as ruresp]))
+
+(defn create-category [req]
+ (cutils/auth-only req
+ (jdbc/execute! @cdb/db (sql/format {:insert-into [:categories]
+ :values [{:name "New category"}]}))
+ (-> (ruresp/created "Created.")
+ (ruresp/header "Refresh" "0"))))
diff --git a/src/chef/pages/home.clj b/src/chef/pages/home.clj
index 2060668..748c60c 100644
--- a/src/chef/pages/home.clj
+++ b/src/chef/pages/home.clj
@@ -27,11 +27,7 @@
[:div {:id "search-results"}]]))
(defn handler [req]
- (let [access-token (get-in req [:oauth2/access-tokens :auth])
- resp (-> (render)
- html/html
- str
- ruresp/response)]
- (if (some? access-token)
- (assoc resp :session (assoc (:session req) :oauth-token access-token))
- resp)))
+ (-> (render)
+ html/html
+ str
+ ruresp/response))
diff --git a/src/chef/routes.clj b/src/chef/routes.clj
index 27d6dfd..8ade8d1 100644
--- a/src/chef/routes.clj
+++ b/src/chef/routes.clj
@@ -9,14 +9,20 @@
[chef.pages.home :as cphome]
[chef.pages.admin :as cpadmin]
- [chef.components.search :as ccsearch]))
+ [chef.components.search :as ccsearch]
+
+ [chef.pages.admin.api :as cpaapi]))
(def router [["/" {:get {:handler cphome/handler}}]
["/admin/" {:get {:handler cpadmin/handler}}]
["/static/*" (rring/create-resource-handler)]
["/components"
- ["/search" {:get {:handler ccsearch/handler}}]]])
+ ["/search" {:get {:handler ccsearch/handler}}]]
+
+ ["/api"
+ ["/admin"
+ ["/create-category" {:post {:handler cpaapi/create-category}}]]]])
(def ring-handler (delay (-> router
rring/router
@@ -28,7 +34,7 @@
:scopes (cstr/split (env/env "OAUTH_SCOPES") #",")
:launch-uri "/auth"
:redirect-uri "/auth/callback"
- :landing-uri "/"
+ :landing-uri "/admin"
:pkce? true}})
rmparams/wrap-params
rmsession/wrap-session)))
diff --git a/src/chef/utils.clj b/src/chef/utils.clj
index 3170be9..f909c52 100644
--- a/src/chef/utils.clj
+++ b/src/chef/utils.clj
@@ -14,4 +14,5 @@
`(if (some? (get-in ~request [:session :oauth-token]))
(do ~@body)
~(-> (ruresp/response "Unauthorized.")
- (ruresp/status 401))))
+ (ruresp/status 302)
+ (ruresp/header "Location" "/auth"))))