summaryrefslogtreecommitdiff
path: root/src/chef/pages/admin/api.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/chef/pages/admin/api.clj')
-rw-r--r--src/chef/pages/admin/api.clj126
1 files changed, 0 insertions, 126 deletions
diff --git a/src/chef/pages/admin/api.clj b/src/chef/pages/admin/api.clj
deleted file mode 100644
index 1119607..0000000
--- a/src/chef/pages/admin/api.clj
+++ /dev/null
@@ -1,126 +0,0 @@
-(ns chef.pages.admin.api
- (:require [chef.utils :as cutils]
- [chef.database :as cdb]
- [clojure.string :as cstr]
- [next.jdbc :as jdbc]
- [honey.sql :as sql]
- [ring.util.response :as ruresp]
- [clojure.java.io :as cjio])
- (:import java.io.File))
-
-(defn create-category [req]
- (cutils/auth-only req
- (jdbc/execute! @cdb/db
- (sql/format {:insert-into [:categories]
- :values [(merge {:name "New category"
- :parent (or (get-in req [:params "parent"])
- -1)})]}))
- (-> (ruresp/created "Created.")
- (ruresp/header "HX-Refresh" "true"))))
-
-(defn- delete-category-children! [id]
- (let [children (->> (sql/format {:select [:*]
- :from [:categories]
- :where [:= :parent id]})
- (jdbc/execute! @cdb/db)
- (map #(:categories/id %)))]
- (doseq [child children]
- (jdbc/execute! @cdb/db
- (sql/format {:delete-from [:categories]
- :where [:= :id child]}))
- (delete-category-children! child))))
-
-(defn delete-category [req]
- (cutils/auth-only req
- (if-let [id (try (Integer/parseInt (get-in req [:path-params :id]))
- (catch Exception _ nil))]
- (when (not= id -1)
- (do (jdbc/execute! @cdb/db
- (sql/format {:delete-from [:categories]
- :where [:= :id id]}))
- (delete-category-children! id)
- (-> (ruresp/response "Deleted.")
- (ruresp/header "HX-Refresh" "true"))))
- (ruresp/bad-request "Bad request."))))
-
-(defn edit-category [req]
- (cutils/auth-only req
- (if-let [id (try (Integer/parseInt (get-in req [:path-params :id]))
- (catch Exception _ nil))]
- (do (when-let [name (get-in req [:params "name"])]
- (jdbc/execute! @cdb/db (sql/format {:update :categories
- :set {:name name}
- :where [:= :id id]})))
- (when-let [question (get-in req [:params "question"])]
- (jdbc/execute! @cdb/db (sql/format {:update :categories
- :set {:question question}
- :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."))))
-
-(defn edit-recipe [req]
- (cutils/auth-only req
- (let [id (try (Integer/parseInt (get-in req [:path-params :id]))
- (catch Exception _ nil))
- ingredients (get-in req [:params "ingredients"])]
- (if (and (some? id)
- (cutils/valid-ingredients? ingredients))
- (do (when-let [thumbnail (get-in req [:params "thumbnail"])]
- (when-let [existing-thumbnail-file (->> {:select [:*]
- :from [:recipes]
- :where [:= :id id]}
- sql/format
- (jdbc/execute! @cdb/db)
- first
- cutils/get-thumbnail-file)]
- (.delete ^File existing-thumbnail-file))
- (cjio/copy (:tempfile thumbnail)
- (File. (str "./thumbnails/" id "."
- (-> thumbnail
- :filename
- (cstr/split #"\.")
- last)))))
- (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 ingredients
- :preparation (get-in req [:params "preparation"])}
- :where [:= :id id]}))
- (ruresp/response "Saved."))
- (ruresp/bad-request "Bad request.")))))
-
-(defn delete-thumbnail [req]
- (cutils/auth-only req
- (if-let [id (try (Integer/parseInt (get-in req [:path-params :id]))
- (catch Exception _ nil))]
- (when-let [thumbnail-file (->> {:select [:*]
- :from [:recipes]
- :where [:= :id id]}
- sql/format
- (jdbc/execute! @cdb/db)
- first
- cutils/get-thumbnail-file)]
- (.delete ^File thumbnail-file)
- (ruresp/response "Done."))
- (ruresp/bad-request "Bad request."))))