niki/productapp/delivery/http/handler.go

39 lines
905 B
Go

package http
import (
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
"git.gocasts.ir/ebhomengo/niki/productapp/service/product"
"github.com/labstack/echo/v4"
"log/slog"
"net/http"
)
type Handler struct {
productService product.Service
Logger *slog.Logger
}
func NewHandler(productService product.Service) *Handler {
return &Handler{
productService: productService,
}
}
func (h Handler) getProductList(c echo.Context) error {
var req product.GetProductListRequest
if err := c.Bind(&req); err != nil {
return c.JSON(http.StatusBadRequest, errmsg.ErrorResponse{
Message: errmsg.ErrInvalidRequestFormat,
})
}
response, err := h.productService.GetProducts(c.Request().Context(), req)
if err != nil {
// todo handle validation error
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
return c.JSON(http.StatusOK, response)
}