From 8e23d9dade945f87f5fc7fb15042a53a7eeb9a9e Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 14 Jun 2025 11:49:28 +0200 Subject: Refactor project structure --- src/chef/api/admin/recipe.clj | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/chef/api/admin/recipe.clj (limited to 'src/chef/api/admin/recipe.clj') 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.")))) -- cgit v1.2.3