From 8e23d9dade945f87f5fc7fb15042a53a7eeb9a9e Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 14 Jun 2025 11:49:28 +0200 Subject: Refactor project structure --- src/chef/logic/categories.clj | 72 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/chef/logic/categories.clj (limited to 'src/chef/logic/categories.clj') 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 " > "))) -- cgit v1.2.3