- 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.
65 lines
1.9 KiB
Go
65 lines
1.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"carrotskin/internal/model"
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// clientRepository ClientRepository的实现
|
|
type clientRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewClientRepository 创建ClientRepository实例
|
|
func NewClientRepository(db *gorm.DB) ClientRepository {
|
|
return &clientRepository{db: db}
|
|
}
|
|
|
|
func (r *clientRepository) Create(ctx context.Context, client *model.Client) error {
|
|
return r.db.WithContext(ctx).Create(client).Error
|
|
}
|
|
|
|
func (r *clientRepository) FindByClientToken(ctx context.Context, clientToken string) (*model.Client, error) {
|
|
var client model.Client
|
|
err := r.db.WithContext(ctx).Where("client_token = ?", clientToken).First(&client).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &client, nil
|
|
}
|
|
|
|
func (r *clientRepository) FindByUUID(ctx context.Context, uuid string) (*model.Client, error) {
|
|
var client model.Client
|
|
err := r.db.WithContext(ctx).Where("uuid = ?", uuid).First(&client).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &client, nil
|
|
}
|
|
|
|
func (r *clientRepository) FindByUserID(ctx context.Context, userID int64) ([]*model.Client, error) {
|
|
var clients []*model.Client
|
|
err := r.db.WithContext(ctx).Where("user_id = ?", userID).Find(&clients).Error
|
|
return clients, err
|
|
}
|
|
|
|
func (r *clientRepository) Update(ctx context.Context, client *model.Client) error {
|
|
return r.db.WithContext(ctx).Save(client).Error
|
|
}
|
|
|
|
func (r *clientRepository) IncrementVersion(ctx context.Context, clientUUID string) error {
|
|
return r.db.WithContext(ctx).Model(&model.Client{}).
|
|
Where("uuid = ?", clientUUID).
|
|
Update("version", gorm.Expr("version + 1")).Error
|
|
}
|
|
|
|
func (r *clientRepository) DeleteByClientToken(ctx context.Context, clientToken string) error {
|
|
return r.db.WithContext(ctx).Where("client_token = ?", clientToken).Delete(&model.Client{}).Error
|
|
}
|
|
|
|
func (r *clientRepository) DeleteByUserID(ctx context.Context, userID int64) error {
|
|
return r.db.WithContext(ctx).Where("user_id = ?", userID).Delete(&model.Client{}).Error
|
|
}
|