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

@@ -403,3 +403,53 @@ func (r *UserRepository) GetMutualFollowStatus(currentUserID string, targetUserI
return result, nil
}
// AdminList 管理端分页获取用户列表(支持关键词和状态筛选)
func (r *UserRepository) AdminList(page, pageSize int, keyword, status string) ([]*model.User, int64, error) {
var users []*model.User
var total int64
query := r.db.Model(&model.User{})
// 关键词搜索(用户名、昵称、邮箱)
if keyword != "" {
if r.db.Dialector.Name() == "postgres" {
query = query.Where(
"username ILIKE ? OR nickname ILIKE ? OR email ILIKE ?",
"%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%",
)
} else {
lowerSearchPattern := "%" + strings.ToLower(keyword) + "%"
query = query.Where(
"LOWER(username) LIKE ? OR LOWER(nickname) LIKE ? OR LOWER(email) LIKE ?",
lowerSearchPattern, lowerSearchPattern, lowerSearchPattern,
)
}
}
// 状态筛选
if status != "" {
query = query.Where("status = ?", status)
}
query.Count(&total)
offset := (page - 1) * pageSize
err := query.Order("created_at DESC, id DESC").Offset(offset).Limit(pageSize).Find(&users).Error
return users, total, err
}
// GetCommentsCount 获取用户评论数
func (r *UserRepository) GetCommentsCount(userID string) (int64, error) {
var count int64
err := r.db.Model(&model.Comment{}).
Where("user_id = ?", userID).
Count(&count).Error
return count, err
}
// UpdateStatus 更新用户状态
func (r *UserRepository) UpdateStatus(userID string, status model.UserStatus) error {
return r.db.Model(&model.User{}).Where("id = ?", userID).Update("status", status).Error
}