Commit 7aeb7fbb authored by qiuqunfeng's avatar qiuqunfeng
Browse files

Enhance GetBlackWhiteLists method to return total count of items

- Update the GetBlackWhiteLists method in both the service and controller layers to return the total number of items alongside the list of MatcherExpr.
- Modify response structure in the controller to include total items for improved client-side handling.
- Ensure proper error handling and maintain existing functionality while enhancing data retrieval capabilities.
parent 5a77e771
......@@ -483,10 +483,14 @@ func (c *WafController) GetBlackWhiteLists(ctx *gin.Context) {
if expr != "" {
query.WithFuzzyExpr(expr)
}
lists, err := c.service.GetBlackWhiteLists(ctx1, query, limit, offset)
lists, total, err := c.service.GetBlackWhiteLists(ctx1, query, limit, offset)
if err != nil {
utils.AssembleResponse(ctx, nil, err)
return
}
utils.AssembleResponse(ctx, lists, nil)
type resp struct {
Items []service.MatcherExpr `json:"items"`
Total int `json:"totalItems"`
}
utils.AssembleResponse(ctx, resp{Items: lists, Total: total}, nil)
}
......@@ -22,5 +22,5 @@ type Service interface {
UpdateBlackWhiteList(ctx context.Context, req *MatcherExpr) error
EnableBlackWhiteList(ctx context.Context, req *MatcherExpr) error
DeleteBlackWhiteList(ctx context.Context, ID uint32) error
GetBlackWhiteLists(ctx context.Context, query *MatchExprQueryOption, limit int, offset int) ([]MatcherExpr, error)
GetBlackWhiteLists(ctx context.Context, query *MatchExprQueryOption, limit int, offset int) ([]MatcherExpr, int, error)
}
......@@ -1210,7 +1210,7 @@ func GetLikeExpr(s string) string {
return sb.String()
}
func (s *wafService) GetBlackWhiteLists(ctx context.Context, query *MatchExprQueryOption, limit int, offset int) ([]MatcherExpr, error) {
func (s *wafService) GetBlackWhiteLists(ctx context.Context, query *MatchExprQueryOption, limit int, offset int) ([]MatcherExpr, int, error) {
oneCtx, oneCancel := context.WithTimeout(ctx, 750*time.Millisecond)
defer oneCancel()
......@@ -1226,12 +1226,18 @@ func (s *wafService) GetBlackWhiteLists(ctx context.Context, query *MatchExprQue
for column, val := range query.whereInCondition {
db = db.Where(fmt.Sprintf("%s in ?", column), val)
}
var total int64
err := db.Count(&total).Error
if err != nil {
return nil, 0, err
}
if limit > 0 && offset >= 0 {
db = db.Offset(offset).Limit(limit)
}
err := db.Order("updated_at DESC").Find(&exprs).Error
err = db.Order("updated_at DESC").Find(&exprs).Error
if err != nil {
return nil, err
return nil, 0, err
}
exprsResp := []MatcherExpr{}
for _, expr := range exprs {
......@@ -1245,5 +1251,5 @@ func (s *wafService) GetBlackWhiteLists(ctx context.Context, query *MatchExprQue
Global: expr.Global,
})
}
return exprsResp, nil
return exprsResp, int(total), nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment