99 lines
2.5 KiB
Go
99 lines
2.5 KiB
Go
package services
|
|
|
|
import (
|
|
"lv8girl/internal/models"
|
|
"lv8girl/internal/repositories"
|
|
)
|
|
|
|
type AdminService struct {
|
|
userRepo *repositories.UserRepository
|
|
discussionRepo *repositories.DiscussionRepository
|
|
commentRepo *repositories.CommentRepository
|
|
likeRepo *repositories.LikeRepository
|
|
}
|
|
|
|
func NewAdminService() *AdminService {
|
|
return &AdminService{
|
|
userRepo: repositories.NewUserRepository(),
|
|
discussionRepo: repositories.NewDiscussionRepository(),
|
|
commentRepo: repositories.NewCommentRepository(),
|
|
likeRepo: repositories.NewLikeRepository(),
|
|
}
|
|
}
|
|
|
|
type Stats struct {
|
|
Posts int64
|
|
Users int64
|
|
Comments int64
|
|
Likes int64
|
|
Online int64
|
|
Approved int64
|
|
Rejected int64
|
|
Pending int64
|
|
}
|
|
|
|
func (s *AdminService) GetStats() (*Stats, error) {
|
|
stats := &Stats{}
|
|
stats.Posts, _ = s.discussionRepo.Count()
|
|
stats.Users, _ = s.userRepo.Count()
|
|
stats.Comments, _ = s.commentRepo.Count()
|
|
stats.Likes, _ = s.likeRepo.Count()
|
|
stats.Online, _ = s.userRepo.CountOnline()
|
|
stats.Approved, _ = s.discussionRepo.CountByStatus("approved")
|
|
stats.Rejected, _ = s.discussionRepo.CountByStatus("rejected")
|
|
stats.Pending, _ = s.discussionRepo.CountByStatus("pending")
|
|
return stats, nil
|
|
}
|
|
|
|
func (s *AdminService) GetPendingPosts() ([]models.Discussion, error) {
|
|
return s.discussionRepo.FindPending()
|
|
}
|
|
|
|
func (s *AdminService) GetPendingUsers() ([]models.User, error) {
|
|
return s.userRepo.FindPending()
|
|
}
|
|
|
|
func (s *AdminService) ApprovePost(id uint) error {
|
|
return s.discussionRepo.UpdateStatus(id, "approved")
|
|
}
|
|
|
|
func (s *AdminService) RejectPost(id uint) error {
|
|
return s.discussionRepo.UpdateStatus(id, "rejected")
|
|
}
|
|
|
|
func (s *AdminService) DeletePost(id uint) error {
|
|
return s.discussionRepo.Delete(id)
|
|
}
|
|
|
|
func (s *AdminService) ApproveUser(id uint) error {
|
|
return s.userRepo.UpdateField(id, "status", "approved")
|
|
}
|
|
|
|
func (s *AdminService) RejectUser(id uint) error {
|
|
return s.userRepo.UpdateField(id, "status", "rejected")
|
|
}
|
|
|
|
func (s *AdminService) UpdateUserRole(id uint, role string) error {
|
|
return s.userRepo.UpdateField(id, "role", role)
|
|
}
|
|
|
|
func (s *AdminService) DeleteUser(id uint) error {
|
|
return s.userRepo.Delete(id)
|
|
}
|
|
|
|
func (s *AdminService) GetAllPosts() ([]models.Discussion, error) {
|
|
return s.discussionRepo.FindAll()
|
|
}
|
|
|
|
func (s *AdminService) GetAllUsers() ([]models.User, error) {
|
|
return s.userRepo.FindAll()
|
|
}
|
|
|
|
func (s *AdminService) GetAllComments() ([]models.Comment, error) {
|
|
return s.commentRepo.FindAll()
|
|
}
|
|
|
|
func (s *AdminService) DeleteComment(id uint) error {
|
|
return s.commentRepo.Delete(id)
|
|
}
|