2025-10-16 12:55:36 +03:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
2025-10-16 12:57:19 +03:00
|
|
|
"code.linberg.su/linberg/awesome-back/internal/service"
|
|
|
|
"code.linberg.su/linberg/awesome-back/pkg/errors"
|
2025-10-16 12:55:36 +03:00
|
|
|
|
|
|
|
"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)
|
|
|
|
}
|