awesome-cli/back/internal/handler/pastry_handler.go

62 lines
1.1 KiB
Go
Raw Normal View History

2025-10-16 12:46:23 +03:00
package handler
import (
"strconv"
"awesome-back/internal/service"
"awesome-back/pkg/errors"
"github.com/gin-gonic/gin"
)
type PastryHandler struct {
pastryService *service.PastryService
}
func NewPastryHandler(pastryService *service.PastryService) *PastryHandler {
return &PastryHandler{pastryService: pastryService}
}
func (h *PastryHandler) GetPastries(c *gin.Context) {
pastries, err := h.pastryService.GetAllPastries()
if err != nil {
Error(c, err)
return
}
Success(c, pastries)
}
func (h *PastryHandler) GetPastryByID(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
Error(c, errors.NewInvalidError("invalid pastry ID format"))
return
}
pastry, err := h.pastryService.GetPastryByID(id)
if err != nil {
Error(c, err)
return
}
Success(c, pastry)
}
func (h *PastryHandler) GetPastriesByCategory(c *gin.Context) {
category := c.Param("category")
if category == "" {
Error(c, errors.NewInvalidError("category parameter is required"))
return
}
pastries, err := h.pastryService.GetPastriesByCategory(category)
if err != nil {
Error(c, err)
return
}
Success(c, pastries)
}