shop.tonybtw.com

shop.tonybtw.com

https://git.tonybtw.com/shop.tonybtw.com.git git://git.tonybtw.com/shop.tonybtw.com.git
3,770 bytes raw
1
package handlers
2
3
import (
4
	"net/http"
5
	"strconv"
6
7
	"shop.tonybtw.com/internal/lib"
8
	"shop.tonybtw.com/internal/models"
9
	"shop.tonybtw.com/internal/views"
10
)
11
12
type Cart_Handler struct {
13
	ctx *lib.App_Context
14
}
15
16
func New_Cart_Handler(ctx *lib.App_Context) *Cart_Handler {
17
	return &Cart_Handler{ctx: ctx}
18
}
19
20
func (h *Cart_Handler) Show_Cart(w http.ResponseWriter, r *http.Request) {
21
	session_id := lib.Get_Session_ID(r)
22
	cart_json := h.ctx.Session_Store.Get_Cart(session_id)
23
	cart, _ := models.Parse_Cart(cart_json)
24
25
	items, err := models.Get_Cart_Items_With_Details(h.ctx.DB, cart)
26
	if err != nil {
27
		http.Error(w, "Failed to load cart", http.StatusInternalServerError)
28
		return
29
	}
30
31
	total := models.Get_Cart_Total(items)
32
	cart_count := models.Count_Cart_Items(cart)
33
34
	views.Cart(items, total, cart_count).Render(r.Context(), w)
35
}
36
37
func (h *Cart_Handler) Add_Item(w http.ResponseWriter, r *http.Request) {
38
	if err := r.ParseForm(); err != nil {
39
		http.Error(w, "Invalid form", http.StatusBadRequest)
40
		return
41
	}
42
43
	product_id, _ := strconv.Atoi(r.FormValue("product_id"))
44
	variant_id, _ := strconv.Atoi(r.FormValue("variant_id"))
45
	quantity, _ := strconv.Atoi(r.FormValue("quantity"))
46
47
	if quantity < 1 {
48
		quantity = 1
49
	}
50
51
	session_id := lib.Get_Or_Create_Session_ID(w, r)
52
	cart_json := h.ctx.Session_Store.Get_Cart(session_id)
53
	cart, _ := models.Parse_Cart(cart_json)
54
55
	cart = models.Add_To_Cart(cart, product_id, variant_id, quantity)
56
57
	updated_cart_json, _ := models.Serialize_Cart(cart)
58
	h.ctx.Session_Store.Set_Cart(session_id, updated_cart_json)
59
60
	cart_count := models.Count_Cart_Items(cart)
61
62
	if lib.Is_HTMX_Request(r) {
63
		views.Cart_Widget(cart_count).Render(r.Context(), w)
64
		return
65
	}
66
67
	referer := r.Header.Get("Referer")
68
	if referer == "" {
69
		referer = "/"
70
	}
71
	lib.Redirect(w, r, referer)
72
}
73
74
func (h *Cart_Handler) Update_Item(w http.ResponseWriter, r *http.Request) {
75
	if err := r.ParseForm(); err != nil {
76
		http.Error(w, "Invalid form", http.StatusBadRequest)
77
		return
78
	}
79
80
	product_id, _ := strconv.Atoi(r.FormValue("product_id"))
81
	variant_id, _ := strconv.Atoi(r.FormValue("variant_id"))
82
	quantity, _ := strconv.Atoi(r.FormValue("quantity"))
83
84
	session_id := lib.Get_Session_ID(r)
85
	cart_json := h.ctx.Session_Store.Get_Cart(session_id)
86
	cart, _ := models.Parse_Cart(cart_json)
87
88
	if quantity == 0 {
89
		cart = models.Remove_From_Cart(cart, product_id, variant_id)
90
	} else {
91
		cart = models.Update_Cart_Quantity(cart, product_id, variant_id, quantity)
92
	}
93
94
	updated_cart_json, _ := models.Serialize_Cart(cart)
95
	h.ctx.Session_Store.Set_Cart(session_id, updated_cart_json)
96
97
	items, err := models.Get_Cart_Items_With_Details(h.ctx.DB, cart)
98
	if err != nil {
99
		http.Error(w, "Failed to load cart", http.StatusInternalServerError)
100
		return
101
	}
102
103
	if lib.Is_HTMX_Request(r) {
104
		views.Cart_Items(items).Render(r.Context(), w)
105
		return
106
	}
107
108
	lib.Redirect(w, r, "/cart")
109
}
110
111
func (h *Cart_Handler) Remove_Item(w http.ResponseWriter, r *http.Request) {
112
	if err := r.ParseForm(); err != nil {
113
		http.Error(w, "Invalid form", http.StatusBadRequest)
114
		return
115
	}
116
117
	product_id, _ := strconv.Atoi(r.FormValue("product_id"))
118
	variant_id, _ := strconv.Atoi(r.FormValue("variant_id"))
119
120
	session_id := lib.Get_Session_ID(r)
121
	cart_json := h.ctx.Session_Store.Get_Cart(session_id)
122
	cart, _ := models.Parse_Cart(cart_json)
123
124
	cart = models.Remove_From_Cart(cart, product_id, variant_id)
125
126
	updated_cart_json, _ := models.Serialize_Cart(cart)
127
	h.ctx.Session_Store.Set_Cart(session_id, updated_cart_json)
128
129
	items, err := models.Get_Cart_Items_With_Details(h.ctx.DB, cart)
130
	if err != nil {
131
		http.Error(w, "Failed to load cart", http.StatusInternalServerError)
132
		return
133
	}
134
135
	if lib.Is_HTMX_Request(r) {
136
		views.Cart_Items(items).Render(r.Context(), w)
137
		return
138
	}
139
140
	lib.Redirect(w, r, "/cart")
141
}