106 lines
1.8 KiB
Go
106 lines
1.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"platform/web/auth"
|
|
"platform/web/core"
|
|
g "platform/web/globals"
|
|
s "platform/web/services"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func PageDiscountByAdmin(c *fiber.Ctx) error {
|
|
_, err := auth.GetAuthCtx(c).PermitAdmin(core.ScopeDiscountRead)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var req core.PageReq
|
|
if err := g.Validator.ParseBody(c, &req); err != nil {
|
|
return err
|
|
}
|
|
|
|
list, total, err := s.ProductDiscount.Page(&req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(core.PageResp{
|
|
Total: int(total),
|
|
Page: req.GetPage(),
|
|
Size: req.GetSize(),
|
|
List: list,
|
|
})
|
|
}
|
|
|
|
func AllDiscountByAdmin(c *fiber.Ctx) error {
|
|
_, err := auth.GetAuthCtx(c).PermitAdmin(core.ScopeDiscountRead)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
list, err := s.ProductDiscount.All()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(list)
|
|
}
|
|
|
|
func CreateDiscount(c *fiber.Ctx) error {
|
|
_, err := auth.GetAuthCtx(c).PermitAdmin(core.ScopeDiscountWrite)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var req s.CreateProductDiscountData
|
|
if err := g.Validator.ParseBody(c, &req); err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.ProductDiscount.Create(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func UpdateDiscount(c *fiber.Ctx) error {
|
|
_, err := auth.GetAuthCtx(c).PermitAdmin(core.ScopeDiscountWrite)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var req s.UpdateProductDiscountData
|
|
if err := g.Validator.ParseBody(c, &req); err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.ProductDiscount.Update(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func DeleteDiscount(c *fiber.Ctx) error {
|
|
_, err := auth.GetAuthCtx(c).PermitAdmin(core.ScopeDiscountWrite)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var req core.IdReq
|
|
if err := g.Validator.ParseBody(c, &req); err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.ProductDiscount.Delete(req.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|