124 lines
3.3 KiB
Go
124 lines
3.3 KiB
Go
package storage
|
|
|
|
import (
|
|
"goalfymax-admin/internal/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type GoalfyMaxUserStorage interface {
|
|
Create(user *models.GoalfyMaxUser) error
|
|
GetByID(id uint) (*models.GoalfyMaxUser, error)
|
|
GetByUserID(userID int) (*models.GoalfyMaxUser, error)
|
|
GetByUsername(username string) (*models.GoalfyMaxUser, error)
|
|
GetByEmail(email string) (*models.GoalfyMaxUser, error)
|
|
Update(user *models.GoalfyMaxUser) error
|
|
Delete(id uint) error
|
|
List(req *models.GoalfyMaxUserListRequest) ([]models.GoalfyMaxUser, int64, error)
|
|
SetBanned(id uint, reason string, adminID int) error
|
|
Unban(id uint) error
|
|
}
|
|
|
|
type goalfyMaxUserStorage struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewGoalfyMaxUserStorage() GoalfyMaxUserStorage {
|
|
return &goalfyMaxUserStorage{db: DB}
|
|
}
|
|
|
|
func (s *goalfyMaxUserStorage) Create(user *models.GoalfyMaxUser) error {
|
|
return s.db.Create(user).Error
|
|
}
|
|
|
|
func (s *goalfyMaxUserStorage) GetByID(id uint) (*models.GoalfyMaxUser, error) {
|
|
var user models.GoalfyMaxUser
|
|
if err := s.db.First(&user, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (s *goalfyMaxUserStorage) GetByUserID(userID int) (*models.GoalfyMaxUser, error) {
|
|
var user models.GoalfyMaxUser
|
|
if err := s.db.Where("user_id = ?", userID).First(&user).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (s *goalfyMaxUserStorage) GetByUsername(username string) (*models.GoalfyMaxUser, error) {
|
|
var user models.GoalfyMaxUser
|
|
if err := s.db.Where("username = ?", username).First(&user).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (s *goalfyMaxUserStorage) GetByEmail(email string) (*models.GoalfyMaxUser, error) {
|
|
var user models.GoalfyMaxUser
|
|
if err := s.db.Where("email = ?", email).First(&user).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (s *goalfyMaxUserStorage) Update(user *models.GoalfyMaxUser) error {
|
|
return s.db.Save(user).Error
|
|
}
|
|
|
|
func (s *goalfyMaxUserStorage) Delete(id uint) error {
|
|
return s.db.Delete(&models.GoalfyMaxUser{}, id).Error
|
|
}
|
|
|
|
func (s *goalfyMaxUserStorage) List(req *models.GoalfyMaxUserListRequest) ([]models.GoalfyMaxUser, int64, error) {
|
|
var users []models.GoalfyMaxUser
|
|
var total int64
|
|
|
|
q := s.db.Model(&models.GoalfyMaxUser{})
|
|
if req.Username != "" {
|
|
q = q.Where("username LIKE ?", "%"+req.Username+"%")
|
|
}
|
|
if req.Email != "" {
|
|
q = q.Where("email LIKE ?", "%"+req.Email+"%")
|
|
}
|
|
if req.Status != nil {
|
|
if *req.Status == 0 {
|
|
q = q.Where("is_banned = ?", true)
|
|
} else if *req.Status == 1 {
|
|
q = q.Where("is_banned = ?", false)
|
|
}
|
|
}
|
|
|
|
if err := q.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (req.Page - 1) * req.Size
|
|
if err := q.Offset(offset).Limit(req.Size).Order("id DESC").Find(&users).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return users, total, nil
|
|
}
|
|
|
|
func (s *goalfyMaxUserStorage) SetBanned(id uint, reason string, adminID int) error {
|
|
return s.db.Model(&models.GoalfyMaxUser{}).
|
|
Where("id = ?", id).
|
|
Updates(map[string]interface{}{
|
|
"is_banned": true,
|
|
"ban_reason": reason,
|
|
"banned_by": adminID,
|
|
"banned_at": gorm.Expr("NOW()"),
|
|
}).Error
|
|
}
|
|
|
|
func (s *goalfyMaxUserStorage) Unban(id uint) error {
|
|
return s.db.Model(&models.GoalfyMaxUser{}).
|
|
Where("id = ?", id).
|
|
Updates(map[string]interface{}{
|
|
"is_banned": false,
|
|
"ban_reason": "",
|
|
"banned_by": 0,
|
|
"banned_at": nil,
|
|
}).Error
|
|
}
|