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:
@@ -9,15 +9,23 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CreateProfile 创建档案
|
||||
func CreateProfile(profile *model.Profile) error {
|
||||
return getDB().Create(profile).Error
|
||||
// profileRepository ProfileRepository的实现
|
||||
type profileRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// FindProfileByUUID 根据UUID查找档案
|
||||
func FindProfileByUUID(uuid string) (*model.Profile, error) {
|
||||
// NewProfileRepository 创建ProfileRepository实例
|
||||
func NewProfileRepository(db *gorm.DB) ProfileRepository {
|
||||
return &profileRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *profileRepository) Create(profile *model.Profile) error {
|
||||
return r.db.Create(profile).Error
|
||||
}
|
||||
|
||||
func (r *profileRepository) FindByUUID(uuid string) (*model.Profile, error) {
|
||||
var profile model.Profile
|
||||
err := getDB().Where("uuid = ?", uuid).
|
||||
err := r.db.Where("uuid = ?", uuid).
|
||||
Preload("Skin").
|
||||
Preload("Cape").
|
||||
First(&profile).Error
|
||||
@@ -27,20 +35,18 @@ func FindProfileByUUID(uuid string) (*model.Profile, error) {
|
||||
return &profile, nil
|
||||
}
|
||||
|
||||
// FindProfileByName 根据角色名查找档案
|
||||
func FindProfileByName(name string) (*model.Profile, error) {
|
||||
func (r *profileRepository) FindByName(name string) (*model.Profile, error) {
|
||||
var profile model.Profile
|
||||
err := getDB().Where("name = ?", name).First(&profile).Error
|
||||
err := r.db.Where("name = ?", name).First(&profile).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &profile, nil
|
||||
}
|
||||
|
||||
// FindProfilesByUserID 获取用户的所有档案
|
||||
func FindProfilesByUserID(userID int64) ([]*model.Profile, error) {
|
||||
func (r *profileRepository) FindByUserID(userID int64) ([]*model.Profile, error) {
|
||||
var profiles []*model.Profile
|
||||
err := getDB().Where("user_id = ?", userID).
|
||||
err := r.db.Where("user_id = ?", userID).
|
||||
Preload("Skin").
|
||||
Preload("Cape").
|
||||
Order("created_at DESC").
|
||||
@@ -48,35 +54,30 @@ func FindProfilesByUserID(userID int64) ([]*model.Profile, error) {
|
||||
return profiles, err
|
||||
}
|
||||
|
||||
// UpdateProfile 更新档案
|
||||
func UpdateProfile(profile *model.Profile) error {
|
||||
return getDB().Save(profile).Error
|
||||
func (r *profileRepository) Update(profile *model.Profile) error {
|
||||
return r.db.Save(profile).Error
|
||||
}
|
||||
|
||||
// UpdateProfileFields 更新指定字段
|
||||
func UpdateProfileFields(uuid string, updates map[string]interface{}) error {
|
||||
return getDB().Model(&model.Profile{}).
|
||||
func (r *profileRepository) UpdateFields(uuid string, updates map[string]interface{}) error {
|
||||
return r.db.Model(&model.Profile{}).
|
||||
Where("uuid = ?", uuid).
|
||||
Updates(updates).Error
|
||||
}
|
||||
|
||||
// DeleteProfile 删除档案
|
||||
func DeleteProfile(uuid string) error {
|
||||
return getDB().Where("uuid = ?", uuid).Delete(&model.Profile{}).Error
|
||||
func (r *profileRepository) Delete(uuid string) error {
|
||||
return r.db.Where("uuid = ?", uuid).Delete(&model.Profile{}).Error
|
||||
}
|
||||
|
||||
// CountProfilesByUserID 统计用户的档案数量
|
||||
func CountProfilesByUserID(userID int64) (int64, error) {
|
||||
func (r *profileRepository) CountByUserID(userID int64) (int64, error) {
|
||||
var count int64
|
||||
err := getDB().Model(&model.Profile{}).
|
||||
err := r.db.Model(&model.Profile{}).
|
||||
Where("user_id = ?", userID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
// SetActiveProfile 设置档案为活跃状态(同时将用户的其他档案设置为非活跃)
|
||||
func SetActiveProfile(uuid string, userID int64) error {
|
||||
return getDB().Transaction(func(tx *gorm.DB) error {
|
||||
func (r *profileRepository) SetActive(uuid string, userID int64) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Model(&model.Profile{}).
|
||||
Where("user_id = ?", userID).
|
||||
Update("is_active", false).Error; err != nil {
|
||||
@@ -89,44 +90,31 @@ func SetActiveProfile(uuid string, userID int64) error {
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateProfileLastUsedAt 更新最后使用时间
|
||||
func UpdateProfileLastUsedAt(uuid string) error {
|
||||
return getDB().Model(&model.Profile{}).
|
||||
func (r *profileRepository) UpdateLastUsedAt(uuid string) error {
|
||||
return r.db.Model(&model.Profile{}).
|
||||
Where("uuid = ?", uuid).
|
||||
Update("last_used_at", gorm.Expr("CURRENT_TIMESTAMP")).Error
|
||||
}
|
||||
|
||||
// FindOneProfileByUserID 根据id找一个角色
|
||||
func FindOneProfileByUserID(userID int64) (*model.Profile, error) {
|
||||
profiles, err := FindProfilesByUserID(userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(profiles) == 0 {
|
||||
return nil, errors.New("未找到角色")
|
||||
}
|
||||
return profiles[0], nil
|
||||
}
|
||||
|
||||
func GetProfilesByNames(names []string) ([]*model.Profile, error) {
|
||||
func (r *profileRepository) GetByNames(names []string) ([]*model.Profile, error) {
|
||||
var profiles []*model.Profile
|
||||
err := getDB().Where("name in (?)", names).Find(&profiles).Error
|
||||
err := r.db.Where("name in (?)", names).Find(&profiles).Error
|
||||
return profiles, err
|
||||
}
|
||||
|
||||
func GetProfileKeyPair(profileId string) (*model.KeyPair, error) {
|
||||
func (r *profileRepository) GetKeyPair(profileId string) (*model.KeyPair, error) {
|
||||
if profileId == "" {
|
||||
return nil, errors.New("参数不能为空")
|
||||
}
|
||||
|
||||
var profile model.Profile
|
||||
result := getDB().WithContext(context.Background()).
|
||||
result := r.db.WithContext(context.Background()).
|
||||
Select("key_pair").
|
||||
Where("id = ?", profileId).
|
||||
First(&profile)
|
||||
|
||||
if result.Error != nil {
|
||||
if IsNotFound(result.Error) {
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.New("key pair未找到")
|
||||
}
|
||||
return nil, fmt.Errorf("获取key pair失败: %w", result.Error)
|
||||
@@ -135,7 +123,7 @@ func GetProfileKeyPair(profileId string) (*model.KeyPair, error) {
|
||||
return &model.KeyPair{}, nil
|
||||
}
|
||||
|
||||
func UpdateProfileKeyPair(profileId string, keyPair *model.KeyPair) error {
|
||||
func (r *profileRepository) UpdateKeyPair(profileId string, keyPair *model.KeyPair) error {
|
||||
if profileId == "" {
|
||||
return errors.New("profileId 不能为空")
|
||||
}
|
||||
@@ -143,7 +131,7 @@ func UpdateProfileKeyPair(profileId string, keyPair *model.KeyPair) error {
|
||||
return errors.New("keyPair 不能为 nil")
|
||||
}
|
||||
|
||||
return getDB().Transaction(func(tx *gorm.DB) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
result := tx.WithContext(context.Background()).
|
||||
Table("profiles").
|
||||
Where("id = ?", profileId).
|
||||
|
||||
Reference in New Issue
Block a user