summaryrefslogtreecommitdiff
path: root/src/chef/pages/admin/api.clj
diff options
context:
space:
mode:
authorTim <contact@bytim.eu>2025-05-30 17:09:57 +0200
committerTim <contact@bytim.eu>2025-05-30 17:09:57 +0200
commitea7b0078478ba7925f2db3cb1fa038e8a3d85ab8 (patch)
tree41b18b1bbd073521891524ee8d32ecc381874a11 /src/chef/pages/admin/api.clj
parentd1699f9af8cb7457f248bf71197535221f3d9472 (diff)
downloadchef-ea7b0078478ba7925f2db3cb1fa038e8a3d85ab8.tar.xz
chef-ea7b0078478ba7925f2db3cb1fa038e8a3d85ab8.zip
Add features to manage recipes in admin interface
Diffstat (limited to 'src/chef/pages/admin/api.clj')
-rw-r--r--src/chef/pages/admin/api.clj36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/chef/pages/admin/api.clj b/src/chef/pages/admin/api.clj
index ff11672..8b2723a 100644
--- a/src/chef/pages/admin/api.clj
+++ b/src/chef/pages/admin/api.clj
@@ -37,7 +37,6 @@
:where [:= :id id]}))
(delete-category-children! id)
(-> (ruresp/response "Deleted.")
- (ruresp/status 200)
(ruresp/header "HX-Refresh" "true"))))
(ruresp/bad-request "Bad request."))))
@@ -55,3 +54,38 @@
:where [:= :id id]})))
(ruresp/response "Updated."))
(ruresp/bad-request "Bad request."))))
+
+(defn create-recipe [req]
+ (cutils/auth-only req
+ (jdbc/execute! @cdb/db
+ (sql/format {:insert-into [:recipes]
+ :values [{:title "New recipe"}]}))
+ (-> (ruresp/created "Created.")
+ (ruresp/header "HX-Refresh" "true"))))
+
+(defn delete-recipe [req]
+ (cutils/auth-only req
+ (if-let [id (try (Integer/parseInt (get-in req [:path-params :id]))
+ (catch Exception _ nil))]
+ (do (jdbc/execute! @cdb/db
+ (sql/format {:delete-from [:recipes]
+ :where [:= :id id]}))
+ (-> (ruresp/response "Deleted.")
+ (ruresp/header "HX-Refresh" "true")))
+ (ruresp/bad-request "Bad request."))))
+
+;;TODO: validate request
+(defn edit-recipe [req]
+ (cutils/auth-only req
+ (if-let [id (try (Integer/parseInt (get-in req [:path-params :id]))
+ (catch Exception _ nil))]
+ (do (jdbc/execute! @cdb/db
+ (sql/format {:update :recipes
+ :set {:title (get-in req [:params "title"])
+ :category (get-in req [:params "category"])
+ :unit (get-in req [:params "ingredients-unit"])
+ :ingredients (get-in req [:params "ingredients"])
+ :preparation (get-in req [:params "preparation"])}
+ :where [:= :id id]}))
+ (ruresp/response "Saved."))
+ (ruresp/bad-request "Bad request."))))