summaryrefslogtreecommitdiffstats
path: root/src/model/authors.phel
diff options
context:
space:
mode:
Diffstat (limited to 'src/model/authors.phel')
-rw-r--r--src/model/authors.phel32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/model/authors.phel b/src/model/authors.phel
new file mode 100644
index 0000000..518c13e
--- /dev/null
+++ b/src/model/authors.phel
@@ -0,0 +1,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))))