配方领域层方法+重构配方领域
This commit is contained in:
@@ -25,8 +25,11 @@ type RecipeRepository interface {
|
||||
GetRecipeByName(ctx context.Context, name string) (*models.Recipe, error)
|
||||
ListRecipes(ctx context.Context, opts RecipeListOptions, page, pageSize int) ([]models.Recipe, int64, error)
|
||||
UpdateRecipe(ctx context.Context, recipe *models.Recipe) error
|
||||
UpdateRecipeIngredients(ctx context.Context, recipeID uint32, ingredients []models.RecipeIngredient) error
|
||||
DeleteRecipe(ctx context.Context, id uint32) error
|
||||
// 在事务中删除配方原料
|
||||
DeleteRecipeIngredientsByRecipeIDTx(ctx context.Context, tx *gorm.DB, recipeID uint32) error
|
||||
// 在事务中批量创建配方原料
|
||||
CreateBatchRecipeIngredientsTx(ctx context.Context, tx *gorm.DB, ingredients []models.RecipeIngredient) error
|
||||
}
|
||||
|
||||
// gormRecipeRepository 是 RecipeRepository 的 GORM 实现
|
||||
@@ -43,12 +46,12 @@ func NewGormRecipeRepository(ctx context.Context, db *gorm.DB) RecipeRepository
|
||||
// CreateRecipe 创建一个新的配方,并处理其关联的配方原料
|
||||
func (r *gormRecipeRepository) CreateRecipe(ctx context.Context, recipe *models.Recipe) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreateRecipe")
|
||||
return r.db.WithContext(repoCtx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(recipe).Error; err != nil {
|
||||
return fmt.Errorf("创建配方失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
// 注意:这里的事务只针对 Recipe 本身,如果 RecipeIngredient 也在同一个 Create 中,GORM 会自动处理。
|
||||
// 但如果 RecipeIngredient 是单独操作,则需要外部事务。
|
||||
if err := r.db.WithContext(repoCtx).Create(recipe).Error; err != nil {
|
||||
return fmt.Errorf("创建配方失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetRecipeByID 根据ID获取单个配方,并预加载其关联的配方原料和原料信息
|
||||
@@ -130,29 +133,6 @@ func (r *gormRecipeRepository) UpdateRecipe(ctx context.Context, recipe *models.
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateRecipeIngredients 更新配方关联的原料列表
|
||||
func (r *gormRecipeRepository) UpdateRecipeIngredients(ctx context.Context, recipeID uint32, ingredients []models.RecipeIngredient) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdateRecipeIngredients")
|
||||
|
||||
return r.db.WithContext(repoCtx).Transaction(func(tx *gorm.DB) error {
|
||||
// 1. 删除所有旧的关联配方原料
|
||||
if err := tx.Where("recipe_id = ?", recipeID).Delete(&models.RecipeIngredient{}).Error; err != nil {
|
||||
return fmt.Errorf("删除旧的配方原料失败: %w", err)
|
||||
}
|
||||
|
||||
// 2. 批量创建新的关联配方原料
|
||||
if len(ingredients) > 0 {
|
||||
for i := range ingredients {
|
||||
ingredients[i].RecipeID = recipeID
|
||||
}
|
||||
if err := tx.Create(&ingredients).Error; err != nil {
|
||||
return fmt.Errorf("创建新的配方原料失败: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteRecipe 根据ID删除一个配方,并级联软删除关联的 RecipeIngredient 记录
|
||||
func (r *gormRecipeRepository) DeleteRecipe(ctx context.Context, id uint32) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "DeleteRecipe")
|
||||
@@ -180,3 +160,31 @@ func (r *gormRecipeRepository) DeleteRecipe(ctx context.Context, id uint32) erro
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteRecipeIngredientsByRecipeIDTx 在给定事务中删除配方原料
|
||||
func (r *gormRecipeRepository) DeleteRecipeIngredientsByRecipeIDTx(ctx context.Context, tx *gorm.DB, recipeID uint32) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "DeleteRecipeIngredientsByRecipeIDTx")
|
||||
if err := tx.WithContext(repoCtx).Where("recipe_id = ?", recipeID).Delete(&models.RecipeIngredient{}).Error; err != nil {
|
||||
return fmt.Errorf("删除配方 %d 的原料失败: %w", recipeID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateBatchRecipeIngredientsTx 在给定事务中批量创建配方原料
|
||||
func (r *gormRecipeRepository) CreateBatchRecipeIngredientsTx(ctx context.Context, tx *gorm.DB, ingredients []models.RecipeIngredient) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreateBatchRecipeIngredientsTx")
|
||||
if len(ingredients) == 0 {
|
||||
return nil // 没有原料需要创建
|
||||
}
|
||||
// 确保每个原料都关联到正确的配方ID
|
||||
// 注意:这里假设传入的 ingredients 已经设置了正确的 RecipeID
|
||||
for i := range ingredients {
|
||||
if ingredients[i].RecipeID == 0 {
|
||||
return fmt.Errorf("创建配方原料时 RecipeID 不能为空")
|
||||
}
|
||||
}
|
||||
if err := tx.WithContext(repoCtx).Create(&ingredients).Error; err != nil {
|
||||
return fmt.Errorf("批量创建配方原料失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user