修复: GET /v1/admin/promo/price/list 字段不匹配导致 400
前端发 rule_id/subscribe_id (均可选), 后端 API 定义为 promo_rule_id (required), 直接返回 "PromoRuleId is a required field"。 对齐前端约定 (与 usage/list 命名一致): - API: promo_rule_id(required) → rule_id + subscribe_id, 均可选 - model.QueryPriceList: 改为接 PriceFilter, 按条件过滤 - 同步 fake mock 与校验测试签名
This commit is contained in:
@@ -52,9 +52,10 @@ type (
|
|||||||
Items []PromoPriceItem `json:"items" validate:"required,min=1,dive"`
|
Items []PromoPriceItem `json:"items" validate:"required,min=1,dive"`
|
||||||
}
|
}
|
||||||
GetPromoPriceListRequest {
|
GetPromoPriceListRequest {
|
||||||
PromoRuleId int64 `form:"promo_rule_id" validate:"required,gt=0"`
|
|
||||||
Page int64 `form:"page" validate:"required,gt=0"`
|
Page int64 `form:"page" validate:"required,gt=0"`
|
||||||
Size int64 `form:"size" validate:"required,gt=0,lte=200"`
|
Size int64 `form:"size" validate:"required,gt=0,lte=200"`
|
||||||
|
RuleId int64 `form:"rule_id,omitempty"`
|
||||||
|
SubscribeId int64 `form:"subscribe_id,omitempty"`
|
||||||
}
|
}
|
||||||
GetPromoPriceListResponse {
|
GetPromoPriceListResponse {
|
||||||
Total int64 `json:"total"`
|
Total int64 `json:"total"`
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ func (fakePromoModel) DeletePrice(context.Context, int64) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fakePromoModel) QueryPriceList(context.Context, int64, int, int) (int64, []*promomodel.SubscribePromo, error) {
|
func (fakePromoModel) QueryPriceList(context.Context, promomodel.PriceFilter) (int64, []*promomodel.SubscribePromo, error) {
|
||||||
return 0, nil, nil
|
return 0, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package promo
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
promomodel "github.com/perfect-panel/server/internal/model/promo"
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
"github.com/perfect-panel/server/internal/svc"
|
||||||
"github.com/perfect-panel/server/internal/types"
|
"github.com/perfect-panel/server/internal/types"
|
||||||
"github.com/perfect-panel/server/pkg/logger"
|
"github.com/perfect-panel/server/pkg/logger"
|
||||||
@@ -25,7 +26,12 @@ func NewGetPriceListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetP
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *GetPriceListLogic) GetPriceList(req *types.GetPromoPriceListRequest) (*types.GetPromoPriceListResponse, error) {
|
func (l *GetPriceListLogic) GetPriceList(req *types.GetPromoPriceListRequest) (*types.GetPromoPriceListResponse, error) {
|
||||||
total, list, err := l.svcCtx.PromoModel.QueryPriceList(l.ctx, req.PromoRuleId, int(req.Page), int(req.Size))
|
total, list, err := l.svcCtx.PromoModel.QueryPriceList(l.ctx, promomodel.PriceFilter{
|
||||||
|
Page: int(req.Page),
|
||||||
|
Size: int(req.Size),
|
||||||
|
RuleId: req.RuleId,
|
||||||
|
SubscribeId: req.SubscribeId,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Errorw("[GetPromoPriceList] Database Error", logger.Field("error", err.Error()))
|
l.Errorw("[GetPromoPriceList] Database Error", logger.Field("error", err.Error()))
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "get promo price list error: %v", err.Error())
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "get promo price list error: %v", err.Error())
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ func (m *fakePromoModel) DeletePrice(context.Context, int64) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *fakePromoModel) QueryPriceList(context.Context, int64, int, int) (int64, []*promo.SubscribePromo, error) {
|
func (m *fakePromoModel) QueryPriceList(context.Context, promo.PriceFilter) (int64, []*promo.SubscribePromo, error) {
|
||||||
return 0, nil, nil
|
return 0, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ type Model interface {
|
|||||||
UpsertPrices(ctx context.Context, ruleId int64, items []*SubscribePromo) error
|
UpsertPrices(ctx context.Context, ruleId int64, items []*SubscribePromo) error
|
||||||
FindPrice(ctx context.Context, id int64) (*SubscribePromo, error)
|
FindPrice(ctx context.Context, id int64) (*SubscribePromo, error)
|
||||||
DeletePrice(ctx context.Context, id int64) error
|
DeletePrice(ctx context.Context, id int64) error
|
||||||
QueryPriceList(ctx context.Context, ruleId int64, page, size int) (int64, []*SubscribePromo, error)
|
QueryPriceList(ctx context.Context, params PriceFilter) (int64, []*SubscribePromo, error)
|
||||||
QueryUsageList(ctx context.Context, params UsageFilter) (int64, []*Usage, error)
|
QueryUsageList(ctx context.Context, params UsageFilter) (int64, []*Usage, error)
|
||||||
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
||||||
}
|
}
|
||||||
@@ -38,6 +38,13 @@ type UsageFilter struct {
|
|||||||
OrderNo string
|
OrderNo string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PriceFilter struct {
|
||||||
|
Page int
|
||||||
|
Size int
|
||||||
|
RuleId int64
|
||||||
|
SubscribeId int64
|
||||||
|
}
|
||||||
|
|
||||||
type defaultPromoModel struct {
|
type defaultPromoModel struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
}
|
}
|
||||||
@@ -164,20 +171,26 @@ func (m *defaultPromoModel) DeletePrice(ctx context.Context, id int64) error {
|
|||||||
return m.db.WithContext(ctx).Delete(&SubscribePromo{}, id).Error
|
return m.db.WithContext(ctx).Delete(&SubscribePromo{}, id).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *defaultPromoModel) QueryPriceList(ctx context.Context, ruleId int64, page, size int) (int64, []*SubscribePromo, error) {
|
func (m *defaultPromoModel) QueryPriceList(ctx context.Context, params PriceFilter) (int64, []*SubscribePromo, error) {
|
||||||
if page <= 0 {
|
if params.Page <= 0 {
|
||||||
page = 1
|
params.Page = 1
|
||||||
}
|
}
|
||||||
if size <= 0 {
|
if params.Size <= 0 {
|
||||||
size = 10
|
params.Size = 10
|
||||||
}
|
}
|
||||||
var total int64
|
var total int64
|
||||||
var list []*SubscribePromo
|
var list []*SubscribePromo
|
||||||
db := m.db.WithContext(ctx).Model(&SubscribePromo{}).Where("promo_rule_id = ?", ruleId)
|
db := m.db.WithContext(ctx).Model(&SubscribePromo{})
|
||||||
|
if params.RuleId > 0 {
|
||||||
|
db = db.Where("promo_rule_id = ?", params.RuleId)
|
||||||
|
}
|
||||||
|
if params.SubscribeId > 0 {
|
||||||
|
db = db.Where("subscribe_id = ?", params.SubscribeId)
|
||||||
|
}
|
||||||
if err := db.Count(&total).Error; err != nil {
|
if err := db.Count(&total).Error; err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, err
|
||||||
}
|
}
|
||||||
err := db.Order("id DESC").Limit(size).Offset((page - 1) * size).Find(&list).Error
|
err := db.Order("id DESC").Limit(params.Size).Offset((params.Page - 1) * params.Size).Find(&list).Error
|
||||||
return total, list, err
|
return total, list, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,9 +34,9 @@ func TestPromoListPageSizeLimit(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "price list",
|
name: "price list",
|
||||||
req: GetPromoPriceListRequest{
|
req: GetPromoPriceListRequest{
|
||||||
PromoRuleId: 1,
|
RuleId: 1,
|
||||||
Page: 1,
|
Page: 1,
|
||||||
Size: 201,
|
Size: 201,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1436,9 +1436,10 @@ type GetPreSendEmailCountResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GetPromoPriceListRequest struct {
|
type GetPromoPriceListRequest struct {
|
||||||
PromoRuleId int64 `form:"promo_rule_id" validate:"required,gt=0"`
|
|
||||||
Page int64 `form:"page" validate:"required,gt=0"`
|
Page int64 `form:"page" validate:"required,gt=0"`
|
||||||
Size int64 `form:"size" validate:"required,gt=0,lte=200"`
|
Size int64 `form:"size" validate:"required,gt=0,lte=200"`
|
||||||
|
RuleId int64 `form:"rule_id,omitempty"`
|
||||||
|
SubscribeId int64 `form:"subscribe_id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetPromoPriceListResponse struct {
|
type GetPromoPriceListResponse struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user