diff options
Diffstat (limited to 'src/chef/logic')
-rw-r--r-- | src/chef/logic/categories.clj | 72 | ||||
-rw-r--r-- | src/chef/logic/recipes.clj | 60 |
2 files changed, 132 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 " > "))) diff --git a/src/chef/logic/recipes.clj b/src/chef/logic/recipes.clj new file mode 100644 index 0000000..1fd1db5 --- /dev/null +++ b/src/chef/logic/recipes.clj @@ -0,0 +1,60 @@ +(ns chef.logic.recipes + (:require [clojure.java.io :as cjio] + [clojure.string :as cstr] + [honey.sql :as sql] + [next.jdbc :as jdbc] + [chef.database :as cdb]) + (:import java.io.File)) + +(defn get-all-recipes [] + (->> {:select [:*] + :from [:recipes]} + sql/format + (jdbc/execute! @cdb/db))) + +(defn get-recipe [id] + (->> {:select [:*] + :from [:recipes] + :where [:= :id id]} + sql/format + (jdbc/execute! @cdb/db) + first)) + +(defn create-recipe! [] + (->> {:insert-into [:recipes] + :values [{:title "New recipe"}]} + sql/format + (jdbc/execute! @cdb/db))) + +(defn delete-recipe! [id] + (->> {:delete-from [:recipes] + :where [:= :id id]} + sql/format + (jdbc/execute! @cdb/db))) + +(defn update-recipe! [id updates] + (->> {:update :recipes + :set updates + :where [:= :id id]} + sql/format + (jdbc/execute! @cdb/db))) + +(defn get-recipe-thumbnail [recipe-id] + (let [thumbnails-folder (File. "./thumbnails/")] + (->> thumbnails-folder + .listFiles + (filter #(cstr/starts-with? (.getName ^File %) + (str recipe-id "."))) + first))) + +(defn remove-recipe-thumbnail! [id] + (when-let [file (get-recipe-thumbnail id)] + (.delete ^File file))) + +(defn set-recipe-thumbnail! [id multipart-param] + (when-let [existing-thumbnail-file (get-recipe-thumbnail id)] + (.delete ^File existing-thumbnail-file)) + (cjio/copy (:tempfile multipart-param) + (File. (str "./thumbnails/" id "." (-> (:filename multipart-param) + (cstr/split #"\.") + last))))) |