package handlers import ( "net/http" "strconv" "shop.tonybtw.com/internal/lib" "shop.tonybtw.com/internal/models" "shop.tonybtw.com/internal/views" ) type Cart_Handler struct { ctx *lib.App_Context } func New_Cart_Handler(ctx *lib.App_Context) *Cart_Handler { return &Cart_Handler{ctx: ctx} } func (h *Cart_Handler) Show_Cart(w http.ResponseWriter, r *http.Request) { session_id := lib.Get_Session_ID(r) cart_json := h.ctx.Session_Store.Get_Cart(session_id) cart, _ := models.Parse_Cart(cart_json) items, err := models.Get_Cart_Items_With_Details(h.ctx.DB, cart) if err != nil { http.Error(w, "Failed to load cart", http.StatusInternalServerError) return } total := models.Get_Cart_Total(items) cart_count := models.Count_Cart_Items(cart) views.Cart(items, total, cart_count).Render(r.Context(), w) } func (h *Cart_Handler) Add_Item(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { http.Error(w, "Invalid form", http.StatusBadRequest) return } product_id, _ := strconv.Atoi(r.FormValue("product_id")) variant_id, _ := strconv.Atoi(r.FormValue("variant_id")) quantity, _ := strconv.Atoi(r.FormValue("quantity")) if quantity < 1 { quantity = 1 } session_id := lib.Get_Or_Create_Session_ID(w, r) cart_json := h.ctx.Session_Store.Get_Cart(session_id) cart, _ := models.Parse_Cart(cart_json) cart = models.Add_To_Cart(cart, product_id, variant_id, quantity) updated_cart_json, _ := models.Serialize_Cart(cart) h.ctx.Session_Store.Set_Cart(session_id, updated_cart_json) cart_count := models.Count_Cart_Items(cart) if lib.Is_HTMX_Request(r) { views.Cart_Widget(cart_count).Render(r.Context(), w) return } referer := r.Header.Get("Referer") if referer == "" { referer = "/" } lib.Redirect(w, r, referer) } func (h *Cart_Handler) Update_Item(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { http.Error(w, "Invalid form", http.StatusBadRequest) return } product_id, _ := strconv.Atoi(r.FormValue("product_id")) variant_id, _ := strconv.Atoi(r.FormValue("variant_id")) quantity, _ := strconv.Atoi(r.FormValue("quantity")) session_id := lib.Get_Session_ID(r) cart_json := h.ctx.Session_Store.Get_Cart(session_id) cart, _ := models.Parse_Cart(cart_json) if quantity == 0 { cart = models.Remove_From_Cart(cart, product_id, variant_id) } else { cart = models.Update_Cart_Quantity(cart, product_id, variant_id, quantity) } updated_cart_json, _ := models.Serialize_Cart(cart) h.ctx.Session_Store.Set_Cart(session_id, updated_cart_json) items, err := models.Get_Cart_Items_With_Details(h.ctx.DB, cart) if err != nil { http.Error(w, "Failed to load cart", http.StatusInternalServerError) return } if lib.Is_HTMX_Request(r) { views.Cart_Items(items).Render(r.Context(), w) return } lib.Redirect(w, r, "/cart") } func (h *Cart_Handler) Remove_Item(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { http.Error(w, "Invalid form", http.StatusBadRequest) return } product_id, _ := strconv.Atoi(r.FormValue("product_id")) variant_id, _ := strconv.Atoi(r.FormValue("variant_id")) session_id := lib.Get_Session_ID(r) cart_json := h.ctx.Session_Store.Get_Cart(session_id) cart, _ := models.Parse_Cart(cart_json) cart = models.Remove_From_Cart(cart, product_id, variant_id) updated_cart_json, _ := models.Serialize_Cart(cart) h.ctx.Session_Store.Set_Cart(session_id, updated_cart_json) items, err := models.Get_Cart_Items_With_Details(h.ctx.DB, cart) if err != nil { http.Error(w, "Failed to load cart", http.StatusInternalServerError) return } if lib.Is_HTMX_Request(r) { views.Cart_Items(items).Render(r.Context(), w) return } lib.Redirect(w, r, "/cart") }