forked from ebhomengo/niki
118 lines
2.7 KiB
Go
118 lines
2.7 KiB
Go
package http
|
|
|
|
import (
|
|
"git.gocasts.ir/ebhomengo/niki/pkg/claim"
|
|
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
|
"git.gocasts.ir/ebhomengo/niki/shoppingbasketapp/service/cart"
|
|
"git.gocasts.ir/ebhomengo/niki/types"
|
|
"github.com/labstack/echo/v4"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type Handler struct {
|
|
svc cart.Service
|
|
}
|
|
|
|
func NewHandler(svc cart.Service) Handler {
|
|
return Handler{svc: svc}
|
|
}
|
|
|
|
func (h Handler) addToBasket(c echo.Context) error {
|
|
claims := claim.GetClaimsFromEchoContext(c)
|
|
|
|
var req cart.AddToCartRequest
|
|
if err := c.Bind(&req); err != nil {
|
|
return c.JSON(http.StatusBadRequest, map[string]string{
|
|
"error": "invalid request body",
|
|
})
|
|
}
|
|
|
|
req.UserID = types.ID(claims.UserID)
|
|
if err := h.svc.AddToBasket(c.Request().Context(), req); err != nil {
|
|
msg, code := httpmsg.Error(err)
|
|
return c.JSON(code, msg)
|
|
}
|
|
|
|
return c.NoContent(http.StatusNoContent)
|
|
}
|
|
|
|
func (h Handler) getCart(c echo.Context) error {
|
|
claims := claim.GetClaimsFromEchoContext(c)
|
|
|
|
res, err := h.svc.GetCart(c.Request().Context(), types.ID(claims.UserID))
|
|
if err != nil {
|
|
msg, code := httpmsg.Error(err)
|
|
return c.JSON(code, msg)
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (h Handler) removeCart(c echo.Context) error {
|
|
claims := claim.GetClaimsFromEchoContext(c)
|
|
|
|
if err := h.svc.ClearCart(c.Request().Context(), types.ID(claims.UserID)); err != nil {
|
|
msg, code := httpmsg.Error(err)
|
|
return c.JSON(code, msg)
|
|
}
|
|
|
|
return c.NoContent(http.StatusNoContent)
|
|
}
|
|
|
|
func (h Handler) removeItem(c echo.Context) error {
|
|
claims := claim.GetClaimsFromEchoContext(c)
|
|
p := c.Param("productID")
|
|
|
|
pID, err := strconv.Atoi(p)
|
|
if err != nil {
|
|
return c.JSON(http.StatusBadRequest, map[string]string{
|
|
"error": "invalid product id",
|
|
})
|
|
}
|
|
|
|
var req cart.RemoveFromCartRequest
|
|
|
|
req.UserID = types.ID(claims.UserID)
|
|
req.ProductID = types.ID(pID)
|
|
|
|
if err := h.svc.RemoveFromCart(c.Request().Context(), req); err != nil {
|
|
msg, code := httpmsg.Error(err)
|
|
return c.JSON(code, msg)
|
|
}
|
|
|
|
return c.NoContent(http.StatusNoContent)
|
|
}
|
|
|
|
func (h Handler) updateQuantity(c echo.Context) error {
|
|
claims := claim.GetClaimsFromEchoContext(c)
|
|
p := c.Param("productID")
|
|
|
|
pID, err := strconv.Atoi(p)
|
|
if err != nil {
|
|
return c.JSON(http.StatusBadRequest, map[string]string{
|
|
"error": "invalid product id",
|
|
})
|
|
}
|
|
|
|
qStr := c.Param("quantity")
|
|
q, err := strconv.Atoi(qStr)
|
|
if err != nil {
|
|
return c.JSON(http.StatusBadRequest, map[string]string{
|
|
"error": "invalid quantity",
|
|
})
|
|
}
|
|
|
|
var req cart.UpdateQuantityRequest
|
|
req.UserID = types.ID(claims.UserID)
|
|
req.ProductID = types.ID(pID)
|
|
req.Quantity = q
|
|
|
|
if err := h.svc.UpdateQuantity(c.Request().Context(), req); err != nil {
|
|
msg, code := httpmsg.Error(err)
|
|
return c.JSON(code, msg)
|
|
}
|
|
|
|
return c.NoContent(http.StatusNoContent)
|
|
}
|