- Updated main.go to initialize email service and include it in the dependency injection container. - Refactored handlers to utilize context in service method calls, improving consistency and error handling. - Introduced new service options for upload, security, and captcha services, enhancing modularity and testability. - Removed unused repository implementations to streamline the codebase. This commit continues the effort to improve the architecture by ensuring all services are properly injected and utilized across the application.
103 lines
2.6 KiB
Go
103 lines
2.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"carrotskin/internal/model"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// userRepository UserRepository的实现
|
|
type userRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewUserRepository 创建UserRepository实例
|
|
func NewUserRepository(db *gorm.DB) UserRepository {
|
|
return &userRepository{db: db}
|
|
}
|
|
|
|
func (r *userRepository) Create(user *model.User) error {
|
|
return r.db.Create(user).Error
|
|
}
|
|
|
|
func (r *userRepository) FindByID(id int64) (*model.User, error) {
|
|
var user model.User
|
|
err := r.db.Where("id = ? AND status != -1", id).First(&user).Error
|
|
return handleNotFoundResult(&user, err)
|
|
}
|
|
|
|
func (r *userRepository) FindByUsername(username string) (*model.User, error) {
|
|
var user model.User
|
|
err := r.db.Where("username = ? AND status != -1", username).First(&user).Error
|
|
return handleNotFoundResult(&user, err)
|
|
}
|
|
|
|
func (r *userRepository) FindByEmail(email string) (*model.User, error) {
|
|
var user model.User
|
|
err := r.db.Where("email = ? AND status != -1", email).First(&user).Error
|
|
return handleNotFoundResult(&user, err)
|
|
}
|
|
|
|
func (r *userRepository) Update(user *model.User) error {
|
|
return r.db.Save(user).Error
|
|
}
|
|
|
|
func (r *userRepository) UpdateFields(id int64, fields map[string]interface{}) error {
|
|
return r.db.Model(&model.User{}).Where("id = ?", id).Updates(fields).Error
|
|
}
|
|
|
|
func (r *userRepository) Delete(id int64) error {
|
|
return r.db.Model(&model.User{}).Where("id = ?", id).Update("status", -1).Error
|
|
}
|
|
|
|
func (r *userRepository) CreateLoginLog(log *model.UserLoginLog) error {
|
|
return r.db.Create(log).Error
|
|
}
|
|
|
|
func (r *userRepository) CreatePointLog(log *model.UserPointLog) error {
|
|
return r.db.Create(log).Error
|
|
}
|
|
|
|
func (r *userRepository) UpdatePoints(userID int64, amount int, changeType, reason string) error {
|
|
return r.db.Transaction(func(tx *gorm.DB) error {
|
|
var user model.User
|
|
if err := tx.Where("id = ?", userID).First(&user).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
balanceBefore := user.Points
|
|
balanceAfter := balanceBefore + amount
|
|
|
|
if balanceAfter < 0 {
|
|
return errors.New("积分不足")
|
|
}
|
|
|
|
if err := tx.Model(&user).Update("points", balanceAfter).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
log := &model.UserPointLog{
|
|
UserID: userID,
|
|
ChangeType: changeType,
|
|
Amount: amount,
|
|
BalanceBefore: balanceBefore,
|
|
BalanceAfter: balanceAfter,
|
|
Reason: reason,
|
|
}
|
|
|
|
return tx.Create(log).Error
|
|
})
|
|
}
|
|
|
|
// handleNotFoundResult 处理记录未找到的情况
|
|
func handleNotFoundResult[T any](result *T, err error) (*T, error) {
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|