package handlers import ( "log" "net/http" "shop/internal/models" "shop/internal/repository" ) type HomeHandler struct { products *repository.ProductRepository pages *Pages } func NewHomeHandler(products *repository.ProductRepository, pages *Pages) *HomeHandler { return &HomeHandler{products: products, pages: pages} } type homePageData struct { Layout Products []models.Product Categories []string TotalItems int } func (h *HomeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) return } ctx := r.Context() featured, err := h.products.Featured(ctx, 8) if err != nil { log.Printf("featured products: %v", err) http.Error(w, "Ошибка загрузки каталога", http.StatusInternalServerError) return } categories, err := h.products.Categories(ctx) if err != nil { log.Printf("categories: %v", err) categories = nil } total, err := h.products.Count(ctx) if err != nil { log.Printf("count: %v", err) total = 0 } data := homePageData{ Layout: h.pages.layout(r, "Главная", "home"), Products: featured, Categories: categories, TotalItems: total, } data.Success = flashMsg(r, "ok") h.pages.render(w, "home.html", data) }