refactor: Update service and repository methods to use context

- Refactored multiple service and repository methods to accept context as a parameter, enhancing consistency and enabling better control over request lifecycles.
- Updated handlers to utilize context in method calls, improving error handling and performance.
- Cleaned up Dockerfile by removing unnecessary whitespace.
This commit is contained in:
lan
2025-12-03 15:27:12 +08:00
parent 4824a997dd
commit 0bcd9336c4
32 changed files with 833 additions and 497 deletions

View File

@@ -19,13 +19,13 @@ 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) Create(ctx context.Context, profile *model.Profile) error {
return r.db.WithContext(ctx).Create(profile).Error
}
func (r *profileRepository) FindByUUID(uuid string) (*model.Profile, error) {
func (r *profileRepository) FindByUUID(ctx context.Context, uuid string) (*model.Profile, error) {
var profile model.Profile
err := r.db.Where("uuid = ?", uuid).
err := r.db.WithContext(ctx).Where("uuid = ?", uuid).
Preload("Skin").
Preload("Cape").
First(&profile).Error
@@ -35,10 +35,10 @@ func (r *profileRepository) FindByUUID(uuid string) (*model.Profile, error) {
return &profile, nil
}
func (r *profileRepository) FindByName(name string) (*model.Profile, error) {
func (r *profileRepository) FindByName(ctx context.Context, name string) (*model.Profile, error) {
var profile model.Profile
// 使用 LOWER 函数进行不区分大小写的查询,并预加载 Skin 和 Cape
err := r.db.Where("LOWER(name) = LOWER(?)", name).
err := r.db.WithContext(ctx).Where("LOWER(name) = LOWER(?)", name).
Preload("Skin").
Preload("Cape").
First(&profile).Error
@@ -48,9 +48,9 @@ func (r *profileRepository) FindByName(name string) (*model.Profile, error) {
return &profile, nil
}
func (r *profileRepository) FindByUserID(userID int64) ([]*model.Profile, error) {
func (r *profileRepository) FindByUserID(ctx context.Context, userID int64) ([]*model.Profile, error) {
var profiles []*model.Profile
err := r.db.Where("user_id = ?", userID).
err := r.db.WithContext(ctx).Where("user_id = ?", userID).
Preload("Skin").
Preload("Cape").
Order("created_at DESC").
@@ -58,30 +58,59 @@ func (r *profileRepository) FindByUserID(userID int64) ([]*model.Profile, error)
return profiles, err
}
func (r *profileRepository) Update(profile *model.Profile) error {
return r.db.Save(profile).Error
func (r *profileRepository) FindByUUIDs(ctx context.Context, uuids []string) ([]*model.Profile, error) {
if len(uuids) == 0 {
return []*model.Profile{}, nil
}
var profiles []*model.Profile
// 使用 IN 查询优化批量查询,并预加载关联
err := r.db.WithContext(ctx).Where("uuid IN ?", uuids).
Preload("Skin").
Preload("Cape").
Find(&profiles).Error
return profiles, err
}
func (r *profileRepository) UpdateFields(uuid string, updates map[string]interface{}) error {
return r.db.Model(&model.Profile{}).
func (r *profileRepository) Update(ctx context.Context, profile *model.Profile) error {
return r.db.WithContext(ctx).Save(profile).Error
}
func (r *profileRepository) UpdateFields(ctx context.Context, uuid string, updates map[string]interface{}) error {
return r.db.WithContext(ctx).Model(&model.Profile{}).
Where("uuid = ?", uuid).
Updates(updates).Error
}
func (r *profileRepository) Delete(uuid string) error {
return r.db.Where("uuid = ?", uuid).Delete(&model.Profile{}).Error
func (r *profileRepository) Delete(ctx context.Context, uuid string) error {
return r.db.WithContext(ctx).Where("uuid = ?", uuid).Delete(&model.Profile{}).Error
}
func (r *profileRepository) CountByUserID(userID int64) (int64, error) {
func (r *profileRepository) BatchUpdate(ctx context.Context, uuids []string, updates map[string]interface{}) (int64, error) {
if len(uuids) == 0 {
return 0, nil
}
result := r.db.WithContext(ctx).Model(&model.Profile{}).Where("uuid IN ?", uuids).Updates(updates)
return result.RowsAffected, result.Error
}
func (r *profileRepository) BatchDelete(ctx context.Context, uuids []string) (int64, error) {
if len(uuids) == 0 {
return 0, nil
}
result := r.db.WithContext(ctx).Where("uuid IN ?", uuids).Delete(&model.Profile{})
return result.RowsAffected, result.Error
}
func (r *profileRepository) CountByUserID(ctx context.Context, userID int64) (int64, error) {
var count int64
err := r.db.Model(&model.Profile{}).
err := r.db.WithContext(ctx).Model(&model.Profile{}).
Where("user_id = ?", userID).
Count(&count).Error
return count, err
}
func (r *profileRepository) SetActive(uuid string, userID int64) error {
return r.db.Transaction(func(tx *gorm.DB) error {
func (r *profileRepository) SetActive(ctx context.Context, uuid string, userID int64) error {
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Model(&model.Profile{}).
Where("user_id = ?", userID).
Update("is_active", false).Error; err != nil {
@@ -94,28 +123,28 @@ func (r *profileRepository) SetActive(uuid string, userID int64) error {
})
}
func (r *profileRepository) UpdateLastUsedAt(uuid string) error {
return r.db.Model(&model.Profile{}).
func (r *profileRepository) UpdateLastUsedAt(ctx context.Context, uuid string) error {
return r.db.WithContext(ctx).Model(&model.Profile{}).
Where("uuid = ?", uuid).
Update("last_used_at", gorm.Expr("CURRENT_TIMESTAMP")).Error
}
func (r *profileRepository) GetByNames(names []string) ([]*model.Profile, error) {
func (r *profileRepository) GetByNames(ctx context.Context, names []string) ([]*model.Profile, error) {
var profiles []*model.Profile
err := r.db.Where("name in (?)", names).
err := r.db.WithContext(ctx).Where("name in (?)", names).
Preload("Skin").
Preload("Cape").
Find(&profiles).Error
return profiles, err
}
func (r *profileRepository) GetKeyPair(profileId string) (*model.KeyPair, error) {
func (r *profileRepository) GetKeyPair(ctx context.Context, profileId string) (*model.KeyPair, error) {
if profileId == "" {
return nil, errors.New("参数不能为空")
}
var profile model.Profile
result := r.db.WithContext(context.Background()).
result := r.db.WithContext(ctx).
Select("key_pair").
Where("id = ?", profileId).
First(&profile)
@@ -130,7 +159,7 @@ func (r *profileRepository) GetKeyPair(profileId string) (*model.KeyPair, error)
return &model.KeyPair{}, nil
}
func (r *profileRepository) UpdateKeyPair(profileId string, keyPair *model.KeyPair) error {
func (r *profileRepository) UpdateKeyPair(ctx context.Context, profileId string, keyPair *model.KeyPair) error {
if profileId == "" {
return errors.New("profileId 不能为空")
}
@@ -138,9 +167,8 @@ func (r *profileRepository) UpdateKeyPair(profileId string, keyPair *model.KeyPa
return errors.New("keyPair 不能为 nil")
}
return r.db.Transaction(func(tx *gorm.DB) error {
result := tx.WithContext(context.Background()).
Table("profiles").
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
result := tx.Table("profiles").
Where("id = ?", profileId).
UpdateColumns(map[string]interface{}{
"private_key": keyPair.PrivateKey,