summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Tom Willemse2026-09-08 23:43:42 -0700
committerGravatar Tom Willemse2026-09-08 23:43:42 -0700
commitbf01dcf88b8f197ea6a968827e6ea7d0c3b75908 (patch)
tree8f79801ad1056cea7d7e8dded2d2057dd94bc083 /src
parent3e1a51d4832939d7ba4d753142a45cdbbcacb817 (diff)
downloadlezer-bf01dcf88b8f197ea6a968827e6ea7d0c3b75908.tar.gz
lezer-bf01dcf88b8f197ea6a968827e6ea7d0c3b75908.zip
Move queries into separate functions
These look terrible, but I'm trying to add a little bit of separation, proper abstractions will come later.
Diffstat (limited to 'src')
-rw-r--r--src/controller/routes.phel82
1 files changed, 58 insertions, 24 deletions
diff --git a/src/controller/routes.phel b/src/controller/routes.phel
index 38d1882..31398e7 100644
--- a/src/controller/routes.phel
+++ b/src/controller/routes.phel
@@ -8,13 +8,49 @@
(:require phel.string :as str)
(:require lezer.db :refer [conn]))
+(defn get-books []
+ (pdo/select conn "select * from books"))
+
+(defn get-authors-by-book [book]
+ (pdo/select
+ conn
+ "select * from authors where id in (select author_id from authors_books where book_id = :book_id)"
+ {:book_id (:id book)}))
+
+(defn create-book [fields]
+ (pdo/insert conn :books fields))
+
+(defn get-or-create-author-by-name [fields]
+ (or (pdo/fetch-column
+ (pdo/query conn "select id from authors where name = :name"
+ {:name (:name fields)}))
+ (pdo/insert conn :authors fields)))
+
+(defn get-book-pages-by-id [book-id]
+ (pdo/fetch-column (pdo/query conn "select pages from books where id = :id" {:id book-id})))
+
+(defn create-book-session [fields]
+ (pdo/insert conn :books_sessions fields))
+
+(defn create-session-progress [fields]
+ (pdo/insert conn :sessions_progress fields))
+
(defn index-handler [req router]
- (let [books (for [b :in (pdo/select conn "select * from books")]
+ (let [books (for [b :in (get-books)]
{:book b
- :authors (pdo/select conn "select * from authors where id in (select author_id from authors_books where book_id = :book_id)" {:book_id (:id b)})})]
+ :authors (get-authors-by-book b)})]
(h/response-from-map {:status 200
:body (index-html books router)})))
+(defn get-author-by-id [id]
+ (first (pdo/select conn "select * from authors where id = :author_id" {:author_id id})))
+
+(defn get-books-by-author-id [id]
+ (pdo/select conn "select * from books where id in (select book_id from authors_books where author_id = :author_id)" {:author_id id}))
+
+(defn add-author-to-book [book-id fields]
+ (pdo/insert conn :authors_books {:book_id book-id
+ :author_id (get-or-create-author-by-name fields)}))
(defn book-create-handler [request]
(let [title (get-in request [:parsed-body "title"])
@@ -23,38 +59,36 @@
pages (get-in request [:parsed-body "pages"])]
(pdo/with-transaction conn
(let [time (php/time)
- book-id (pdo/insert conn :books {:title title
- :subtitle subtitle
- :pages pages
- :created_at time
- :updated_at time})]
+ book-id (create-book {:title title
+ :subtitle subtitle
+ :pages pages
+ :created_at time
+ :updated_at time})]
(for [a :in authors]
- (pdo/insert conn :authors_books {:book_id book-id
- :author_id (or (pdo/fetch-column (pdo/query conn "select id from authors where name = :name" {:name a}))
- (pdo/insert conn :authors {:name (str/trim a)
- :created_at time
- :updated_at time}))})))))
+ (add-author-to-book book-id {:name (str/trim a)
+ :created_at time
+ :updated_at time})))))
(h/response-from-map {:status 301 :headers {:location "/"}}))
(defn book-session-create-handler [request]
(let [id (get-in request [:attributes :match :path-params :id])
- pages (pdo/fetch-column (pdo/query conn "select pages from books where id = :id" {:id id}))
+ pages (get-book-pages-by-id id)
time (php/time)]
(pdo/with-transaction conn
- (let [session-id (pdo/insert conn :books_sessions {:book_id id
- :status "started"
- :started_at time
- :created_at time
- :updated_at time})]
- (pdo/insert conn :sessions_progress {:session_id session-id
- :progress 0
- :total pages
+ (let [session-id (create-book-session {:book_id id
+ :status "started"
+ :started_at time
:created_at time
- :updated_at time}))))
+ :updated_at time})]
+ (create-session-progress {:session_id session-id
+ :progress 0
+ :total pages
+ :created_at time
+ :updated_at time}))))
(h/response-from-map {:status 301 :headers {:location "/"}}))
(defn author-view-handler [req]
(let [id (get-in req [:attributes :match :path-params :id])
- author (first (pdo/select conn "select * from authors where id = :author_id" {:author_id id}))
- books (pdo/select conn "select * from books where id in (select book_id from authors_books where author_id = :author_id)" {:author_id id})]
+ author (get-author-by-id id)
+ books (get-books-by-author-id id)]
(h/response-from-map {:status 200 :body (author-html author books)})))