summaryrefslogtreecommitdiff
path: root/src/chef/api/admin
diff options
context:
space:
mode:
Diffstat (limited to 'src/chef/api/admin')
-rw-r--r--src/chef/api/admin/category.clj35
-rw-r--r--src/chef/api/admin/recipe.clj45
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."))))