summaryrefslogtreecommitdiff
path: root/pages/shop/cart/add.fnl
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2025-08-31 17:51:57 +0600
committerunwox <me@unwox.com>2025-09-04 20:14:11 +0600
commit66c51b0e714fa8a1c80784108191270babc8525e (patch)
tree0640549f522092096d83c78b9be9b1fa4a03929e /pages/shop/cart/add.fnl
parentd8039a77d582f696ab98b2a6d02ce924fbacfa41 (diff)
implement shop
Diffstat (limited to 'pages/shop/cart/add.fnl')
-rw-r--r--pages/shop/cart/add.fnl38
1 files changed, 38 insertions, 0 deletions
diff --git a/pages/shop/cart/add.fnl b/pages/shop/cart/add.fnl
new file mode 100644
index 0000000..36e3e41
--- /dev/null
+++ b/pages/shop/cart/add.fnl
@@ -0,0 +1,38 @@
+(local lib (require :lib))
+
+(fn create-order [db]
+ (let [id (_G.must (luna.crypto.random-string 64))]
+ (_G.must
+ (luna.db.exec
+ db "INSERT INTO orders (id, creation_time) VALUES (?, ?)"
+ [id (lib.now)]))
+ id))
+
+(fn create-order-line [db order-id name quantity]
+ (_G.must
+ (luna.db.exec
+ db
+ "INSERT INTO order_lines (order_id, product_name, quantity) VALUES (?, ?, ?)"
+ [order-id name quantity])))
+
+(fn render [request db]
+ (if (= request.method "POST")
+ (do
+ (var order-id (lib.order-id request))
+ (var headers
+ (if (not order-id)
+ (do
+ (set order-id (create-order db))
+ {:Set-Cookie (.. "order= " order-id "; HttpOnly; SameSite=strict"
+ (if luna.debug? "" "; Secure"))})
+ {}))
+
+ (if (and order-id request.body)
+ (let [body-values (lib.parse-values request.body)]
+ (create-order-line db order-id body-values.name body-values.quantity)
+ (tset headers :Location "/shop")
+ (values 302 headers ""))
+ (values 400 {} "bad body")))
+ (values 404 {} "not found")))
+
+{: render}