diff options
author | Tim <contact@bytim.eu> | 2025-06-14 11:49:28 +0200 |
---|---|---|
committer | Tim <contact@bytim.eu> | 2025-06-14 11:49:28 +0200 |
commit | 8e23d9dade945f87f5fc7fb15042a53a7eeb9a9e (patch) | |
tree | c0f5b6587b6f9f0b591b395c69ad7da08717a30b /src/chef/api/admin | |
parent | 229299146376a2b847f4fe3f331efbd26c0abc70 (diff) | |
download | chef-8e23d9dade945f87f5fc7fb15042a53a7eeb9a9e.tar.xz chef-8e23d9dade945f87f5fc7fb15042a53a7eeb9a9e.zip |
Refactor project structure
Diffstat (limited to 'src/chef/api/admin')
-rw-r--r-- | src/chef/api/admin/category.clj | 35 | ||||
-rw-r--r-- | src/chef/api/admin/recipe.clj | 45 |
2 files changed, 80 insertions, 0 deletions
diff --git a/src/chef/api/admin/category.clj b/src/chef/api/admin/category.clj new file mode 100644 index 0000000..2d11510 --- /dev/null +++ b/src/chef/api/admin/category.clj @@ -0,0 +1,35 @@ +(ns chef.api.admin.category + (:require [chef.utils :as cutils] + [chef.logic.categories :as clcategories] + [ring.util.response :as ruresp])) + +;; POST / +(defn handle-edit [req] + (cutils/auth-only req + (if-let [id (cutils/s->int-or-nil (get-in req [:path-params :id]))] + (do (clcategories/update-category! id (merge {} + (when-let [name (get-in req [:params "name"])] + {:name name}) + (when-let [question (get-in req [:params "question"])] + {:question question}))) + (ruresp/response "Updated.")) + (ruresp/bad-request "Bad request.")))) + +;; DELETE / +(defn handle-delete [req] + (cutils/auth-only req + (let [id (cutils/s->int-or-nil (get-in req [:path-params :id]))] + (if (and (some? id) + (not= id -1)) + (do (clcategories/delete-category-and-children! id) + (-> (ruresp/response "Deleted.") + (ruresp/header "HX-Refresh" "true"))) + (ruresp/bad-request "Bad request."))))) + +;; POST /create +(defn handle-create [req] + (cutils/auth-only req + (clcategories/create-category! (or (get-in req [:params "parent"]) + -1)) + (-> (ruresp/created "Created.") + (ruresp/header "HX-Refresh" "true")))) 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.")))) |