306 lines
10 KiB
Go
306 lines
10 KiB
Go
package restricted
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"git.ma-al.com/goc_daniel/b2b/app/internal/logger"
|
|
"git.ma-al.com/goc_daniel/b2b/app/service/cartsService"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/localeExtractor"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/nullable"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/response"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
|
|
"github.com/gofiber/fiber/v3"
|
|
)
|
|
|
|
// CartsHandler handles endpoints that modify carts.
|
|
type CartsHandler struct {
|
|
cartsService *cartsService.CartsService
|
|
}
|
|
|
|
// CartsHandler creates a new CartsHandler instance
|
|
func NewCartsHandler() *CartsHandler {
|
|
cartsService := cartsService.New()
|
|
return &CartsHandler{
|
|
cartsService: cartsService,
|
|
}
|
|
}
|
|
|
|
func CartsHandlerRoutes(r fiber.Router) fiber.Router {
|
|
handler := NewCartsHandler()
|
|
|
|
r.Get("/add-new-cart", handler.AddNewCart)
|
|
r.Delete("/remove-cart", handler.RemoveCart)
|
|
r.Get("/change-cart-name", handler.ChangeCartName)
|
|
r.Get("/retrieve-carts-info", handler.RetrieveCartsInfo)
|
|
r.Get("/retrieve-cart", handler.RetrieveCart)
|
|
r.Get("/add-product-to-cart", handler.AddProduct)
|
|
r.Delete("/remove-product-from-cart", handler.RemoveProduct)
|
|
|
|
return r
|
|
}
|
|
|
|
func (h *CartsHandler) AddNewCart(c fiber.Ctx) error {
|
|
userID, ok := localeExtractor.GetUserID(c)
|
|
if !ok {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody)))
|
|
}
|
|
|
|
name := c.Query("name")
|
|
new_cart, err := h.cartsService.CreateNewCart(userID, name)
|
|
if err != nil {
|
|
|
|
logger.Error("failed to create cart",
|
|
"handler", "CartsHandler.AddNewCart",
|
|
|
|
"user_id", userID,
|
|
"error", err.Error(),
|
|
)
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
}
|
|
|
|
return c.JSON(response.Make(&new_cart, 0, i18n.T_(c, response.Message_OK)))
|
|
}
|
|
|
|
func (h *CartsHandler) RemoveCart(c fiber.Ctx) error {
|
|
userID, ok := localeExtractor.GetUserID(c)
|
|
if !ok {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody)))
|
|
}
|
|
|
|
cart_id_attribute := c.Query("cart_id")
|
|
cart_id, err := strconv.Atoi(cart_id_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
err = h.cartsService.RemoveCart(userID, uint(cart_id))
|
|
if err != nil {
|
|
|
|
logger.Error("failed to remove cart",
|
|
"handler", "CartsHandler.RemoveCart",
|
|
|
|
"user_id", userID,
|
|
"cart_id", cart_id,
|
|
"error", err.Error(),
|
|
)
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
}
|
|
|
|
return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK)))
|
|
}
|
|
|
|
func (h *CartsHandler) ChangeCartName(c fiber.Ctx) error {
|
|
userID, ok := localeExtractor.GetUserID(c)
|
|
if !ok {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody)))
|
|
}
|
|
|
|
new_name := c.Query("new_name")
|
|
|
|
cart_id_attribute := c.Query("cart_id")
|
|
cart_id, err := strconv.Atoi(cart_id_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
err = h.cartsService.UpdateCartName(userID, uint(cart_id), new_name)
|
|
if err != nil {
|
|
|
|
logger.Error("failed to update cart name",
|
|
"handler", "CartsHandler.ChangeCartName",
|
|
|
|
"user_id", userID,
|
|
"cart_id", cart_id,
|
|
"error", err.Error(),
|
|
)
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
}
|
|
|
|
return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK)))
|
|
}
|
|
|
|
func (h *CartsHandler) RetrieveCartsInfo(c fiber.Ctx) error {
|
|
userID, ok := localeExtractor.GetUserID(c)
|
|
if !ok {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody)))
|
|
}
|
|
|
|
carts_info, err := h.cartsService.RetrieveCartsInfo(userID)
|
|
if err != nil {
|
|
|
|
logger.Error("failed to retrieve carts info",
|
|
"handler", "CartsHandler.RetrieveCartsInfo",
|
|
|
|
"user_id", userID,
|
|
"error", err.Error(),
|
|
)
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
}
|
|
|
|
return c.JSON(response.Make(&carts_info, 0, i18n.T_(c, response.Message_OK)))
|
|
}
|
|
|
|
func (h *CartsHandler) RetrieveCart(c fiber.Ctx) error {
|
|
userID, ok := localeExtractor.GetUserID(c)
|
|
if !ok {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody)))
|
|
}
|
|
|
|
cart_id_attribute := c.Query("cart_id")
|
|
cart_id, err := strconv.Atoi(cart_id_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
cart, err := h.cartsService.RetrieveCart(userID, uint(cart_id))
|
|
if err != nil {
|
|
|
|
logger.Error("failed to retrieve cart",
|
|
"handler", "CartsHandler.RetrieveCart",
|
|
|
|
"user_id", userID,
|
|
"cart_id", cart_id,
|
|
"error", err.Error(),
|
|
)
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
}
|
|
|
|
return c.JSON(response.Make(cart, 0, i18n.T_(c, response.Message_OK)))
|
|
}
|
|
|
|
// adds or sets given amount of products to the cart
|
|
func (h *CartsHandler) AddProduct(c fiber.Ctx) error {
|
|
userID, ok := localeExtractor.GetUserID(c)
|
|
if !ok {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody)))
|
|
}
|
|
|
|
cart_id_attribute := c.Query("cart_id")
|
|
cart_id, err := strconv.Atoi(cart_id_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
product_id_attribute := c.Query("product_id")
|
|
product_id, err := strconv.Atoi(product_id_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
product_attribute_id_attribute := c.Query("product_attribute_id")
|
|
var product_attribute_id *uint
|
|
if product_attribute_id_attribute == "" {
|
|
product_attribute_id = nil
|
|
} else {
|
|
val, err := strconv.Atoi(product_attribute_id_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
uval := uint(val)
|
|
product_attribute_id = &uval
|
|
}
|
|
|
|
amount_attribute := c.Query("amount")
|
|
amount, err := strconv.Atoi(amount_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
set_amount_attribute := c.Query("set_amount")
|
|
set_amount, err := strconv.ParseBool(set_amount_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
err = h.cartsService.AddProduct(userID, uint(cart_id), uint(product_id), product_attribute_id, amount, set_amount)
|
|
if err != nil {
|
|
|
|
logger.Error("failed to add product to cart",
|
|
"handler", "CartsHandler.AddProduct",
|
|
|
|
"user_id", userID,
|
|
"cart_id", cart_id,
|
|
"product_id", product_id,
|
|
"error", err.Error(),
|
|
)
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
}
|
|
|
|
return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK)))
|
|
}
|
|
|
|
// removes product from the cart.
|
|
func (h *CartsHandler) RemoveProduct(c fiber.Ctx) error {
|
|
userID, ok := localeExtractor.GetUserID(c)
|
|
if !ok {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody)))
|
|
}
|
|
|
|
cart_id_attribute := c.Query("cart_id")
|
|
cart_id, err := strconv.Atoi(cart_id_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
product_id_attribute := c.Query("product_id")
|
|
product_id, err := strconv.Atoi(product_id_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
product_attribute_id_attribute := c.Query("product_attribute_id")
|
|
var product_attribute_id *uint
|
|
if product_attribute_id_attribute == "" {
|
|
product_attribute_id = nil
|
|
} else {
|
|
val, err := strconv.Atoi(product_attribute_id_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
uval := uint(val)
|
|
product_attribute_id = &uval
|
|
}
|
|
|
|
err = h.cartsService.RemoveProduct(userID, uint(cart_id), uint(product_id), product_attribute_id)
|
|
if err != nil {
|
|
|
|
logger.Error("failed to remove product from cart",
|
|
"handler", "CartsHandler.RemoveProduct",
|
|
|
|
"user_id", userID,
|
|
"cart_id", cart_id,
|
|
"product_id", product_id,
|
|
"error", err.Error(),
|
|
)
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
}
|
|
|
|
return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK)))
|
|
}
|