From 3f5ade2e7a139bb4405437e8fc5546aafc7b05ef Mon Sep 17 00:00:00 2001 From: unwox Date: Fri, 3 Oct 2025 11:56:37 +0600 Subject: WIP shop --- pages/shop/cart/add.fnl | 53 +++++++++++++++++++++------------------------- pages/shop/cart/remove.fnl | 9 +++----- 2 files changed, 27 insertions(+), 35 deletions(-) (limited to 'pages/shop/cart') diff --git a/pages/shop/cart/add.fnl b/pages/shop/cart/add.fnl index 36e3e41..53366b4 100644 --- a/pages/shop/cart/add.fnl +++ b/pages/shop/cart/add.fnl @@ -1,38 +1,33 @@ (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]))) +(local shop (require :shop)) (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"))}) - {})) + (var order-id (shop.order-id request)) + (var headers {}) + + (when (not order-id) + (local next-week + (os.date "%a, %d %b %Y %H:%M:%S GMT" (+ (os.time) (* 60 60 24 7)))) + (set order-id (shop.create-order db)) + (set headers + {:Set-Cookie (.. "order=" order-id "; Path=/; " + "Expires=" next-week "; " + "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"))) + (let [body-values (lib.parse-values request.body)] + (if (and order-id request.body + (< 0 (tonumber body-values.quantity))) + (do + (shop.create-order-line + db order-id body-values.name body-values.quantity) + (tset headers :Location (_G.must + (luna.http.decode-url + body-values.redirect-url))) + (values 302 headers "")) + (values 400 {} "bad body")))) (values 404 {} "not found"))) {: render} diff --git a/pages/shop/cart/remove.fnl b/pages/shop/cart/remove.fnl index d5e3531..0cdc417 100644 --- a/pages/shop/cart/remove.fnl +++ b/pages/shop/cart/remove.fnl @@ -1,16 +1,13 @@ (local lib (require :lib)) +(local shop (require :shop)) (fn render [request db] (if (= request.method "POST") - (let [order-id (lib.order-id request)] + (let [order-id (shop.order-id request)] (if (and order-id request.body) (do (local body-values (lib.parse-values request.body)) - (_G.must - (luna.db.exec - db - "DELETE FROM order_lines WHERE id = ? AND order_id = ?" - [body-values.id order-id])) + (shop.delete-order-line db body-values.id) (values 302 {:Location (_G.must -- cgit v1.2.3