新增 ClientRepository 接口,用于管理客户端相关操作。 更新 Token 模型,加入版本号和过期时间字段,以提升令牌管理能力。 将 ClientRepo 集成到容器中,支持依赖注入。 重构 TokenService,采用 JWT 以增强安全性。 更新 Docker 配置,并清理多个文件中的空白字符。
64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"carrotskin/internal/model"
|
|
|
|
"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(client *model.Client) error {
|
|
return r.db.Create(client).Error
|
|
}
|
|
|
|
func (r *clientRepository) FindByClientToken(clientToken string) (*model.Client, error) {
|
|
var client model.Client
|
|
err := r.db.Where("client_token = ?", clientToken).First(&client).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &client, nil
|
|
}
|
|
|
|
func (r *clientRepository) FindByUUID(uuid string) (*model.Client, error) {
|
|
var client model.Client
|
|
err := r.db.Where("uuid = ?", uuid).First(&client).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &client, nil
|
|
}
|
|
|
|
func (r *clientRepository) FindByUserID(userID int64) ([]*model.Client, error) {
|
|
var clients []*model.Client
|
|
err := r.db.Where("user_id = ?", userID).Find(&clients).Error
|
|
return clients, err
|
|
}
|
|
|
|
func (r *clientRepository) Update(client *model.Client) error {
|
|
return r.db.Save(client).Error
|
|
}
|
|
|
|
func (r *clientRepository) IncrementVersion(clientUUID string) error {
|
|
return r.db.Model(&model.Client{}).
|
|
Where("uuid = ?", clientUUID).
|
|
Update("version", gorm.Expr("version + 1")).Error
|
|
}
|
|
|
|
func (r *clientRepository) DeleteByClientToken(clientToken string) error {
|
|
return r.db.Where("client_token = ?", clientToken).Delete(&model.Client{}).Error
|
|
}
|
|
|
|
func (r *clientRepository) DeleteByUserID(userID int64) error {
|
|
return r.db.Where("user_id = ?", userID).Delete(&model.Client{}).Error
|
|
}
|