summaryrefslogtreecommitdiff
path: root/pages/shop/order.fnl
blob: 50e12da4bccc09d70e1737375b53701c45ae8145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(import-macros {:compile-html HTML} :macros)
(local lib (require :lib))
(local templates (require :templates))

(fn content-template [db basket basket-total]
  [(HTML
    [:div {:class "side"}
     (templates.header "/shop/order")])
   (HTML
    [: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 = ?"
     [(lib.now) 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 {} (templates.base (content-template db basket basket-total)))
        (values 302 {:Location "/shop"} "")))))

{: render}