feat: Enhance dependency injection and service integration
- 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.
This commit is contained in:
@@ -7,60 +7,60 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CreateUser 创建用户
|
||||
func CreateUser(user *model.User) error {
|
||||
return getDB().Create(user).Error
|
||||
// userRepository UserRepository的实现
|
||||
type userRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// FindUserByID 根据ID查找用户
|
||||
func FindUserByID(id int64) (*model.User, error) {
|
||||
// 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 := getDB().Where("id = ? AND status != -1", id).First(&user).Error
|
||||
return HandleNotFound(&user, err)
|
||||
err := r.db.Where("id = ? AND status != -1", id).First(&user).Error
|
||||
return handleNotFoundResult(&user, err)
|
||||
}
|
||||
|
||||
// FindUserByUsername 根据用户名查找用户
|
||||
func FindUserByUsername(username string) (*model.User, error) {
|
||||
func (r *userRepository) FindByUsername(username string) (*model.User, error) {
|
||||
var user model.User
|
||||
err := getDB().Where("username = ? AND status != -1", username).First(&user).Error
|
||||
return HandleNotFound(&user, err)
|
||||
err := r.db.Where("username = ? AND status != -1", username).First(&user).Error
|
||||
return handleNotFoundResult(&user, err)
|
||||
}
|
||||
|
||||
// FindUserByEmail 根据邮箱查找用户
|
||||
func FindUserByEmail(email string) (*model.User, error) {
|
||||
func (r *userRepository) FindByEmail(email string) (*model.User, error) {
|
||||
var user model.User
|
||||
err := getDB().Where("email = ? AND status != -1", email).First(&user).Error
|
||||
return HandleNotFound(&user, err)
|
||||
err := r.db.Where("email = ? AND status != -1", email).First(&user).Error
|
||||
return handleNotFoundResult(&user, err)
|
||||
}
|
||||
|
||||
// UpdateUser 更新用户
|
||||
func UpdateUser(user *model.User) error {
|
||||
return getDB().Save(user).Error
|
||||
func (r *userRepository) Update(user *model.User) error {
|
||||
return r.db.Save(user).Error
|
||||
}
|
||||
|
||||
// UpdateUserFields 更新指定字段
|
||||
func UpdateUserFields(id int64, fields map[string]interface{}) error {
|
||||
return getDB().Model(&model.User{}).Where("id = ?", id).Updates(fields).Error
|
||||
func (r *userRepository) UpdateFields(id int64, fields map[string]interface{}) error {
|
||||
return r.db.Model(&model.User{}).Where("id = ?", id).Updates(fields).Error
|
||||
}
|
||||
|
||||
// DeleteUser 软删除用户
|
||||
func DeleteUser(id int64) error {
|
||||
return getDB().Model(&model.User{}).Where("id = ?", id).Update("status", -1).Error
|
||||
func (r *userRepository) Delete(id int64) error {
|
||||
return r.db.Model(&model.User{}).Where("id = ?", id).Update("status", -1).Error
|
||||
}
|
||||
|
||||
// CreateLoginLog 创建登录日志
|
||||
func CreateLoginLog(log *model.UserLoginLog) error {
|
||||
return getDB().Create(log).Error
|
||||
func (r *userRepository) CreateLoginLog(log *model.UserLoginLog) error {
|
||||
return r.db.Create(log).Error
|
||||
}
|
||||
|
||||
// CreatePointLog 创建积分日志
|
||||
func CreatePointLog(log *model.UserPointLog) error {
|
||||
return getDB().Create(log).Error
|
||||
func (r *userRepository) CreatePointLog(log *model.UserPointLog) error {
|
||||
return r.db.Create(log).Error
|
||||
}
|
||||
|
||||
// UpdateUserPoints 更新用户积分(事务)
|
||||
func UpdateUserPoints(userID int64, amount int, changeType, reason string) error {
|
||||
return getDB().Transaction(func(tx *gorm.DB) 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
|
||||
@@ -90,12 +90,13 @@ func UpdateUserPoints(userID int64, amount int, changeType, reason string) error
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateUserAvatar 更新用户头像
|
||||
func UpdateUserAvatar(userID int64, avatarURL string) error {
|
||||
return getDB().Model(&model.User{}).Where("id = ?", userID).Update("avatar", avatarURL).Error
|
||||
}
|
||||
|
||||
// UpdateUserEmail 更新用户邮箱
|
||||
func UpdateUserEmail(userID int64, email string) error {
|
||||
return getDB().Model(&model.User{}).Where("id = ?", userID).Update("email", email).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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user