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/logic/categories.clj | |
parent | 229299146376a2b847f4fe3f331efbd26c0abc70 (diff) | |
download | chef-8e23d9dade945f87f5fc7fb15042a53a7eeb9a9e.tar.xz chef-8e23d9dade945f87f5fc7fb15042a53a7eeb9a9e.zip |
Refactor project structure
Diffstat (limited to 'src/chef/logic/categories.clj')
-rw-r--r-- | src/chef/logic/categories.clj | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/chef/logic/categories.clj b/src/chef/logic/categories.clj new file mode 100644 index 0000000..aa9931e --- /dev/null +++ b/src/chef/logic/categories.clj @@ -0,0 +1,72 @@ +(ns chef.logic.categories + (:require [clojure.string :as cstr] + [honey.sql :as sql] + [next.jdbc :as jdbc] + [chef.database :as cdb])) + +(defn get-all-categories [] + (->> {:select [:*] + :from [:categories]} + sql/format + (jdbc/execute! @cdb/db) + (filter #(pos? (:categories/id %))) ; Filter out root category + )) + +(defn get-category [id] + (->> {:select [:*] + :from [:categories] + :where [:= :id id]} + sql/format + (jdbc/execute! @cdb/db) + first)) + +(defn find-categories-with-parent [parent-id] + (->> {:select [:*] + :from [:categories] + :where [:= :parent parent-id]} + sql/format + (jdbc/execute! @cdb/db))) + +(defn create-category! [parent] + (->> {:insert-into [:categories] + :values [{:name "New category" + :parent parent}]} + sql/format + (jdbc/execute! @cdb/db))) + +(defn- delete-category! [id] + (->> {:delete-from [:categories] + :where [:= :id id]} + sql/format + (jdbc/execute! @cdb/db))) + +(defn- delete-category-children! [id] + (doseq [child (find-categories-with-parent id)] + (delete-category! (:categories/id child)) + (delete-category-children! (:categories/id child)))) + +(defn delete-category-and-children! [id] + (delete-category! id) + (delete-category-children! id)) + +(defn update-category! [id updates] + (->> {:update :categories + :set updates + :where [:= :id id]} + sql/format + (jdbc/execute! @cdb/db))) + +(defn get-parents [category] + (loop [parents (list) + category category] + (let [updated-parents (conj parents category)] + (if (not= -1 (:categories/parent category)) + (recur updated-parents + (get-category (:categories/parent category))) + updated-parents)))) + +(defn generate-path [category] + (->> category + get-parents + (map #(:categories/name %)) + (cstr/join " > "))) |