- 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.
149 lines
3.7 KiB
Go
149 lines
3.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"carrotskin/internal/model"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// profileRepository ProfileRepository的实现
|
|
type profileRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// 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 := r.db.Where("uuid = ?", uuid).
|
|
Preload("Skin").
|
|
Preload("Cape").
|
|
First(&profile).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &profile, nil
|
|
}
|
|
|
|
func (r *profileRepository) FindByName(name string) (*model.Profile, error) {
|
|
var profile model.Profile
|
|
err := r.db.Where("name = ?", name).First(&profile).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &profile, nil
|
|
}
|
|
|
|
func (r *profileRepository) FindByUserID(userID int64) ([]*model.Profile, error) {
|
|
var profiles []*model.Profile
|
|
err := r.db.Where("user_id = ?", userID).
|
|
Preload("Skin").
|
|
Preload("Cape").
|
|
Order("created_at DESC").
|
|
Find(&profiles).Error
|
|
return profiles, err
|
|
}
|
|
|
|
func (r *profileRepository) Update(profile *model.Profile) error {
|
|
return r.db.Save(profile).Error
|
|
}
|
|
|
|
func (r *profileRepository) UpdateFields(uuid string, updates map[string]interface{}) error {
|
|
return r.db.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) CountByUserID(userID int64) (int64, error) {
|
|
var count int64
|
|
err := r.db.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 {
|
|
if err := tx.Model(&model.Profile{}).
|
|
Where("user_id = ?", userID).
|
|
Update("is_active", false).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.Model(&model.Profile{}).
|
|
Where("uuid = ? AND user_id = ?", uuid, userID).
|
|
Update("is_active", true).Error
|
|
})
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (r *profileRepository) GetByNames(names []string) ([]*model.Profile, error) {
|
|
var profiles []*model.Profile
|
|
err := r.db.Where("name in (?)", names).Find(&profiles).Error
|
|
return profiles, err
|
|
}
|
|
|
|
func (r *profileRepository) GetKeyPair(profileId string) (*model.KeyPair, error) {
|
|
if profileId == "" {
|
|
return nil, errors.New("参数不能为空")
|
|
}
|
|
|
|
var profile model.Profile
|
|
result := r.db.WithContext(context.Background()).
|
|
Select("key_pair").
|
|
Where("id = ?", profileId).
|
|
First(&profile)
|
|
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, errors.New("key pair未找到")
|
|
}
|
|
return nil, fmt.Errorf("获取key pair失败: %w", result.Error)
|
|
}
|
|
|
|
return &model.KeyPair{}, nil
|
|
}
|
|
|
|
func (r *profileRepository) UpdateKeyPair(profileId string, keyPair *model.KeyPair) error {
|
|
if profileId == "" {
|
|
return errors.New("profileId 不能为空")
|
|
}
|
|
if keyPair == nil {
|
|
return errors.New("keyPair 不能为 nil")
|
|
}
|
|
|
|
return r.db.Transaction(func(tx *gorm.DB) error {
|
|
result := tx.WithContext(context.Background()).
|
|
Table("profiles").
|
|
Where("id = ?", profileId).
|
|
UpdateColumns(map[string]interface{}{
|
|
"private_key": keyPair.PrivateKey,
|
|
"public_key": keyPair.PublicKey,
|
|
})
|
|
|
|
if result.Error != nil {
|
|
return fmt.Errorf("更新 keyPair 失败: %w", result.Error)
|
|
}
|
|
return nil
|
|
})
|
|
}
|