feat: 重构内容列表接口,使用过滤器结构体简化参数传递;更新相关服务和测试用例

This commit is contained in:
2025-12-29 15:04:55 +08:00
parent 8c4bc55f45
commit fba77afec1
14 changed files with 65 additions and 60 deletions

View File

@@ -18,19 +18,19 @@ import (
// @provider
type content struct{}
func (s *content) List(ctx context.Context, keyword, genre, tenantId, sort string, page int) (*requests.Pager, error) {
func (s *content) List(ctx context.Context, filter *content_dto.ContentListFilter) (*requests.Pager, error) {
tbl, q := models.ContentQuery.QueryContext(ctx)
// Filters
q = q.Where(tbl.Status.Eq(consts.ContentStatusPublished))
if keyword != "" {
q = q.Where(tbl.Title.Like("%" + keyword + "%"))
if filter.Keyword != "" {
q = q.Where(tbl.Title.Like("%" + filter.Keyword + "%"))
}
if genre != "" {
q = q.Where(tbl.Genre.Eq(genre))
if filter.Genre != "" {
q = q.Where(tbl.Genre.Eq(filter.Genre))
}
if tenantId != "" {
tid := cast.ToInt64(tenantId)
if filter.TenantID != "" {
tid := cast.ToInt64(filter.TenantID)
q = q.Where(tbl.TenantID.Eq(tid))
}
@@ -38,7 +38,7 @@ func (s *content) List(ctx context.Context, keyword, genre, tenantId, sort strin
q = q.Preload(tbl.Author)
// Sort
switch sort {
switch filter.Sort {
case "hot":
q = q.Order(tbl.Views.Desc())
case "price_asc":
@@ -48,13 +48,14 @@ func (s *content) List(ctx context.Context, keyword, genre, tenantId, sort strin
}
// Pagination
p := requests.Pagination{Page: int64(page), Limit: 10}
// Use embedded pagination directly
filter.Pagination.Format()
total, err := q.Count()
if err != nil {
return nil, errorx.ErrDatabaseError.WithCause(err)
}
list, err := q.Offset(int(p.Offset())).Limit(int(p.Limit)).Find()
list, err := q.Offset(int(filter.Pagination.Offset())).Limit(int(filter.Pagination.Limit)).Find()
if err != nil {
return nil, errorx.ErrDatabaseError.WithCause(err)
}
@@ -66,12 +67,9 @@ func (s *content) List(ctx context.Context, keyword, genre, tenantId, sort strin
}
return &requests.Pager{
Pagination: requests.Pagination{
Page: p.Page,
Limit: p.Limit,
},
Total: total,
Items: data,
Pagination: filter.Pagination,
Total: total,
Items: data,
}, nil
}