summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemse2026-08-24 00:21:43 -0700
committerGravatar Tom Willemse2026-08-24 00:22:01 -0700
commit94a7f1fc01ec27f85853294c167c1ca05c3269b8 (patch)
tree1e33f63ec9e44903173daabcf1a577eed3d7b634
parent2edfa920e0be2b3d63f3c6b0fb36f0c1c32c4339 (diff)
downloadlezer-94a7f1fc01ec27f85853294c167c1ca05c3269b8.tar.gz
lezer-94a7f1fc01ec27f85853294c167c1ca05c3269b8.zip
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.
-rw-r--r--composer.json9
-rw-r--r--php.ini1
-rw-r--r--src/app.phel3
-rw-r--r--src/controller/routes.phel17
-rw-r--r--src/view/main.phel31
5 files changed, 44 insertions, 17 deletions
diff --git a/composer.json b/composer.json
index 57f4b25..39d96fe 100644
--- a/composer.json
+++ b/composer.json
@@ -12,8 +12,15 @@
"type": "project",
"require": {
"php": ">=8.3",
- "phel-lang/phel-lang": ">=0.50"
+ "phel-lang/phel-lang": "^0.50",
+ "phel-lang/phel-pdo": "dev-bump"
},
+ "repositories": [
+ {
+ "type": "path",
+ "url": "../../foreign/phel-pdo"
+ }
+ ],
"require-dev": {
"symfony/var-dumper": "^6.4"
},
diff --git a/php.ini b/php.ini
new file mode 100644
index 0000000..603c36a
--- /dev/null
+++ b/php.ini
@@ -0,0 +1 @@
+extension=pdo_sqlite
diff --git a/src/app.phel b/src/app.phel
index 9e1e0bd..ac3b94e 100644
--- a/src/app.phel
+++ b/src/app.phel
@@ -9,7 +9,8 @@
[["/" {:handler routes/index-handler}]
["/ping" {:name ::ping
:get {:handler routes/ping-handler}
- :post {:handler routes/ping-handler}}]])))
+ :post {:handler routes/ping-handler}}]
+ ["/book" {:post {:handler routes/book-create-handler}}]])))
(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 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")}))
diff --git a/src/view/main.phel b/src/view/main.phel
index 5d369ab..a47f308 100644
--- a/src/view/main.phel
+++ b/src/view/main.phel
@@ -20,16 +20,23 @@
[:a {:href "https://github.com/phel-lang/phel-lang"} "GitHub"]
[:a {:href "https://gitter.im/phel-lang/community"} "Community"]])
-(def- content
- [:div {:class "flex-center position-ref full-height"}
- [:form
- [:label {:for "title"} "Title: "]
- [:input {:type "text" :name "title"}]
- [:br]
- [:label {:for "authors"} "Author: "]
- [:input {:type "text" :name "author"}]]])
+(defn content [books]
+ (html
+ (doctype :html5)
+ [:html
+ head
+ [:body
+ [:ul
+ (for [book :in books]
+ [:li (:title book)])]
+ [:div {:class "flex-center position-ref full-height"}
+ [:form {:action "/book" :method "POST"}
+ [:label {:for "title"} "Title: "]
+ [:input {:type "text" :name "title"}]
+ [:br]
+ [:label {:for "authors"} "Author: "]
+ [:input {:type "text" :name "author"}]
+ [:input {:type "submit"}]]]]]))
-(def index-html (html
- (doctype :html5)
- head
- content))
+(defn index-html [books]
+ (content books))