blob: 096e23e51ffb1f57bf2c02569da0878695336a43 (
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
(ns lezer.controller.routes
(:require lezer.view.main :refer [index-html])
(:require lezer.view.author :refer [author-html])
(:require lezer.module.greet :as g)
(: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 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-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)}))
(defn index-handler [req router]
(let [books (for [b :in (get-books)]
{:book b
:authors (get-authors-by-book b)})]
(h/response-from-map {:status 200
:body (index-html books router)})))
(defn book-create-handler [request]
(let [title (get-in request [:parsed-body "title"])
subtitle (get-in request [:parsed-body "subtitle"])
authors (str/split (get-in request [:parsed-body "author"]) #",")
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})]
(for [a :in authors]
(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 (get-book-pages-by-id 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}))))
(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)]
(h/response-from-map {:status 200 :body (author-html author books)})))
|