实现 StartUpgrade
This commit is contained in:
@@ -14,12 +14,16 @@ import (
|
||||
type OtaRepository interface {
|
||||
// Create 创建一个新的 OTA 任务。
|
||||
Create(ctx context.Context, task *models.OTATask) error
|
||||
// CreateTx 在指定的事务中创建一个新的 OTA 任务。
|
||||
CreateTx(ctx context.Context, tx *gorm.DB, task *models.OTATask) error
|
||||
// FindByID 根据任务 ID 查找任务。
|
||||
FindByID(ctx context.Context, id uint32) (*models.OTATask, error)
|
||||
// FindTasksByStatusesAndCreationTime 根据状态列表和创建时间查找任务。
|
||||
FindTasksByStatusesAndCreationTime(ctx context.Context, statuses []models.OTATaskStatus, createdBefore time.Time) ([]*models.OTATask, error)
|
||||
// Update 更新单个 OTA 任务。
|
||||
Update(ctx context.Context, task *models.OTATask) error
|
||||
// UpdateTx 在指定的事务中更新单个 OTA 任务。
|
||||
UpdateTx(ctx context.Context, tx *gorm.DB, task *models.OTATask) error
|
||||
}
|
||||
|
||||
// gormOtaRepository 是 OtaRepository 的 GORM 实现
|
||||
@@ -36,10 +40,17 @@ func NewGormOtaRepository(ctx context.Context, db *gorm.DB) OtaRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// Create 实现了创建新 OTA 任务的逻辑。
|
||||
// Create 实现了创建新 OTA 任务的逻辑,内部调用 CreateTx 以复用代码。
|
||||
func (r *gormOtaRepository) Create(ctx context.Context, task *models.OTATask) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "Create")
|
||||
return r.db.WithContext(repoCtx).Create(task).Error
|
||||
// 使用 r.db 作为事务对象,调用通用的事务方法
|
||||
return r.CreateTx(repoCtx, r.db, task)
|
||||
}
|
||||
|
||||
// CreateTx 实现了在事务中创建新 OTA 任务的核心逻辑。
|
||||
func (r *gormOtaRepository) CreateTx(ctx context.Context, tx *gorm.DB, task *models.OTATask) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreateTx")
|
||||
return tx.WithContext(repoCtx).Create(task).Error
|
||||
}
|
||||
|
||||
// FindByID 实现了根据 ID 查找任务的逻辑。
|
||||
@@ -47,7 +58,10 @@ func (r *gormOtaRepository) FindByID(ctx context.Context, id uint32) (*models.OT
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindByID")
|
||||
var task models.OTATask
|
||||
err := r.db.WithContext(repoCtx).First(&task, id).Error
|
||||
return &task, err
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &task, nil
|
||||
}
|
||||
|
||||
// FindTasksByStatusesAndCreationTime 实现了根据状态和创建时间查找任务的逻辑。
|
||||
@@ -63,8 +77,15 @@ func (r *gormOtaRepository) FindTasksByStatusesAndCreationTime(ctx context.Conte
|
||||
return tasks, err
|
||||
}
|
||||
|
||||
// Update 实现了更新单个 OTA 任务的逻辑。
|
||||
// Update 实现了更新单个 OTA 任务的逻辑,内部调用 UpdateTx 以复用代码。
|
||||
func (r *gormOtaRepository) Update(ctx context.Context, task *models.OTATask) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "Update")
|
||||
return r.db.WithContext(repoCtx).Save(task).Error
|
||||
// 使用 r.db 作为事务对象,调用通用的事务方法
|
||||
return r.UpdateTx(repoCtx, r.db, task)
|
||||
}
|
||||
|
||||
// UpdateTx 实现了在事务中更新单个 OTA 任务的核心逻辑。
|
||||
func (r *gormOtaRepository) UpdateTx(ctx context.Context, tx *gorm.DB, task *models.OTATask) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdateTx")
|
||||
return tx.WithContext(repoCtx).Save(task).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user