lv8girl!
This commit is contained in:
94
internal/services/user.go
Normal file
94
internal/services/user.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"lv8girl/internal/models"
|
||||
"lv8girl/internal/repositories"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
userRepo *repositories.UserRepository
|
||||
discussionRepo *repositories.DiscussionRepository
|
||||
commentRepo *repositories.CommentRepository
|
||||
messageRepo *repositories.MessageRepository
|
||||
}
|
||||
|
||||
func NewUserService() *UserService {
|
||||
return &UserService{
|
||||
userRepo: repositories.NewUserRepository(),
|
||||
discussionRepo: repositories.NewDiscussionRepository(),
|
||||
commentRepo: repositories.NewCommentRepository(),
|
||||
messageRepo: repositories.NewMessageRepository(),
|
||||
}
|
||||
}
|
||||
|
||||
type UserProfileView struct {
|
||||
User *models.User
|
||||
Posts []models.Discussion
|
||||
PostCount int64
|
||||
UnreadCount int64
|
||||
IsOwner bool
|
||||
}
|
||||
|
||||
func (s *UserService) GetUserProfile(viewerID, targetUserID uint) (*UserProfileView, error) {
|
||||
user, err := s.userRepo.FindByID(targetUserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
posts, _ := s.discussionRepo.FindByUserID(targetUserID)
|
||||
postCount, _ := s.discussionRepo.CountByUserID(targetUserID)
|
||||
|
||||
var unreadCount int64
|
||||
if viewerID != 0 {
|
||||
unreadCount, _ = s.messageRepo.CountUnread(viewerID)
|
||||
}
|
||||
|
||||
return &UserProfileView{
|
||||
User: user,
|
||||
Posts: posts,
|
||||
PostCount: postCount,
|
||||
UnreadCount: unreadCount,
|
||||
IsOwner: viewerID == targetUserID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *UserService) UpdateAvatar(userID uint, avatarPath string) error {
|
||||
user, err := s.userRepo.FindByID(userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.userRepo.UpdateField(user.ID, "avatar", avatarPath)
|
||||
}
|
||||
|
||||
func (s *UserService) GetUserByID(id uint) (*models.User, error) {
|
||||
return s.userRepo.FindByID(id)
|
||||
}
|
||||
|
||||
func (s *UserService) UpdateUserStatus(id uint, status string) error {
|
||||
return s.userRepo.UpdateField(id, "status", status)
|
||||
}
|
||||
|
||||
func (s *UserService) UpdateUserRole(id uint, role string) error {
|
||||
return s.userRepo.UpdateField(id, "role", role)
|
||||
}
|
||||
|
||||
func (s *UserService) DeleteUser(id uint) error {
|
||||
return s.userRepo.Delete(id)
|
||||
}
|
||||
|
||||
func (s *UserService) GetPendingUsers() ([]models.User, error) {
|
||||
return s.userRepo.FindPending()
|
||||
}
|
||||
|
||||
func (s *UserService) GetAllUsers() ([]models.User, error) {
|
||||
return s.userRepo.FindAll()
|
||||
}
|
||||
|
||||
func (s *UserService) GetUserStats() (int64, int64, error) {
|
||||
total, err := s.userRepo.Count()
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
online, err := s.userRepo.CountOnline()
|
||||
return total, online, err
|
||||
}
|
||||
Reference in New Issue
Block a user