diff options
Diffstat (limited to 'src/chef/pages/admin')
-rw-r--r-- | src/chef/pages/admin/api.clj | 91 | ||||
-rw-r--r-- | src/chef/pages/admin/recipe_editor.clj | 61 |
2 files changed, 0 insertions, 152 deletions
diff --git a/src/chef/pages/admin/api.clj b/src/chef/pages/admin/api.clj deleted file mode 100644 index f1b7226..0000000 --- a/src/chef/pages/admin/api.clj +++ /dev/null @@ -1,91 +0,0 @@ -(ns chef.pages.admin.api - (:require [chef.utils :as cutils] - [chef.database :as cdb] - [next.jdbc :as jdbc] - [honey.sql :as sql] - [ring.util.response :as ruresp])) - -(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.")))) - -;;TODO: validate request -(defn edit-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 {:update :recipes - :set {:title (get-in req [:params "title"]) - :category (get-in req [:params "category"]) - :unit (get-in req [:params "ingredients-unit"]) - :ingredients (get-in req [:params "ingredients"]) - :preparation (get-in req [:params "preparation"])} - :where [:= :id id]})) - (ruresp/response "Saved.")) - (ruresp/bad-request "Bad request.")))) diff --git a/src/chef/pages/admin/recipe_editor.clj b/src/chef/pages/admin/recipe_editor.clj deleted file mode 100644 index 6afd591..0000000 --- a/src/chef/pages/admin/recipe_editor.clj +++ /dev/null @@ -1,61 +0,0 @@ -(ns chef.pages.admin.recipe-editor - (:require [hiccup2.core :as html] - [hiccup.util :as hutil] - [honey.sql :as sql] - [next.jdbc :as jdbc] - [chef.database :as cdb] - [ring.util.response :as ruresp] - [chef.utils :as cutils])) - -(defn render [recipe] - (cutils/gen-page "chef - Rezept bearbeiten" - [:div {:style {:margin-left "1em"}} - [:h1 "Rezept bearbeiten"] - [:form {:style {:width "50%"} - :hx-post (str "/api/admin/edit-recipe/" (:recipes/id recipe)) - :hx-swap "none"} - [:input {:type :text :name "title" :placeholder "Titel" - :value (:recipes/title recipe)}] - [:h2 "Kategorie"] - [:select {:name "category"} - (for [category (->> (sql/format {:select [:*] - :from [:categories]}) - (jdbc/execute! @cdb/db) - (filter #(pos? (:categories/id %))))] - [:option {:value (:categories/id category) - :selected (= (:categories/id category) (:recipes/category recipe))} - (cutils/category-path category)])] - [:h2 "Zutaten"] - [:div {:style {:display :flex}} - [:p {:style {:margin-right "0.5em"}} "Pro"] - [:select {:name "ingredients-unit" - :style {:height :fit-content - :padding "0.3em"}} - [:option {:value 0 :selected (= (:recipes/unit recipe) 0)} "Portion"] - [:option {:value 1 :selected (= (:recipes/unit recipe) 1)} "Person"]] - [:p ":"]] - [:textarea {:name "ingredients"} - (:recipes/ingredients recipe)] - ;; Regex: ([A-z0-9 ]*)=([0-9]*) ?([A-z]*) - [:p "(Je Zeile eine Zutat, in dem Format " [:code "[Beschreibung der Zutat]=[Menge als Zahl][Einheit der Menge]"] ".)"] - [:h2 "Zubereitung"] - [:textarea {:name "preparation"} - (:recipes/preparation recipe)] - [:button {:type :submit - :style {:margin-top "1em"}} "Speichern"]]] - [:script (hutil/raw-string "window.addEventListener(\"htmx:afterRequest\", () => window.close())")])) - -(defn handler [req] - (cutils/auth-only req - (if-let [id (try (Integer/parseInt (get-in req [:path-params :id])) - (catch Exception _ nil))] - (->> (sql/format {:select [:*] - :from [:recipes] - :where [:= :id id]}) - (jdbc/execute! @cdb/db) - first - render - html/html - str - ruresp/response) - (ruresp/bad-request "Bad request.")))) |