|
|
|
|
@@ -2,11 +2,14 @@ package restricted
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"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/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/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"
|
|
|
|
|
)
|
|
|
|
|
@@ -27,13 +30,13 @@ func NewSpecificPriceHandler() *SpecificPriceHandler {
|
|
|
|
|
func SpecificPriceHandlerRoutes(r fiber.Router) fiber.Router {
|
|
|
|
|
handler := NewSpecificPriceHandler()
|
|
|
|
|
|
|
|
|
|
r.Post("/", handler.Create)
|
|
|
|
|
r.Put("/:id", handler.Update)
|
|
|
|
|
r.Delete("/:id", handler.Delete)
|
|
|
|
|
r.Get("/", handler.List)
|
|
|
|
|
r.Get("/:id", handler.GetByID)
|
|
|
|
|
r.Patch("/:id/activate", handler.Activate)
|
|
|
|
|
r.Patch("/:id/deactivate", handler.Deactivate)
|
|
|
|
|
r.Post("/", middleware.Require("specific_price.manage"), handler.Create)
|
|
|
|
|
r.Put("/:id", middleware.Require("specific_price.manage"), handler.Update)
|
|
|
|
|
r.Delete("/:id", middleware.Require("specific_price.manage"), handler.Delete)
|
|
|
|
|
r.Get("/", middleware.Require("specific_price.manage"), handler.List)
|
|
|
|
|
r.Get("/:id", middleware.Require("specific_price.manage"), handler.GetByID)
|
|
|
|
|
r.Patch("/:id/activate", middleware.Require("specific_price.manage"), handler.Activate)
|
|
|
|
|
r.Patch("/:id/deactivate", middleware.Require("specific_price.manage"), handler.Deactivate)
|
|
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
@@ -41,147 +44,116 @@ func SpecificPriceHandlerRoutes(r fiber.Router) fiber.Router {
|
|
|
|
|
func (h *SpecificPriceHandler) Create(c fiber.Ctx) error {
|
|
|
|
|
var pr model.SpecificPrice
|
|
|
|
|
if err := c.Bind().Body(&pr); err != nil {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody),
|
|
|
|
|
})
|
|
|
|
|
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 {
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, err),
|
|
|
|
|
})
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
|
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusCreated).JSON(result)
|
|
|
|
|
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(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody),
|
|
|
|
|
})
|
|
|
|
|
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(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody),
|
|
|
|
|
})
|
|
|
|
|
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 {
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, err),
|
|
|
|
|
})
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
|
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.JSON(result)
|
|
|
|
|
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 {
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, err),
|
|
|
|
|
})
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
|
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.JSON(result)
|
|
|
|
|
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(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody),
|
|
|
|
|
})
|
|
|
|
|
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 {
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, err),
|
|
|
|
|
})
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
|
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.JSON(result)
|
|
|
|
|
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(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody),
|
|
|
|
|
})
|
|
|
|
|
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 {
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, err),
|
|
|
|
|
})
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
|
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
|
"message": "price reduction activated",
|
|
|
|
|
})
|
|
|
|
|
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(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody),
|
|
|
|
|
})
|
|
|
|
|
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 {
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, err),
|
|
|
|
|
})
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
|
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
|
"message": "price reduction deactivated",
|
|
|
|
|
})
|
|
|
|
|
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(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody),
|
|
|
|
|
})
|
|
|
|
|
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 {
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).JSON(fiber.Map{
|
|
|
|
|
"error": responseErrors.GetErrorCode(c, err),
|
|
|
|
|
})
|
|
|
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
|
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
|
"message": "specific price deleted",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseTime(s *string) *time.Time {
|
|
|
|
|
if s == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
t, err := time.Parse(time.RFC3339, *s)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return &t
|
|
|
|
|
return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK)))
|
|
|
|
|
}
|
|
|
|
|
|