Skip to content

Commit 06b6ac9

Browse files
committed
feat: 添加分类名称过滤功能,更新文章列表查询逻辑以支持标签和分类的联合过滤
1 parent f409eaa commit 06b6ac9

File tree

5 files changed

+83
-31
lines changed

5 files changed

+83
-31
lines changed

internal/post/internal/domain/post.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ type PostDetail struct {
4444

4545
// Page 分页查询参数
4646
type Page struct {
47-
PageNo int64 // 页码,从1开始
48-
PageSize int64 // 每页大小
49-
Field string // 排序字段
50-
Order string // 排序方式:ASC/DESC
51-
Keyword string // 搜索关键词
52-
LabelName string // 标签名称,用于过滤LabelType为"tag"的标签
47+
PageNo int64 // 页码,从1开始
48+
PageSize int64 // 每页大小
49+
Field string // 排序字段
50+
Order string // 排序方式:ASC/DESC
51+
Keyword string // 搜索关键词
52+
LabelName string // 标签名称,用于过滤LabelType为"tag"的标签
53+
CategoryName string // 分类名称,用于过滤LabelType为"category"的分类
5354
}

internal/post/internal/repository/dao/post.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type IPostDao interface {
8888
GetDetailByID(ctx context.Context, id bson.ObjectID) (*PostCategoryTags, error)
8989
GetList(ctx context.Context, pagePipeline mongo.Pipeline, cond bson.D) ([]*PostCategoryTags, int64, error)
9090
GetListWithTagFilter(ctx context.Context, pagePipeline mongo.Pipeline, cond bson.D, hasTagFilter bool, labelName string) ([]*PostCategoryTags, int64, error)
91+
GetListWithFilter(ctx context.Context, pagePipeline mongo.Pipeline, cond bson.D, hasTagFilter bool, labelName string, hasCategoryFilter bool, categoryName string) ([]*PostCategoryTags, int64, error)
9192
GetAllPublishPost(ctx context.Context) ([]*Post, error)
9293
FindByAlias(ctx context.Context, alias string) (*Post, error)
9394
}
@@ -209,7 +210,7 @@ func (d *PostDao) GetList(ctx context.Context, pagePipeline mongo.Pipeline, cond
209210
}
210211

211212
// buildCountPipeline 构建计数管道
212-
func (d *PostDao) buildCountPipeline(cond bson.D, labelName string) mongo.Pipeline {
213+
func (d *PostDao) buildCountPipeline(cond bson.D, labelName string, categoryName string) mongo.Pipeline {
213214
stageBuilder := aggregation.NewStageBuilder().Match(cond).
214215
Lookup("label", "category", &aggregation.LookUpOptions{
215216
LocalField: "category_id",
@@ -231,6 +232,14 @@ func (d *PostDao) buildCountPipeline(cond bson.D, labelName string) mongo.Pipeli
231232
)))
232233
}
233234

235+
// 添加分类过滤条件
236+
if categoryName != "" {
237+
stageBuilder = stageBuilder.Match(query.And(
238+
query.Eq("category.type", "category"),
239+
query.Eq("category.name", categoryName),
240+
))
241+
}
242+
234243
return stageBuilder.Build()
235244
}
236245

