package handlers import ( "net/http" "shop.tonybtw.com/internal/lib" "shop.tonybtw.com/internal/models" "shop.tonybtw.com/internal/views" ) type Shop_Handler struct { ctx *lib.App_Context } func New_Shop_Handler(ctx *lib.App_Context) *Shop_Handler { return &Shop_Handler{ctx: ctx} } func (h *Shop_Handler) Show_Home(w http.ResponseWriter, r *http.Request) { products, err := models.Get_All_Products(h.ctx.DB) if err != nil { http.Error(w, "Failed to load products", http.StatusInternalServerError) return } session_id := lib.Get_Session_ID(r) cart_json := h.ctx.Session_Store.Get_Cart(session_id) cart, _ := models.Parse_Cart(cart_json) cart_count := models.Count_Cart_Items(cart) views.Home(products, cart_count).Render(r.Context(), w) } func (h *Shop_Handler) Show_Product(w http.ResponseWriter, r *http.Request, slug string) { product, err := models.Get_Product_By_Slug(h.ctx.DB, slug) if err != nil { http.Error(w, "Database error", http.StatusInternalServerError) return } if product == nil { session_id := lib.Get_Session_ID(r) cart_json := h.ctx.Session_Store.Get_Cart(session_id) cart, _ := models.Parse_Cart(cart_json) cart_count := models.Count_Cart_Items(cart) views.Error("Product not found", cart_count).Render(r.Context(), w) w.WriteHeader(http.StatusNotFound) return } variants, err := models.Get_Product_Variants(h.ctx.DB, product.ID) if err != nil { http.Error(w, "Failed to load variants: "+err.Error(), http.StatusInternalServerError) return } images, err := models.Get_Product_Images(h.ctx.DB, product.ID) if err != nil { http.Error(w, "Failed to load images: "+err.Error(), http.StatusInternalServerError) return } session_id := lib.Get_Session_ID(r) cart_json := h.ctx.Session_Store.Get_Cart(session_id) cart, _ := models.Parse_Cart(cart_json) cart_count := models.Count_Cart_Items(cart) views.Product(*product, variants, images, cart_count).Render(r.Context(), w) }