diff options
Diffstat (limited to 'src/chef/logic/recipes.clj')
-rw-r--r-- | src/chef/logic/recipes.clj | 60 |
1 files changed, 60 insertions, 0 deletions
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))))) |