配方增删改查服务层和控制器

This commit is contained in:
2025-11-24 13:25:15 +08:00
parent 1200f36d14
commit d7deaa346b
13 changed files with 1411 additions and 1 deletions

View File

@@ -198,3 +198,84 @@ func ConvertPigTypeListToDTO(pigTypes []models.PigType, total int64, page, pageS
},
}
}
// ConvertRecipeToDto 将 models.Recipe 转换为 RecipeResponse DTO
func ConvertRecipeToDto(recipe *models.Recipe) *RecipeResponse {
if recipe == nil {
return nil
}
ingredients := make([]RecipeIngredientDto, len(recipe.RecipeIngredients))
for i, ri := range recipe.RecipeIngredients {
ingredients[i] = RecipeIngredientDto{
RawMaterialID: ri.RawMaterialID,
Percentage: ri.Percentage,
}
}
return &RecipeResponse{
ID: recipe.ID,
Name: recipe.Name,
Description: recipe.Description,
RecipeIngredients: ingredients,
}
}
// ConvertRecipeListToDTO 将 []models.Recipe 转换为 ListRecipeResponse DTO
func ConvertRecipeListToDTO(recipes []models.Recipe, total int64, page, pageSize int) *ListRecipeResponse {
recipeDTOs := make([]RecipeResponse, len(recipes))
for i, r := range recipes {
recipeDTOs[i] = *ConvertRecipeToDto(&r)
}
return &ListRecipeResponse{
List: recipeDTOs,
Pagination: PaginationDTO{
Page: page,
PageSize: pageSize,
Total: total,
},
}
}
// ConvertCreateRecipeRequestToModel 将 CreateRecipeRequest DTO 转换为 models.Recipe 模型
func ConvertCreateRecipeRequestToModel(req *CreateRecipeRequest) *models.Recipe {
if req == nil {
return nil
}
ingredients := make([]models.RecipeIngredient, len(req.RecipeIngredients))
for i, ri := range req.RecipeIngredients {
ingredients[i] = models.RecipeIngredient{
RawMaterialID: ri.RawMaterialID,
Percentage: ri.Percentage,
}
}
return &models.Recipe{
Name: req.Name,
Description: req.Description,
RecipeIngredients: ingredients,
}
}
// ConvertUpdateRecipeRequestToModel 将 UpdateRecipeRequest DTO 转换为 models.Recipe 模型
func ConvertUpdateRecipeRequestToModel(req *UpdateRecipeRequest) *models.Recipe {
if req == nil {
return nil
}
ingredients := make([]models.RecipeIngredient, len(req.RecipeIngredients))
for i, ri := range req.RecipeIngredients {
ingredients[i] = models.RecipeIngredient{
RawMaterialID: ri.RawMaterialID,
Percentage: ri.Percentage,
}
}
return &models.Recipe{
Name: req.Name,
Description: req.Description,
RecipeIngredients: ingredients,
}
}