summaryrefslogtreecommitdiff
path: root/pages/shop/order.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/order.fnl
parentd8039a77d582f696ab98b2a6d02ce924fbacfa41 (diff)
implement shop
Diffstat (limited to 'pages/shop/order.fnl')
-rw-r--r--pages/shop/order.fnl61
1 files changed, 61 insertions, 0 deletions
diff --git a/pages/shop/order.fnl b/pages/shop/order.fnl
new file mode 100644
index 0000000..6edaf8a
--- /dev/null
+++ b/pages/shop/order.fnl
@@ -0,0 +1,61 @@
+(local lib (require :lib))
+(local templates (require :templates))
+(local html (require :vendor.html))
+
+(fn content-template [db basket basket-total]
+ [[:div {:class "side"}
+ (templates.header "/shop/order")]
+ [:div {:class "content"}
+ (if (< 0 (# basket))
+ [:section {}
+ [:h2 {} "Состав заказа"]
+ [:div {}
+ (table.unpack
+ (icollect [_ item (ipairs basket)]
+ (templates.basket-item item "/shop/order")))]
+ [:div {} "~~~"]
+ [:div {:class "basket-total"} (.. "Итого: " basket-total "₽")]]
+ "")
+ [:section {}
+ [:h2 {} "Данные для связи"]
+ [:form {:class "form" :method "POST"}
+ [:div {:class "form-row"}
+ [:label {:for "name"} "Имя"]
+ [:input {:type "text" :id "name" :name "name" :required "required"}]]
+ [:div {:class "form-row"}
+ [:label {:for "contact"} "Телеграм или Email для связи"]
+ [:input {:type "text" :id "contact" :name "contact" :required "required"}]]
+ [:div {:class "form-row"}
+ [:input {:type "checkbox" :id "everything-is-correct"
+ :name "everything-is-correct" :required "required"}]
+ [:label {:for "everything-is-correct"} "Данные заказа верны"]]
+ [:div {:class "form-row"}
+ [:input {:type "checkbox" :id "agree-to-conditions"
+ :name "agree-to-conditions" :required "required"}]
+ [:label {:for "agree-to-conditions"} "Согласен с условиями"]]
+ [:button {:type "submit"} "Оформить заказ"]]]]])
+
+(fn place-order [db order-id form]
+ (_G.must
+ (luna.db.exec db
+ "UPDATE orders SET placement_time = ?, first_name = ?, contact = ?"
+ [(os.date "%Y-%m-%d %H:%M:%S") form.name form.contact])))
+
+(fn render [request db]
+ (let [order-id (lib.order-id request)
+ basket (if order-id (lib.basket db order-id) [])
+ basket-total (accumulate [sum 0 _ v (ipairs basket)]
+ (+ sum (* v.quantity v.price-per)))]
+ (if (= request.method "POST")
+ (do
+ (place-order db order-id (lib.parse-values request.body))
+ (values 302 {:Location "/shop/success"} ""))
+ (if (< 0 (# basket))
+ (values
+ 200 {}
+ (html.render
+ (templates.base (content-template db basket basket-total))
+ true))
+ (values 302 {:Location "/shop"} "")))))
+
+{: render}