summaryrefslogtreecommitdiff
path: root/src/chef/logic/recipes.clj
blob: 1fd1db53038bbcd99c275512591080ae27953976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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)))))