diff options
| author | 2026-09-14 00:51:56 -0700 | |
|---|---|---|
| committer | 2026-09-14 00:53:27 -0700 | |
| commit | ddead52187bfc77e970550ae1fb663b0da88d15f (patch) | |
| tree | ee077241aefbc38712102669829c36613e2896d9 /src/model/authors.phel | |
| parent | 24fc3ae9367fab7468cd3600cc4c3f61ad9506f6 (diff) | |
| download | lezer-ddead52187bfc77e970550ae1fb663b0da88d15f.tar.gz lezer-ddead52187bfc77e970550ae1fb663b0da88d15f.zip | |
Move database functions to model modules
Diffstat (limited to 'src/model/authors.phel')
| -rw-r--r-- | src/model/authors.phel | 32 |
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)))) |
