feat(admin): add comprehensive admin management system

- Add admin handlers and services for users, posts, comments, groups, and dashboard
- Implement role permission management with ability to view and update permissions
- Add database seeding for roles and Casbin permission policies
- Upgrade Casbin from v2 to v3 with GORM adapter for persistent policy storage
- Add admin-specific repository methods with filtering and pagination
- Register new admin API routes under /api/v1/admin/*
This commit is contained in:
2026-03-14 18:01:55 +08:00
parent ae5b997924
commit d3065b30cc
28 changed files with 4018 additions and 107 deletions

View File

@@ -298,3 +298,96 @@ func (r *CommentRepository) IsLiked(commentID, userID string) bool {
r.db.Model(&model.CommentLike{}).Where("comment_id = ? AND user_id = ?", commentID, userID).Count(&count)
return count > 0
}
// AdminCommentListFilter 管理端评论列表筛选条件
type AdminCommentListFilter struct {
Keyword string
PostID string
AuthorID string
Status string
StartDate string
EndDate string
Page int
PageSize int
}
// GetAdminCommentList 获取管理端评论列表
func (r *CommentRepository) GetAdminCommentList(filter AdminCommentListFilter) ([]*model.Comment, int64, error) {
var comments []*model.Comment
var total int64
query := r.db.Model(&model.Comment{}).Preload("User")
// 应用筛选条件
if filter.Keyword != "" {
query = query.Where("content LIKE ?", "%"+filter.Keyword+"%")
}
if filter.PostID != "" {
query = query.Where("post_id = ?", filter.PostID)
}
if filter.AuthorID != "" {
query = query.Where("user_id = ?", filter.AuthorID)
}
if filter.Status != "" {
query = query.Where("status = ?", filter.Status)
}
if filter.StartDate != "" {
query = query.Where("created_at >= ?", filter.StartDate)
}
if filter.EndDate != "" {
query = query.Where("created_at <= ?", filter.EndDate+" 23:59:59")
}
// 统计总数
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
// 分页查询
offset := (filter.Page - 1) * filter.PageSize
if err := query.Offset(offset).Limit(filter.PageSize).Order("created_at DESC").Find(&comments).Error; err != nil {
return nil, 0, err
}
return comments, total, nil
}
// GetAdminCommentByID 获取管理端评论详情(包含关联信息)
func (r *CommentRepository) GetAdminCommentByID(id string) (*model.Comment, error) {
var comment model.Comment
err := r.db.Preload("User").First(&comment, "id = ?", id).Error
if err != nil {
return nil, err
}
return &comment, nil
}
// BatchDelete 批量删除评论
func (r *CommentRepository) BatchDelete(ids []string) ([]string, error) {
var failedIDs []string
for _, id := range ids {
if err := r.Delete(id); err != nil {
failedIDs = append(failedIDs, id)
}
}
return failedIDs, nil
}
// GetCommentsByPostIDForAdmin 管理端获取帖子的评论列表(包含所有状态)
func (r *CommentRepository) GetCommentsByPostIDForAdmin(postID string, page, pageSize int) ([]*model.Comment, int64, error) {
var comments []*model.Comment
var total int64
r.db.Model(&model.Comment{}).Where("post_id = ?", postID).Count(&total)
offset := (page - 1) * pageSize
err := r.db.Where("post_id = ?", postID).
Preload("User").
Offset(offset).Limit(pageSize).
Order("created_at DESC").
Find(&comments).Error
return comments, total, err
}