package restricted import ( "fmt" "strconv" "git.ma-al.com/goc_daniel/b2b/app/config" "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/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" ) 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) 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))) } p_id_customer, ok := c.Locals(constdata.USER_LOCALES_ID).(uint) if !ok { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } fmt.Printf("p_id_customer: %v\n", p_id_customer) id_lang, ok := c.Locals(constdata.LANG_LOCALES_ID).(uint) if !ok { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } productJson, err := h.productService.GetJSON(p_id_product, int(id_lang), int(p_id_customer), b2b_id_country, 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))) }