summaryrefslogtreecommitdiff
path: root/src/chef/logic/recipes.clj
diff options
context:
space:
mode:
authorTim <contact@bytim.eu>2025-06-14 11:49:28 +0200
committerTim <contact@bytim.eu>2025-06-14 11:49:28 +0200
commit8e23d9dade945f87f5fc7fb15042a53a7eeb9a9e (patch)
treec0f5b6587b6f9f0b591b395c69ad7da08717a30b /src/chef/logic/recipes.clj
parent229299146376a2b847f4fe3f331efbd26c0abc70 (diff)
downloadchef-8e23d9dade945f87f5fc7fb15042a53a7eeb9a9e.tar.xz
chef-8e23d9dade945f87f5fc7fb15042a53a7eeb9a9e.zip
Refactor project structure
Diffstat (limited to 'src/chef/logic/recipes.clj')
-rw-r--r--src/chef/logic/recipes.clj60
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)))))