This commit is contained in:
2025-09-01 21:15:15 +08:00
parent 16a8cd5506
commit 2f3817c8f0
6 changed files with 181 additions and 1 deletions

View File

@@ -263,11 +263,100 @@ func (r *GormArticleRepository) Search(ctx context.Context, query *repoQueries.S
return result, total, nil
}
// ListArticles 获取文章列表
// ListArticles 获取文章列表(用户端)
func (r *GormArticleRepository) ListArticles(ctx context.Context, query *repoQueries.ListArticleQuery) ([]*entities.Article, int64, error) {
var articles []entities.Article
var total int64
dbQuery := r.db.WithContext(ctx).Model(&entities.Article{}).
Select("id, title, summary, cover_image, category_id, status, is_featured, published_at, created_at, updated_at, scheduled_at")
// 用户端不显示归档文章
dbQuery = dbQuery.Where("status != ?", entities.ArticleStatusArchived)
// 应用筛选条件
if query.Status != "" {
dbQuery = dbQuery.Where("status = ?", query.Status)
}
if query.CategoryID != "" {
// 如果指定了分类ID只查询该分类的文章包括没有分类的文章当CategoryID为空字符串时
if query.CategoryID == "null" || query.CategoryID == "" {
// 查询没有分类的文章
dbQuery = dbQuery.Where("category_id IS NULL OR category_id = ''")
} else {
// 查询指定分类的文章
dbQuery = dbQuery.Where("category_id = ?", query.CategoryID)
}
}
if query.TagID != "" {
// 如果指定了标签ID只查询有关联该标签的文章
// 使用子查询而不是JOIN避免影响其他查询条件
subQuery := r.db.WithContext(ctx).Table("article_tag_relations").
Select("article_id").
Where("tag_id = ?", query.TagID)
dbQuery = dbQuery.Where("id IN (?)", subQuery)
}
if query.Title != "" {
dbQuery = dbQuery.Where("title ILIKE ?", "%"+query.Title+"%")
}
if query.Summary != "" {
dbQuery = dbQuery.Where("summary ILIKE ?", "%"+query.Summary+"%")
}
if query.IsFeatured != nil {
dbQuery = dbQuery.Where("is_featured = ?", *query.IsFeatured)
}
// 获取总数
if err := dbQuery.Count(&total).Error; err != nil {
r.logger.Error("获取文章列表总数失败", zap.Error(err))
return nil, 0, err
}
// 应用排序
if query.OrderBy != "" {
orderDir := "DESC"
if query.OrderDir != "" {
orderDir = strings.ToUpper(query.OrderDir)
}
dbQuery = dbQuery.Order(fmt.Sprintf("%s %s", query.OrderBy, orderDir))
} else {
dbQuery = dbQuery.Order("created_at DESC")
}
// 应用分页
if query.Page > 0 && query.PageSize > 0 {
offset := (query.Page - 1) * query.PageSize
dbQuery = dbQuery.Offset(offset).Limit(query.PageSize)
}
// 预加载关联数据
dbQuery = dbQuery.Preload("Category").Preload("Tags")
// 获取数据
if err := dbQuery.Find(&articles).Error; err != nil {
r.logger.Error("获取文章列表失败", zap.Error(err))
return nil, 0, err
}
// 转换为指针切片
result := make([]*entities.Article, len(articles))
for i := range articles {
result[i] = &articles[i]
}
return result, total, nil
}
// ListArticlesForAdmin 获取文章列表(管理员端)
func (r *GormArticleRepository) ListArticlesForAdmin(ctx context.Context, query *repoQueries.ListArticleQuery) ([]*entities.Article, int64, error) {
var articles []entities.Article
var total int64
dbQuery := r.db.WithContext(ctx).Model(&entities.Article{}).
Select("id, title, summary, cover_image, category_id, status, is_featured, published_at, view_count, created_at, updated_at, scheduled_at")