feat: 文章属性 — Post Model 扩展 + 置顶系统
- Post 新增 7 字段:Visibility/PostType/ReprintSource/Declaration/ReprintProhibited/PinType/PinnedAt - 公开列表过滤 private 文章(管理员可见全部) - 列表排序:全局置顶 > 按时间 - declarationLabel 模板函数(7 种创作声明) - Pin/Unpin API(admin+,category/global 两种置顶类型)
This commit is contained in:
@ -62,22 +62,24 @@ func (r *PostRepo) buildQuery(keyword, status string) *gorm.DB {
|
||||
return query
|
||||
}
|
||||
|
||||
// FindPageable 分页查询帖子列表(仅 approved 状态,关键词模糊搜索标题)
|
||||
// FindPageable 分页查询帖子列表(仅 approved 状态,关键词模糊搜索标题,过滤私密)
|
||||
func (r *PostRepo) FindPageable(keyword string, offset, limit int) ([]model.Post, int64, error) {
|
||||
query := r.buildQuery(keyword, model.PostStatusApproved)
|
||||
query := r.buildQuery(keyword, model.PostStatusApproved).
|
||||
Where("posts.visibility = ?", model.VisibilityPublic)
|
||||
return r.pageResults(query, offset, limit)
|
||||
}
|
||||
|
||||
// FindAdminPageable 管理后台分页查询(全状态筛选,待审置顶)
|
||||
// FindAdminPageable 管理后台分页查询(全状态筛选,待审置顶,管理可见私密)
|
||||
func (r *PostRepo) FindAdminPageable(keyword, status string, offset, limit int) ([]model.Post, int64, error) {
|
||||
query := r.buildQuery(keyword, status)
|
||||
return r.pageResultsAdmin(query, offset, limit)
|
||||
}
|
||||
|
||||
// FindByUserID 分页查询某用户发布的帖子(仅 approved,含作者用户名)
|
||||
// FindByUserID 分页查询某用户发布的帖子(仅 approved + 公开,含作者用户名)
|
||||
func (r *PostRepo) FindByUserID(userID uint, offset, limit int) ([]model.Post, int64, error) {
|
||||
query := r.buildQuery("", model.PostStatusApproved).
|
||||
Where("posts.user_id = ?", userID)
|
||||
Where("posts.user_id = ?", userID).
|
||||
Where("posts.visibility = ?", model.VisibilityPublic)
|
||||
return r.pageResults(query, offset, limit)
|
||||
}
|
||||
|
||||
@ -95,7 +97,7 @@ func (r *PostRepo) FindPendingRevisions(keyword string, offset, limit int) ([]mo
|
||||
return r.pageResults(query, offset, limit)
|
||||
}
|
||||
|
||||
// pageResults 执行 Count + Offset/Limit + ORDER BY
|
||||
// pageResults 执行 Count + Offset/Limit + ORDER BY(置顶优先,再按时间排序)
|
||||
func (r *PostRepo) pageResults(query *gorm.DB, offset, limit int) ([]model.Post, int64, error) {
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
@ -103,7 +105,7 @@ func (r *PostRepo) pageResults(query *gorm.DB, offset, limit int) ([]model.Post,
|
||||
}
|
||||
|
||||
var posts []model.Post
|
||||
err := query.Order("posts.created_at DESC").
|
||||
err := query.Order("CASE WHEN posts.pin_type = 'global' THEN 0 ELSE 1 END ASC, posts.pinned_at DESC NULLS LAST, posts.created_at DESC").
|
||||
Offset(offset).Limit(limit).Find(&posts).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
|
||||
Reference in New Issue
Block a user