diff options
| author | 2026-08-25 23:02:08 -0700 | |
|---|---|---|
| committer | 2026-08-25 23:02:08 -0700 | |
| commit | 9d4733413c791c8e4f589a197a0da91dad279740 (patch) | |
| tree | 42ae877b286de0a66ccd17efbcb258f5ea07b823 | |
| parent | 8bf0bca2c77a4af53002f43941fe634f3c12ef56 (diff) | |
| download | lezer-9d4733413c791c8e4f589a197a0da91dad279740.tar.gz lezer-9d4733413c791c8e4f589a197a0da91dad279740.zip | |
| -rw-r--r-- | src/app.phel | 19 | ||||
| -rw-r--r-- | src/controller/routes.phel | 11 | ||||
| -rw-r--r-- | src/view/author.phel | 14 | ||||
| -rw-r--r-- | src/view/main.phel | 18 |
4 files changed, 43 insertions, 19 deletions
diff --git a/src/app.phel b/src/app.phel index ac3b94e..8bb61c1 100644 --- a/src/app.phel +++ b/src/app.phel @@ -3,14 +3,19 @@ (:require phel.router :as r) (:require phel.http :as h)) +(def- router + (r/router + [["/" {:handler (fn [req] + (routes/index-handler req router))}] + ["/ping" {:name ::ping + :get {:handler routes/ping-handler} + :post {:handler routes/ping-handler}}] + ["/book" {:post {:handler routes/book-create-handler}}] + ["/author/{id}" {:name :author + :get {:handler routes/author-view-handler}}]])) + (def app - (r/handler - (r/router - [["/" {:handler routes/index-handler}] - ["/ping" {:name ::ping - :get {:handler routes/ping-handler} - :post {:handler routes/ping-handler}}] - ["/book" {:post {:handler routes/book-create-handler}}]]))) + (r/handler router)) (when-not *build-mode* (h/emit-response (app (h/request-from-globals)))) 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)}))) diff --git a/src/view/author.phel b/src/view/author.phel new file mode 100644 index 0000000..4253941 --- /dev/null +++ b/src/view/author.phel @@ -0,0 +1,14 @@ +(ns lezer.view.author + (:require phel.html :refer [html doctype])) + +(defn author-html [author books] + (html + (doctype :html5) + [:html + [:head + [:title (str "Author: " author " - Lezer")]] + [:body + [:h1 (:name author)] + [:ul + (for [book :in books] + [:li (:title book)])]]])) diff --git a/src/view/main.phel b/src/view/main.phel index 1be6226..9476591 100644 --- a/src/view/main.phel +++ b/src/view/main.phel @@ -1,6 +1,6 @@ (ns lezer.view.main + (:require phel.router :as r) (:require phel.html :refer [html doctype]) - (:require phel.pdo) (:require phel.string :as str)) (def- head @@ -22,7 +22,7 @@ [:a {:href "https://github.com/phel-lang/phel-lang"} "GitHub"] [:a {:href "https://gitter.im/phel-lang/community"} "Community"]]) -(defn content [books] +(defn content [books router] (html (doctype :html5) [:html @@ -30,12 +30,10 @@ [:body [:ul (for [b :in books] - (let [authors (:authors b) - book (:book b)] - [:li (:title book) - (if (not (empty? authors)) - (str " by " - (str/join ", " (map :name authors))))]))] + [:li (:title (:book b)) + " by " + (for [a :in (:authors b)] + [:a {:href (r/generate router :author {:id (:id a)})} (:name a)])])] [:div {:class "flex-center position-ref full-height"} [:form {:action "/book" :method "POST"} [:label {:for "title"} "Title: "] @@ -45,5 +43,5 @@ [:input {:type "text" :name "author"}] [:input {:type "submit"}]]]]])) -(defn index-html [books] - (content books)) +(defn index-html [books router] + (content books router)) |
