diff options
| author | 2026-09-14 00:51:56 -0700 | |
|---|---|---|
| committer | 2026-09-14 00:53:27 -0700 | |
| commit | ddead52187bfc77e970550ae1fb663b0da88d15f (patch) | |
| tree | ee077241aefbc38712102669829c36613e2896d9 /src/controller | |
| parent | 24fc3ae9367fab7468cd3600cc4c3f61ad9506f6 (diff) | |
| download | lezer-ddead52187bfc77e970550ae1fb663b0da88d15f.tar.gz lezer-ddead52187bfc77e970550ae1fb663b0da88d15f.zip | |
Move database functions to model modules
Diffstat (limited to 'src/controller')
| -rw-r--r-- | src/controller/routes.phel | 140 |
1 files changed, 31 insertions, 109 deletions
diff --git a/src/controller/routes.phel b/src/controller/routes.phel index d891ec4..980cf48 100644 --- a/src/controller/routes.phel +++ b/src/controller/routes.phel @@ -6,105 +6,27 @@ (:require phel.router :as r) (:require phel.http :as h) (:require phel.pdo) - (:require phel.sql) (:require phel.string :as str) - (:require lezer.db :refer [conn])) - -(defn- get-books [] - (let [[query _] (sql/format {:select [:*] - :from [:books]})] - (pdo/select conn query))) - -(defn- get-authors-by-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- get-sessions-by-book [book] - (let [[query params] (sql/format - {:select [:*] - :from [:books_sessions] - :where [:= :book_id (:id book)]})] - (-> (pdo/prepare conn query) - (pdo/execute params) - (pdo/fetch-all)))) - -(defn- create-book [fields] - (pdo/insert conn :books fields)) - -(defn- get-or-create-author-by-name [fields] - (let [[query params] (sql/format {:select [:id] - :from [:authors] - :where [:= :name (:name fields)]})] - (or (-> (pdo/prepare conn query) - (pdo/execute params) - (pdo/fetch-column)) - (pdo/insert conn :authors fields)))) - -(defn- get-book-pages-by-id [book-id] - (let [[query params] (sql/format {:select [:pages] - :from [:books] - :where [:= :id book-id]})] - (-> (pdo/prepare conn query) - (pdo/execute params) - (pdo/fetch-column)))) - -(defn- create-book-session [fields] - (pdo/insert conn :books_sessions fields)) - -(defn- create-session-progress [fields] - (pdo/insert conn :sessions_progress fields)) - -(defn- get-book-by-id [id] - (let [[query params] (sql/format {:select [:*] - :from [:books] - :where [:= :id id]})] - (-> (pdo/prepare conn query) - (pdo/execute params) - (pdo/fetch)))) - -(defn- get-author-by-id [id] - (let [[query params] (sql/format {:select [:*] - :from [:authors] - :where [:= :id id]})] - (-> (pdo/prepare conn query) - (pdo/execute params) - (pdo/fetch)))) - -(defn- get-books-by-author-id [id] - (let [[query params] (sql/format {:select [:*] - :from [:books] - :where [:in :id {:select [:book_id] - :from [:authors_books] - :where [:= :author_id id]}]})] - (-> (pdo/prepare conn query) - (pdo/execute params) - (pdo/fetch-all)))) - -(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)})) + (:require lezer.db :refer [conn]) + (:require lezer.model.books) + (:require lezer.model.authors) + (:require lezer.model.sessions) + (:require lezer.model.progress)) (defn index-handler [req router] - (let [books (for [b :in (get-books)] + (let [books (for [b :in (books/all)] {:book b - :authors (get-authors-by-book b)})] + :authors (authors/for-book b)})] (h/response-from-map {:status 200 :body (index-html books router)}))) (defn book-view-handler [request] (let [id (get-in request [:attributes :match :path-params :id]) - book (get-book-by-id id)] + book (books/find id)] (h/response-from-map {:status 200 :body (book-html (conj book - {:authors (get-authors-by-book book) - :sessions (get-sessions-by-book book)}))}))) + {:authors (authors/for-book book) + :sessions (sessions/for-book book)}))}))) (defn book-create-handler [request] (let [title (get-in request [:parsed-body "title"]) @@ -113,36 +35,36 @@ pages (get-in request [:parsed-body "pages"])] (pdo/with-transaction conn (let [time (php/time) - book-id (create-book {:title title - :subtitle subtitle - :pages pages - :created_at time - :updated_at time})] + book-id (books/create {:title title + :subtitle subtitle + :pages pages + :created_at time + :updated_at time})] (for [a :in authors] - (add-author-to-book book-id {:name (str/trim a) - :created_at time - :updated_at time}))))) + (books/add-author 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 (get-book-pages-by-id id) + pages (books/pages id) time (php/time)] (pdo/with-transaction conn - (let [session-id (create-book-session {:book_id id - :status "started" - :started_at time - :created_at time - :updated_at time})] - (create-session-progress {:session_id session-id - :progress 0 - :total pages - :created_at time - :updated_at time})))) + (let [session-id (sessions/create {:book_id id + :status "started" + :started_at time + :created_at time + :updated_at time})] + (progress/create {: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 (get-author-by-id id) - books (get-books-by-author-id id)] + author (authors/find id) + books (books/for-author id)] (h/response-from-map {:status 200 :body (author-html author books)}))) |
