diff options
-rw-r--r-- | src/chef/database.clj | 6 | ||||
-rw-r--r-- | src/chef/database/root_category.clj | 7 | ||||
-rw-r--r-- | src/chef/pages/admin.clj | 39 |
3 files changed, 35 insertions, 17 deletions
diff --git a/src/chef/database.clj b/src/chef/database.clj index ec5d89d..af34b72 100644 --- a/src/chef/database.clj +++ b/src/chef/database.clj @@ -2,11 +2,13 @@ (:require [honey.sql :as sql] [next.jdbc :as jdbc] - [chef.database.init :as cdinit])) + [chef.database.init :as cdinit] + [chef.database.root-category :as cdroot-category])) (def db (delay (jdbc/get-datasource {:dbtype "sqlite" :dbname "chef.db"}))) -(def ^:private patches {"init" cdinit/exec!}) +(def ^:private patches {"init" cdinit/exec! + "root-category" cdroot-category/exec!}) (defn run-patches! [] (->> (sql/format {:create-table [:applied-patches :if-not-exists] :with-columns [[:name :text]]}) diff --git a/src/chef/database/root_category.clj b/src/chef/database/root_category.clj new file mode 100644 index 0000000..a01e3e6 --- /dev/null +++ b/src/chef/database/root_category.clj @@ -0,0 +1,7 @@ +(ns chef.database.root-category + (:require [next.jdbc :as jdbc] + [honey.sql :as sql])) + +(defn exec! [db] + (jdbc/execute! db (sql/format {:insert-into [:categories] + :values [{:id -1}]}))) diff --git a/src/chef/pages/admin.clj b/src/chef/pages/admin.clj index 031da8c..859dda4 100644 --- a/src/chef/pages/admin.clj +++ b/src/chef/pages/admin.clj @@ -9,28 +9,35 @@ (defn- render-category [data children] [:li [:p {:style {:display :inline-block}} - (if (some? data) + (if (pos? (:categories/id data)) [:input {:type :text :placeholder "Name" :value (:categories/name data) :name "name" :hx-post (str "/api/admin/edit-category/" (:categories/id data)) :hx-trigger "change"}] "Startseite")] - [:p {:style {:display :inline-block - :margin-left "1em" - :margin-right "1em"}} "->"] - [:input {:type :text :placeholder "Frage" - :style {:display :inline-block - :width :auto} - :value (:categories/question data) - :name "question" - :hx-post (str "/api/admin/edit-category/" (:categories/id data)) - :hx-trigger "change"}] ; TODO: only show when category has children + (when (or (neg? (:categories/id data)) + (->> (sql/format {:select [:*] + :from [:categories] + :where [:= :parent (:categories/id data)]}) + (jdbc/execute! @cdb/db) + count + pos?)) + (list [:p {:style {:display :inline-block + :margin-left "1em" + :margin-right "1em"}} "->"] + [:input {:type :text :placeholder "Frage" + :style {:display :inline-block + :width :auto} + :value (:categories/question data) + :name "question" + :hx-post (str "/api/admin/edit-category/" (:categories/id data)) + :hx-trigger "change"}])) [:img {:src "/static/icons/plus.svg" :height "30em" :style {:vertical-align :middle :margin-left "1em"} :hx-post (str "/api/admin/create-category" - (when (some? data) (str "?parent=" (:categories/id data)))) + (when (pos? (:categories/id data)) (str "?parent=" (:categories/id data)))) :hx-swap "none"}] [:img {:src "/static/icons/trash.svg" :height "30em" :style {:vertical-align :middle @@ -48,12 +55,14 @@ (cutils/gen-page "chef - Admin" [:h1 "chef - Admin"] [:h2 "Kategorien"] - ;;TODO: add delete icons to non-root categories [:ul - (render-category nil + (render-category (first (->> (sql/format {:select [:*] + :from [:categories] + :where [:= :id -1]}) + (jdbc/execute! @cdb/db))) (->> (sql/format {:select [:*] :from [:categories] - :where [:is :parent :null]}) + :where [:and [:is :parent :null] [:> :id 0]]}) (jdbc/execute! @cdb/db)))] [:h2 "Rezepte"] [:i "Coming soon..."])) |