feat: 管理首页帖子总数统计(总数/已发布/待审核)

- PostRepo 新增 CountPostsByStatus 和 CountPending 方法
- Service 层定义 postAdminStatStore 接口 (ISP)
- AdminService 注入 postRepo,新增 PostStats 结构体和 GetPostStats 方法
- AdminController.Dashboard 调用 GetPostStats 并传参到模板
- Dashboard 帖子卡片显示总数 + 已发布 + 待审核(橙色徽章)
- DI 层将 AdminController 创建从 buildDepsCore 移至 buildDeps,以便注入 postRepo
This commit is contained in:
2026-05-31 12:46:06 +08:00
parent 891990bb35
commit f0bf450725
8 changed files with 69 additions and 8 deletions

View File

@ -137,6 +137,24 @@ func (r *PostRepo) SoftDelete(id uint) error {
return r.db.Delete(&model.Post{}, id).Error
}
// CountPostsByStatus 统计帖子数量status 为空则统计全部未删除帖子)
func (r *PostRepo) CountPostsByStatus(status string) (int64, error) {
query := r.buildQuery("", status)
var total int64
err := query.Count(&total).Error
return total, err
}
// CountPending 统计待审核帖子数pending 状态 + approved 但有待审修订)
func (r *PostRepo) CountPending() (int64, error) {
var total int64
err := r.db.Table("posts").
Where("deleted_at IS NULL").
Where("status = ? OR (status = ? AND pending_body != '')", model.PostStatusPending, model.PostStatusApproved).
Count(&total).Error
return total, err
}
// Restore 恢复软删除
func (r *PostRepo) Restore(id uint) error {
result := r.db.Unscoped().Model(&model.Post{}).Where("id = ?", id).Update("deleted_at", nil)