拓展接口响应

This commit is contained in:
2025-11-21 16:37:09 +08:00
parent 534891309c
commit 4224be8567
5 changed files with 115 additions and 30 deletions

View File

@@ -9,12 +9,24 @@ func ConvertNutrientToDTO(nutrient *models.Nutrient) *NutrientResponse {
if nutrient == nil {
return nil
}
rawMaterials := make([]NutrientRawMaterialDTO, 0, len(nutrient.RawMaterialNutrients))
for _, rmn := range nutrient.RawMaterialNutrients {
// 根据您的反馈,移除了不必要的 nil 检查,以保持代码简洁和一致性
rawMaterials = append(rawMaterials, NutrientRawMaterialDTO{
ID: rmn.RawMaterial.ID,
Name: rmn.RawMaterial.Name,
Value: rmn.Value,
})
}
return &NutrientResponse{
ID: nutrient.ID,
CreatedAt: nutrient.CreatedAt,
UpdatedAt: nutrient.UpdatedAt,
Name: nutrient.Name,
Description: nutrient.Description,
ID: nutrient.ID,
CreatedAt: nutrient.CreatedAt,
UpdatedAt: nutrient.UpdatedAt,
Name: nutrient.Name,
Description: nutrient.Description,
RawMaterials: rawMaterials,
}
}

View File

@@ -20,13 +20,21 @@ type UpdateNutrientRequest struct {
Description string `json:"description" validate:"max=255"` // 描述
}
// NutrientRawMaterialDTO 用于在营养素信息中展示关联的原料及其含量
type NutrientRawMaterialDTO struct {
ID uint32 `json:"id"` // 原料ID
Name string `json:"name"` // 原料名称
Value float32 `json:"value"` // 该原料中此营养素的含量
}
// NutrientResponse 营养种类响应体
type NutrientResponse struct {
ID uint32 `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
Description string `json:"description"`
ID uint32 `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
Description string `json:"description"`
RawMaterials []NutrientRawMaterialDTO `json:"raw_materials"` // 包含此营养的原料列表
}
// ListNutrientRequest 定义了获取营养种类列表的请求参数