149 lines
3.8 KiB
Go
149 lines
3.8 KiB
Go
|
|
package repository
|
||
|
|
|
||
|
|
import (
|
||
|
|
"carrotskin/internal/model"
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// profileRepositoryImpl ProfileRepository的实现
|
||
|
|
type profileRepositoryImpl struct {
|
||
|
|
db *gorm.DB
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewProfileRepository 创建ProfileRepository实例
|
||
|
|
func NewProfileRepository(db *gorm.DB) ProfileRepository {
|
||
|
|
return &profileRepositoryImpl{db: db}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *profileRepositoryImpl) Create(profile *model.Profile) error {
|
||
|
|
return r.db.Create(profile).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *profileRepositoryImpl) 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 *profileRepositoryImpl) 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 *profileRepositoryImpl) 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 *profileRepositoryImpl) Update(profile *model.Profile) error {
|
||
|
|
return r.db.Save(profile).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *profileRepositoryImpl) UpdateFields(uuid string, updates map[string]interface{}) error {
|
||
|
|
return r.db.Model(&model.Profile{}).
|
||
|
|
Where("uuid = ?", uuid).
|
||
|
|
Updates(updates).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *profileRepositoryImpl) Delete(uuid string) error {
|
||
|
|
return r.db.Where("uuid = ?", uuid).Delete(&model.Profile{}).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *profileRepositoryImpl) 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 *profileRepositoryImpl) 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 *profileRepositoryImpl) UpdateLastUsedAt(uuid string) error {
|
||
|
|
return r.db.Model(&model.Profile{}).
|
||
|
|
Where("uuid = ?", uuid).
|
||
|
|
Update("last_used_at", gorm.Expr("CURRENT_TIMESTAMP")).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *profileRepositoryImpl) 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 *profileRepositoryImpl) 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 *profileRepositoryImpl) 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
|
||
|
|
})
|
||
|
|
}
|