diff options
Diffstat (limited to 'src/controller/routes.phel')
| -rw-r--r-- | src/controller/routes.phel | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/controller/routes.phel b/src/controller/routes.phel index 4569472..75d1d5f 100644 --- a/src/controller/routes.phel +++ b/src/controller/routes.phel @@ -1,5 +1,6 @@ (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) @@ -11,12 +12,12 @@ (pdo/exec conn "create table if not exists authors (id integer primary key autoincrement, name varchar(255))") (pdo/exec conn "create table if not exists authors_books (author_id integer, book_id integer)") -(defn index-handler [req] +(defn index-handler [req router] (let [books (for [b :in (pdo/select conn "select * from 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)})})] (h/response-from-map {:status 200 - :body (index-html books)}))) + :body (index-html books router)}))) (defn ping-handler [req] (h/response-from-map {:status 200 @@ -32,3 +33,9 @@ :author_id (or (pdo/fetch-column (pdo/query conn "select id from authors where name = :name" {:name a})) (pdo/insert conn :authors {:name a}))}))))) (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})] + (h/response-from-map {:status 200 :body (author-html author books)}))) |
