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:
145
internal/service/admin_user_service.go
Normal file
145
internal/service/admin_user_service.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"carrot_bbs/internal/dto"
|
||||
"carrot_bbs/internal/model"
|
||||
"carrot_bbs/internal/repository"
|
||||
)
|
||||
|
||||
// AdminUserService 管理端用户服务接口
|
||||
type AdminUserService interface {
|
||||
// GetUserList 获取用户列表(分页、搜索、状态筛选)
|
||||
GetUserList(ctx context.Context, page, pageSize int, keyword, status string) ([]dto.AdminUserListResponse, int64, error)
|
||||
// GetUserDetail 获取用户详情(含统计信息)
|
||||
GetUserDetail(ctx context.Context, userID string) (*dto.AdminUserDetailResponse, error)
|
||||
// UpdateUserStatus 更新用户状态
|
||||
UpdateUserStatus(ctx context.Context, userID string, status model.UserStatus) (*dto.AdminUserDetailResponse, error)
|
||||
}
|
||||
|
||||
// adminUserServiceImpl 管理端用户服务实现
|
||||
type adminUserServiceImpl struct {
|
||||
userRepo *repository.UserRepository
|
||||
}
|
||||
|
||||
// NewAdminUserService 创建管理端用户服务
|
||||
func NewAdminUserService(userRepo *repository.UserRepository) AdminUserService {
|
||||
return &adminUserServiceImpl{
|
||||
userRepo: userRepo,
|
||||
}
|
||||
}
|
||||
|
||||
// GetUserList 获取用户列表
|
||||
func (s *adminUserServiceImpl) GetUserList(ctx context.Context, page, pageSize int, keyword, status string) ([]dto.AdminUserListResponse, int64, error) {
|
||||
users, total, err := s.userRepo.AdminList(page, pageSize, keyword, status)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
responses := make([]dto.AdminUserListResponse, len(users))
|
||||
for i, user := range users {
|
||||
responses[i] = convertUserToAdminListResponse(user)
|
||||
}
|
||||
|
||||
return responses, total, nil
|
||||
}
|
||||
|
||||
// GetUserDetail 获取用户详情
|
||||
func (s *adminUserServiceImpl) GetUserDetail(ctx context.Context, userID string) (*dto.AdminUserDetailResponse, error) {
|
||||
user, err := s.userRepo.GetByID(userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取帖子数
|
||||
postsCount, _ := s.userRepo.GetPostsCount(userID)
|
||||
|
||||
// 获取评论数
|
||||
commentsCount, _ := s.userRepo.GetCommentsCount(userID)
|
||||
|
||||
return convertUserToAdminDetailResponse(user, postsCount, commentsCount), nil
|
||||
}
|
||||
|
||||
// UpdateUserStatus 更新用户状态
|
||||
func (s *adminUserServiceImpl) UpdateUserStatus(ctx context.Context, userID string, status model.UserStatus) (*dto.AdminUserDetailResponse, error) {
|
||||
// 先检查用户是否存在
|
||||
user, err := s.userRepo.GetByID(userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 更新状态
|
||||
if err := s.userRepo.UpdateStatus(userID, status); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 重新获取用户信息
|
||||
user, err = s.userRepo.GetByID(userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取统计信息
|
||||
postsCount, _ := s.userRepo.GetPostsCount(userID)
|
||||
commentsCount, _ := s.userRepo.GetCommentsCount(userID)
|
||||
|
||||
return convertUserToAdminDetailResponse(user, postsCount, commentsCount), nil
|
||||
}
|
||||
|
||||
// convertUserToAdminListResponse 转换用户为管理端列表响应
|
||||
func convertUserToAdminListResponse(user *model.User) dto.AdminUserListResponse {
|
||||
var lastLoginAt string
|
||||
if user.LastLoginAt != nil {
|
||||
lastLoginAt = user.LastLoginAt.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
return dto.AdminUserListResponse{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
Nickname: user.Nickname,
|
||||
Email: user.Email,
|
||||
Phone: user.Phone,
|
||||
EmailVerified: user.EmailVerified,
|
||||
Avatar: user.Avatar,
|
||||
Status: string(user.Status),
|
||||
PostsCount: user.PostsCount,
|
||||
FollowersCount: user.FollowersCount,
|
||||
FollowingCount: user.FollowingCount,
|
||||
LastLoginAt: lastLoginAt,
|
||||
LastLoginIP: user.LastLoginIP,
|
||||
CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
}
|
||||
|
||||
// convertUserToAdminDetailResponse 转换用户为管理端详情响应
|
||||
func convertUserToAdminDetailResponse(user *model.User, postsCount, commentsCount int64) *dto.AdminUserDetailResponse {
|
||||
var lastLoginAt string
|
||||
if user.LastLoginAt != nil {
|
||||
lastLoginAt = user.LastLoginAt.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
return &dto.AdminUserDetailResponse{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
Nickname: user.Nickname,
|
||||
Email: user.Email,
|
||||
Phone: user.Phone,
|
||||
EmailVerified: user.EmailVerified,
|
||||
Avatar: user.Avatar,
|
||||
CoverURL: user.CoverURL,
|
||||
Bio: user.Bio,
|
||||
Website: user.Website,
|
||||
Location: user.Location,
|
||||
IsVerified: user.IsVerified,
|
||||
Status: string(user.Status),
|
||||
PostsCount: int(postsCount),
|
||||
CommentsCount: commentsCount,
|
||||
FollowersCount: user.FollowersCount,
|
||||
FollowingCount: user.FollowingCount,
|
||||
LastLoginAt: lastLoginAt,
|
||||
LastLoginIP: user.LastLoginIP,
|
||||
CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: user.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user