From 94a7f1fc01ec27f85853294c167c1ca05c3269b8 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Mon, 24 Aug 2026 00:21:43 -0700 Subject: Add simplistic database This doesn't include a full database, or even a secure one, but this is the first step into moving forward with this project. I can now write to and read from a database. --- src/controller/routes.phel | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/controller/routes.phel') diff --git a/src/controller/routes.phel b/src/controller/routes.phel index 5f93cc2..5016bb3 100644 --- a/src/controller/routes.phel +++ b/src/controller/routes.phel @@ -2,12 +2,23 @@ (:require lezer.view.main :refer [index-html]) (:require lezer.module.greet :as g) (:require phel.router :as r) - (:require phel.http :as h)) + (:require phel.http :as h) + (:require phel.pdo)) + +(def conn (pdo/connect "sqlite:test.db")) +(pdo/exec conn "create table if not exists books (id integer primary key autoincrement, title varchar(255))") (defn index-handler [req] - (h/response-from-map {:status 200 - :body index-html})) + (let [books (pdo/select conn "select * from books")] + (h/response-from-map {:status 200 + :body (index-html books)}))) (defn ping-handler [req] (h/response-from-map {:status 200 :body (g/greet (str "pong - " (rand)))})) + +(defn book-create-handler [request] + (let [title (get-in request [:parsed-body "title"])] + (pdo/insert conn :books {:title title})) + (h/response-from-map {:status 201 + :body (pdo/select conn "select * from books")})) -- cgit v1.3-2-g0d8e