@@ -247,7 +256,7 @@ func (d *PostDao) GetListWithTagFilter(ctx context.Context, pagePipeline mongo.P
247256
var count int64
248257
if hasTagFilter {
249258
// 有标签过滤时,使用聚合管道计数
250-
countPipeline := d.buildCountPipeline(cond, labelName)
259+
countPipeline := d.buildCountPipeline(cond, labelName, "")
251260
var countResult []bson.M
252261
err = d.coll.Aggregator().Pipeline(countPipeline).AggregateWithParse(ctx, &countResult)
253262
if err != nil {
@@ -265,6 +274,37 @@ func (d *PostDao) GetListWithTagFilter(ctx context.Context, pagePipeline mongo.P
265274
return utils.ValToPtrList(postResult), count, err
266275
}
267276

277+
// GetListWithFilter 获取文章列表(带标签和分类过滤)
278+
func (d *PostDao) GetListWithFilter(ctx context.Context, pagePipeline mongo.Pipeline, cond bson.D, hasTagFilter bool, labelName string, hasCategoryFilter bool, categoryName string) ([]*PostCategoryTags, int64, error) {
279+
// 执行分页查询
280+
var postResult []PostCategoryTags
281+
err := d.coll.Aggregator().Pipeline(pagePipeline).AggregateWithParse(ctx, &postResult)
282+
if err != nil {
283+
return nil, 0, err
284+
}
285+
286+
// 计算总数
287+
var count int64
288+
if hasTagFilter || hasCategoryFilter {
289+
// 有过滤条件时,使用聚合管道计数
290+
countPipeline := d.buildCountPipeline(cond, labelName, categoryName)
291+
var countResult []bson.M
292+
err = d.coll.Aggregator().Pipeline(countPipeline).AggregateWithParse(ctx, &countResult)
293+
if err != nil {
294+
return nil, 0, err
295+
}
296+
count = int64(len(countResult))
297+
} else {
298+
// 无过滤条件时,使用简单计数
299+
count, err = d.coll.Finder().Filter(cond).Count(ctx)
300+
if err != nil {
301+
return nil, 0, err
302+
}
303+
}
304+
305+
return utils.ValToPtrList(postResult), count, err
306+
}
307+
268308
// SoftDeleteBatch 批量软删除文章
269309
func (d *PostDao) SoftDeleteBatch(ctx context.Context, ids []bson.ObjectID) error {
270310
_, err := d.coll.Updater().Filter(query.In("_id", ids...)).Updates(update.NewBuilder().Set("deleted_at", time.Now()).Set("is_publish", false).Set("is_top", false).Build()).UpdateMany(ctx)

internal/post/internal/repository/post.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ func (r *PostRepository) buildPostAggregationPipeline(cond bson.D, page *domain.
161161
)))
162162
}
163163

164+
// 添加分类过滤条件
165+
if page.CategoryName != "" {
166+
stageBuilder = stageBuilder.Match(query.And(
167+
query.Eq("category.type", "category"),
168+
query.Eq("category.name", page.CategoryName),
169+
))
170+
}
171+
164172
return stageBuilder.Sort(sortBuilder.Build()).Skip(skip).Limit(limit).Build()
165173
}
166174

@@ -178,7 +186,8 @@ func (r *PostRepository) GetList(ctx context.Context, page *domain.Page, postTyp
178186

179187
// 执行查询
180188
hasTagFilter := page.LabelName != ""
181-
posts, count, err := r.dao.GetListWithTagFilter(ctx, pagePipeline, cond, hasTagFilter, page.LabelName)
189+
hasCategoryFilter := page.CategoryName != ""
190+
posts, count, err := r.dao.GetListWithFilter(ctx, pagePipeline, cond, hasTagFilter, page.LabelName, hasCategoryFilter, page.CategoryName)
182191
if err != nil {
183192
return nil, 0, err
184193
}
@@ -219,7 +228,6 @@ func (r *PostRepository) FindByAlias(ctx context.Context, alias string) (*domain
219228
return r.PostDOToPostDomain(post), nil
220229
}
221230

222-
// PostDomain2PostDO 将domain.Post转换为dao.Post
223231
func (r *PostRepository) PostDomainToPostDO(post *domain.Post) *dao.Post {
224232
return &dao.Post{
225233
Model: mongox.Model{CreatedAt: post.CreatedAt},
@@ -258,7 +266,6 @@ func (r *PostRepository) PostDOToPostDomainList(posts []*dao.Post) []*domain.Pos
258266
})
259267
}
260268

261-
// PostDOToPostDomain 将dao.Post转换为domain.Post
262269
func (r *PostRepository) PostDOToPostDomain(post *dao.Post) *domain.Post {
263270
return &domain.Post{
264271
Id: post.ID,
@@ -277,7 +284,6 @@ func (r *PostRepository) PostDOToPostDomain(post *dao.Post) *domain.Post {
277284
}
278285
}
279286

280-
// PostCategoryTagsDOToPostDetail 将dao.PostCategoryTags转换为domain.PostDetail
281287
func (r *PostRepository) PostCategoryTagsDOToPostDetail(postCategoryTags *dao.PostCategoryTags) *domain.PostDetail {
282288
return &domain.PostDetail{
283289
Id: postCategoryTags.Id,

internal/post/internal/web/post.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,13 @@ func (h *PostHandler) AdminDeletePostBatch(c *gin.Context, postIDListRequest Pos
165165
// GetPublishPostList 获取发布文章列表
166166
func (h *PostHandler) GetPublishPostList(c *gin.Context, pageReq Page) (*apiwrap.Response[any], error) {
167167
postDetailList, total, err := h.serv.GetPostList(c, &domain.Page{
168-
PageNo: pageReq.PageNo,
169-
PageSize: pageReq.PageSize,
170-
Field: pageReq.Field,
171-
Order: pageReq.Order,
172-
Keyword: pageReq.Keyword,
173-
LabelName: pageReq.LabelName,
168+
PageNo: pageReq.PageNo,
169+
PageSize: pageReq.PageSize,
170+
Field: pageReq.Field,
171+
Order: pageReq.Order,
172+
Keyword: pageReq.Keyword,
173+
LabelName: pageReq.LabelName,
174+
CategoryName: pageReq.CategoryName,
174175
}, "publish")
175176
if err != nil {
176177
return nil, apiwrap.NewInternalError(err.Error())
@@ -184,12 +185,13 @@ func (h *PostHandler) GetPublishPostList(c *gin.Context, pageReq Page) (*apiwrap
184185
// AdminGetDraftDetailPostList 获取草稿箱文章列表
185186
func (h *PostHandler) AdminGetDraftDetailPostList(c *gin.Context, pageReq Page) (*apiwrap.Response[any], error) {
186187
postDetailList, total, err := h.serv.GetPostList(c, &domain.Page{
187-
PageNo: pageReq.PageNo,
188-
PageSize: pageReq.PageSize,
189-
Field: pageReq.Field,
190-
Order: pageReq.Order,
191-
Keyword: pageReq.Keyword,
192-
LabelName: pageReq.LabelName,
188+
PageNo: pageReq.PageNo,
189+
PageSize: pageReq.PageSize,
190+
Field: pageReq.Field,
191+
Order: pageReq.Order,
192+
Keyword: pageReq.Keyword,
193+
LabelName: pageReq.LabelName,
194+
CategoryName: pageReq.CategoryName,
193195
}, "draft")
194196
if err != nil {
195197
return nil, apiwrap.NewInternalError(err.Error())
@@ -202,12 +204,13 @@ func (h *PostHandler) AdminGetDraftDetailPostList(c *gin.Context, pageReq Page)
202204

203205
func (h *PostHandler) AdminGetBinDetailPostList(c *gin.Context, pageReq Page) (*apiwrap.Response[any], error) {
204206
postDetailList, total, err := h.serv.GetPostList(c, &domain.Page{
205-
PageNo: pageReq.PageNo,
206-
PageSize: pageReq.PageSize,
207-
Field: pageReq.Field,
208-
Order: pageReq.Order,
209-
Keyword: pageReq.Keyword,
210-
LabelName: pageReq.LabelName,
207+
PageNo: pageReq.PageNo,
208+
PageSize: pageReq.PageSize,
209+
Field: pageReq.Field,
210+
Order: pageReq.Order,
211+
Keyword: pageReq.Keyword,
212+
LabelName: pageReq.LabelName,
213+
CategoryName: pageReq.CategoryName,
211214
}, "bin")
212215
if err != nil {
213216
return nil, apiwrap.NewInternalError(err.Error())

internal/post/internal/web/request.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ type Page struct {
1515
Order string `form:"order" json:"order,omitempty" binding:"omitempty,oneof=ASC DESC"`
1616
// 搜索内容
1717
Keyword string `form:"keyword" json:"keyword,omitempty"`
18-
// 标签名称(用于过滤LabelType为"tag"的标签)
18+
// 标签名称
1919
LabelName string `form:"label_name" json:"label_name,omitempty"`
20+
// 分类名称
21+
CategoryName string `form:"category_name" json:"category_name,omitempty"`
2022
}
2123

2224
type PostDto struct {

0 commit comments

Comments
 (0)