package restricted import ( "strconv" "git.ma-al.com/goc_daniel/b2b/app/config" "git.ma-al.com/goc_daniel/b2b/app/delivery/middleware" "git.ma-al.com/goc_daniel/b2b/app/delivery/middleware/perms" "git.ma-al.com/goc_daniel/b2b/app/model" "git.ma-al.com/goc_daniel/b2b/app/service/specificPriceService" "git.ma-al.com/goc_daniel/b2b/app/utils/i18n" "git.ma-al.com/goc_daniel/b2b/app/utils/logger" "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 SpecificPriceHandler struct { SpecificPriceService *specificPriceService.SpecificPriceService config *config.Config } func NewSpecificPriceHandler() *SpecificPriceHandler { SpecificPriceService := specificPriceService.New() return &SpecificPriceHandler{ SpecificPriceService: SpecificPriceService, config: config.Get(), } } func SpecificPriceHandlerRoutes(r fiber.Router) fiber.Router { handler := NewSpecificPriceHandler() r.Post("/", middleware.Require(perms.SpecificPriceManage), handler.Create) r.Put("/:id", middleware.Require(perms.SpecificPriceManage), handler.Update) r.Delete("/:id", middleware.Require(perms.SpecificPriceManage), handler.Delete) r.Get("/", middleware.Require(perms.SpecificPriceManage), handler.List) r.Get("/:id", middleware.Require(perms.SpecificPriceManage), handler.GetByID) r.Patch("/:id/activate", middleware.Require(perms.SpecificPriceManage), handler.Activate) r.Patch("/:id/deactivate", middleware.Require(perms.SpecificPriceManage), handler.Deactivate) return r } func (h *SpecificPriceHandler) Create(c fiber.Ctx) error { var pr model.SpecificPrice if err := c.Bind().Body(&pr); err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } result, err := h.SpecificPriceService.Create(c.Context(), &pr) if err != nil { logger.Error("failed to create specific price", "handler", "SpecificPriceHandler.Create", "error", err.Error(), ) return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(&result, 1, i18n.T_(c, response.Message_OK))) } func (h *SpecificPriceHandler) Update(c fiber.Ctx) error { idStr := c.Params("id") id, err := strconv.ParseUint(idStr, 10, 64) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } var pr model.SpecificPrice if err := c.Bind().Body(&pr); err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } result, err := h.SpecificPriceService.Update(c.Context(), id, &pr) if err != nil { logger.Error("failed to update specific price", "handler", "SpecificPriceHandler.Update", "specific_price_id", 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(&result, 1, i18n.T_(c, response.Message_OK))) } func (h *SpecificPriceHandler) List(c fiber.Ctx) error { result, err := h.SpecificPriceService.List(c.Context()) if err != nil { logger.Error("failed to list specific prices", "handler", "SpecificPriceHandler.List", "error", err.Error(), ) return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(&result, 1, i18n.T_(c, response.Message_OK))) } func (h *SpecificPriceHandler) GetByID(c fiber.Ctx) error { idStr := c.Params("id") id, err := strconv.ParseUint(idStr, 10, 64) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } result, err := h.SpecificPriceService.GetByID(c.Context(), id) if err != nil { logger.Error("failed to get specific price", "handler", "SpecificPriceHandler.GetByID", "specific_price_id", 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(&result, 1, i18n.T_(c, response.Message_OK))) } func (h *SpecificPriceHandler) Activate(c fiber.Ctx) error { idStr := c.Params("id") id, err := strconv.ParseUint(idStr, 10, 64) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } err = h.SpecificPriceService.SetActive(c.Context(), id, true) if err != nil { logger.Error("failed to activate specific price", "handler", "SpecificPriceHandler.Activate", "specific_price_id", 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 *SpecificPriceHandler) Deactivate(c fiber.Ctx) error { idStr := c.Params("id") id, err := strconv.ParseUint(idStr, 10, 64) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } err = h.SpecificPriceService.SetActive(c.Context(), id, false) if err != nil { logger.Error("failed to deactivate specific price", "handler", "SpecificPriceHandler.Deactivate", "specific_price_id", 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 *SpecificPriceHandler) Delete(c fiber.Ctx) error { idStr := c.Params("id") id, err := strconv.ParseUint(idStr, 10, 64) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } err = h.SpecificPriceService.Delete(c.Context(), id) if err != nil { logger.Error("failed to delete specific price", "handler", "SpecificPriceHandler.Delete", "specific_price_id", 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))) }