160 lines
5.9 KiB
Go
160 lines
5.9 KiB
Go
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/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"
|
|
)
|
|
|
|
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("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
|
|
}
|
|
|
|
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 {
|
|
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 {
|
|
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 {
|
|
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 {
|
|
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 {
|
|
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 {
|
|
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 {
|
|
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)))
|
|
}
|