summaryrefslogtreecommitdiff
path: root/src/chef/api/admin/recipe.clj
diff options
context:
space:
mode:
authorTim <contact@bytim.eu>2025-06-14 11:49:28 +0200
committerTim <contact@bytim.eu>2025-06-14 11:49:28 +0200
commit8e23d9dade945f87f5fc7fb15042a53a7eeb9a9e (patch)
treec0f5b6587b6f9f0b591b395c69ad7da08717a30b /src/chef/api/admin/recipe.clj
parent229299146376a2b847f4fe3f331efbd26c0abc70 (diff)
downloadchef-8e23d9dade945f87f5fc7fb15042a53a7eeb9a9e.tar.xz
chef-8e23d9dade945f87f5fc7fb15042a53a7eeb9a9e.zip
Refactor project structure
Diffstat (limited to 'src/chef/api/admin/recipe.clj')
-rw-r--r--src/chef/api/admin/recipe.clj45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/chef/api/admin/recipe.clj b/src/chef/api/admin/recipe.clj
new file mode 100644
index 0000000..c69ade5
--- /dev/null
+++ b/src/chef/api/admin/recipe.clj
@@ -0,0 +1,45 @@
+(ns chef.api.admin.recipe
+ (:require [chef.utils :as cutils]
+ [chef.logic.recipes :as clrecipes]
+ [ring.util.response :as ruresp]))
+
+;; POST /
+(defn handle-edit [req]
+ (cutils/auth-only req
+ (let [id (cutils/s->int-or-nil (get-in req [:path-params :id]))
+ ingredients (get-in req [:params "ingredients"])]
+ (if (and (some? id)
+ (cutils/valid-ingredients? ingredients))
+ (do (when-let [thumbnail (get-in req [:params "thumbnail"])]
+ (clrecipes/set-recipe-thumbnail! id thumbnail))
+ (clrecipes/update-recipe! id {:title (get-in req [:params "title"])
+ :category (get-in req [:params "category"])
+ :unit (get-in req [:params "ingredients-unit"])
+ :ingredients ingredients
+ :preparation (get-in req [:params "preparation"])})
+ (ruresp/response "Saved."))
+ (ruresp/bad-request "Bad request.")))))
+
+;; DELETE /
+(defn handle-delete [req]
+ (cutils/auth-only req
+ (if-let [id (cutils/s->int-or-nil (get-in req [:path-params :id]))]
+ (do (clrecipes/delete-recipe! id)
+ (-> (ruresp/response "Deleted.")
+ (ruresp/header "HX-Refresh" "true")))
+ (ruresp/bad-request "Bad request."))))
+
+;; POST /create
+(defn handle-create [req]
+ (cutils/auth-only req
+ (clrecipes/create-recipe!)
+ (-> (ruresp/created "Created.")
+ (ruresp/header "HX-Refresh" "true"))))
+
+;; DELETE /thumbnail
+(defn handle-delete-thumbnail [req]
+ (cutils/auth-only req
+ (if-let [id (cutils/s->int-or-nil (get-in req [:path-params :id]))]
+ (do (clrecipes/remove-recipe-thumbnail! id)
+ (ruresp/response "Done."))
+ (ruresp/bad-request "Bad request."))))