package restricted import ( "strconv" "git.ma-al.com/goc_daniel/b2b/app/config" "git.ma-al.com/goc_daniel/b2b/app/model/dbmodel" "git.ma-al.com/goc_daniel/b2b/app/service/productService" constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data" "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/query/query_params" "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" ) type ProductsHandler struct { productService *productService.ProductService config *config.Config } // NewListProductsHandler creates a new ListProductsHandler instance func NewProductsHandler() *ProductsHandler { productService := productService.New() return &ProductsHandler{ productService: productService, config: config.Get(), } } func ProductsHandlerRoutes(r fiber.Router) fiber.Router { handler := NewProductsHandler() r.Get("/:id/:country_id/:quantity", handler.GetProductJson) r.Get("/list", handler.ListProducts) r.Get("/list-variants/:product_id", handler.ListProductVariants) r.Post("/favorite/:product_id", handler.AddToFavorites) r.Delete("/favorite/:product_id", handler.RemoveFromFavorites) return r } func (h *ProductsHandler) GetProductJson(c fiber.Ctx) error { idStr := c.Params("id") p_id_product, err := strconv.Atoi(idStr) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } country_idStr := c.Params("country_id") b2b_id_country, err := strconv.Atoi(country_idStr) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } quantityStr := c.Params("quantity") p_quantity, err := strconv.Atoi(quantityStr) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } customer, ok := localeExtractor.GetCustomer(c) if !ok || customer == nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } productJson, err := h.productService.Get(uint(p_id_product), customer.LangID, customer.ID, uint(b2b_id_country), uint(p_quantity)) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(&productJson, 1, i18n.T_(c, response.Message_OK))) } func (h *ProductsHandler) ListProducts(c fiber.Ctx) error { paging, filters, err := query_params.ParseFilters[dbmodel.PsProduct](c, columnMappingListProducts) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } customer, ok := localeExtractor.GetCustomer(c) if !ok || customer == nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } list, err := h.productService.Find(customer.LangID, customer.ID, paging, filters, customer, constdata.DEFAULT_PRODUCT_QUANTITY, constdata.SHOP_ID) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(&list.Items, int(list.Count), i18n.T_(c, response.Message_OK))) } // These are all the filterable fields var columnMappingListProducts map[string]string = map[string]string{ "product_id": "bp.product_id", "name": "bp.name", "reference": "bp.reference", "category_id": "bp.category_id", "quantity": "bp.quantity", "is_favorite": "bp.is_favorite", "is_new": "bp.is_new", } func (h *ProductsHandler) AddToFavorites(c fiber.Ctx) error { productIDStr := c.Params("product_id") productID, err := strconv.Atoi(productIDStr) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } 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))) } err = h.productService.AddToFavorites(userID, uint(productID)) if err != nil { 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 *ProductsHandler) RemoveFromFavorites(c fiber.Ctx) error { productIDStr := c.Params("product_id") productID, err := strconv.Atoi(productIDStr) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } 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))) } err = h.productService.RemoveFromFavorites(userID, uint(productID)) if err != nil { 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 *ProductsHandler) ListProductVariants(c fiber.Ctx) error { productIDStr := c.Params("product_id") productID, err := strconv.Atoi(productIDStr) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } customer, ok := localeExtractor.GetCustomer(c) if !ok || customer == nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } list, err := h.productService.GetProductAttributes(customer.LangID, uint(productID), constdata.SHOP_ID, customer.ID, customer.CountryID, constdata.DEFAULT_PRODUCT_QUANTITY) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(&list, len(list), i18n.T_(c, response.Message_OK))) }