summaryrefslogtreecommitdiffstats
path: root/src/model/authors.phel
blob: 518c13e79b0212b7b85f82aaad8766560e7ab902 (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
(ns lezer.model.authors
  (:require lezer.db :refer [conn])
  (:require phel.pdo)
  (:require phel.sql))

(defn for-book [book]
  (let [[query params] (sql/format
                        {:select [:*]
                         :from [:authors]
                         :where [:in :id {:select [:author_id]
                                          :from [:authors_books]
                                          :where [:= :book_id (:id book)]}]})]
    (-> (pdo/prepare conn query)
        (pdo/execute params)
        (pdo/fetch-all))))

(defn find-or-create [name fields]
  (let [[query params] (sql/format {:select [:id]
                                    :from [:authors]
                                    :where [:= :name name]})]
    (or (-> (pdo/prepare conn query)
            (pdo/execute params)
            (pdo/fetch-column))
        (pdo/insert conn :authors (conj {:name name} fields)))))

(defn find [id]
  (let [[query params] (sql/format {:select [:*]
                                    :from [:authors]
                                    :where [:= :id id]})]
    (-> (pdo/prepare conn query)
        (pdo/execute params)
        (pdo/fetch))))