修复店铺列表参数校验与企业权限
This commit is contained in:
@@ -3,27 +3,51 @@ package admin
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
shopService "github.com/break/junhong_cmp_fiber/internal/service/shop"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/logger"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
)
|
||||
|
||||
// ShopHandler 店铺管理处理器。
|
||||
type ShopHandler struct {
|
||||
service *shopService.Service
|
||||
service *shopService.Service
|
||||
validator *validator.Validate
|
||||
}
|
||||
|
||||
func NewShopHandler(service *shopService.Service) *ShopHandler {
|
||||
return &ShopHandler{service: service}
|
||||
// NewShopHandler 创建店铺管理处理器。
|
||||
func NewShopHandler(service *shopService.Service, validate *validator.Validate) *ShopHandler {
|
||||
return &ShopHandler{service: service, validator: validate}
|
||||
}
|
||||
|
||||
// List 查询店铺列表。
|
||||
// GET /api/admin/shops
|
||||
func (h *ShopHandler) List(c *fiber.Ctx) error {
|
||||
var req dto.ShopListRequest
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
h.logListValidationFailure(c, err)
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
if hasExplicitZeroPagination(c, &req) {
|
||||
err := errors.New(errors.CodeInvalidParam)
|
||||
h.logListValidationFailure(c, err)
|
||||
return err
|
||||
}
|
||||
if h.validator == nil {
|
||||
return errors.New(errors.CodeInternalError, "店铺列表校验器未配置")
|
||||
}
|
||||
if err := h.validator.Struct(&req); err != nil {
|
||||
h.logListValidationFailure(c, err)
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
|
||||
normalizeShopListPagination(&req)
|
||||
|
||||
shops, total, err := h.service.ListShopResponses(c.UserContext(), &req)
|
||||
if err != nil {
|
||||
@@ -33,6 +57,31 @@ func (h *ShopHandler) List(c *fiber.Ctx) error {
|
||||
return response.SuccessWithPagination(c, shops, total, req.Page, req.PageSize)
|
||||
}
|
||||
|
||||
func hasExplicitZeroPagination(c *fiber.Ctx, req *dto.ShopListRequest) bool {
|
||||
queryArgs := c.Context().QueryArgs()
|
||||
return queryArgs.Has("page") && req.Page == 0 || queryArgs.Has("page_size") && req.PageSize == 0
|
||||
}
|
||||
|
||||
func (h *ShopHandler) logListValidationFailure(c *fiber.Ctx, err error) {
|
||||
logger.GetAppLogger().Warn("店铺列表参数验证失败",
|
||||
zap.String("method", c.Method()),
|
||||
zap.String("path", c.Path()),
|
||||
zap.String("query", c.Context().QueryArgs().String()),
|
||||
zap.Error(err),
|
||||
)
|
||||
}
|
||||
|
||||
func normalizeShopListPagination(req *dto.ShopListRequest) {
|
||||
if req.Page == 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = constants.DefaultPageSize
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建店铺。
|
||||
// POST /api/admin/shops
|
||||
func (h *ShopHandler) Create(c *fiber.Ctx) error {
|
||||
var req dto.CreateShopRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
@@ -47,6 +96,8 @@ func (h *ShopHandler) Create(c *fiber.Ctx) error {
|
||||
return response.Success(c, shop)
|
||||
}
|
||||
|
||||
// Update 更新店铺。
|
||||
// PUT /api/admin/shops/:id
|
||||
func (h *ShopHandler) Update(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil {
|
||||
@@ -66,6 +117,8 @@ func (h *ShopHandler) Update(c *fiber.Ctx) error {
|
||||
return response.Success(c, shop)
|
||||
}
|
||||
|
||||
// Delete 删除店铺。
|
||||
// DELETE /api/admin/shops/:id
|
||||
func (h *ShopHandler) Delete(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user