105 lines
3.0 KiB
Go
105 lines
3.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"cloudnest/internal/domain/auth/entity"
|
|
"cloudnest/internal/domain/auth/repository"
|
|
"cloudnest/internal/infrastructure/database"
|
|
"cloudnest/internal/pkg/errors"
|
|
"gorm.io/gorm"
|
|
"strings"
|
|
)
|
|
|
|
type userRepository struct {
|
|
db *database.MySQLClient
|
|
}
|
|
|
|
func NewUserRepository(db *database.MySQLClient) repository.UserRepository {
|
|
return &userRepository{db: db}
|
|
}
|
|
|
|
func (r *userRepository) FindByUsername(username string) (*entity.User, error) {
|
|
var user entity.User
|
|
err := r.db.DB.Where("username = ?", username).First(&user).Error
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, errors.NotFound("用户不存在")
|
|
}
|
|
return nil, errors.InternalError("查询用户失败", err)
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *userRepository) FindByUserCode(userCode string) (*entity.User, error) {
|
|
var user entity.User
|
|
err := r.db.DB.Where("user_code = ?", userCode).First(&user).Error
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, errors.NotFound("用户不存在")
|
|
}
|
|
return nil, errors.InternalError("查询用户失败", err)
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *userRepository) FindByEmail(email string) (*entity.User, error) {
|
|
var user entity.User
|
|
err := r.db.DB.Where("email = ?", email).First(&user).Error
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, errors.NotFound("用户不存在")
|
|
}
|
|
return nil, errors.InternalError("查询用户失败", err)
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *userRepository) Create(user *entity.User) error {
|
|
err := r.db.DB.Create(user).Error
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "Duplicate entry") || strings.Contains(err.Error(), "unique constraint") {
|
|
return errors.Conflict("用户名或用户码已存在")
|
|
}
|
|
return errors.InternalError("创建用户失败", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *userRepository) FindByID(id uint) (*entity.User, error) {
|
|
var user entity.User
|
|
err := r.db.DB.First(&user, id).Error
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, errors.NotFound("用户不存在")
|
|
}
|
|
return nil, errors.InternalError("查询用户失败", err)
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *userRepository) UpdateStorageUsed(userCode string, size int64) error {
|
|
err := r.db.DB.Model(&entity.User{}).Where("user_code = ?", userCode).Update("storage_used", gorm.Expr("storage_used + ?", size)).Error
|
|
if err != nil {
|
|
return errors.InternalError("更新存储空间失败", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *userRepository) UpdateProfile(userCode, email, phone, nickname string) error {
|
|
err := r.db.DB.Model(&entity.User{}).Where("user_code = ?", userCode).Updates(map[string]interface{}{
|
|
"email": email,
|
|
"phone": phone,
|
|
"nickname": nickname,
|
|
}).Error
|
|
if err != nil {
|
|
return errors.InternalError("更新个人信息失败", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *userRepository) UpdatePassword(userCode, newPassword string) error {
|
|
err := r.db.DB.Model(&entity.User{}).Where("user_code = ?", userCode).Update("password", newPassword).Error
|
|
if err != nil {
|
|
return errors.InternalError("更新密码失败", err)
|
|
}
|
|
return nil
|
|
} |