diff --git a/src/modules/food/DTO/food.dto.ts b/src/modules/food/DTO/food.dto.ts index 4e6279a..9fd3bf8 100644 --- a/src/modules/food/DTO/food.dto.ts +++ b/src/modules/food/DTO/food.dto.ts @@ -6,6 +6,16 @@ import { IsOptional, } from 'class-validator'; +export class IngredientDto { + @IsNumber() + @IsNotEmpty() + id_ingredient: number; + + @IsNumber() + @IsNotEmpty() + quantity: number; +} + export class FoodDto { @IsString() @IsNotEmpty() @@ -22,7 +32,7 @@ export class FoodDto { @IsOptional() @IsArray() @IsNumber({}, { each: true }) - ingredients?: number[]; + ingredients?: IngredientDto[]; @IsOptional() @IsArray() diff --git a/src/modules/food/food.controller.spec.ts b/src/modules/food/food.controller.spec.ts index 243cb89..c99806c 100644 --- a/src/modules/food/food.controller.spec.ts +++ b/src/modules/food/food.controller.spec.ts @@ -117,7 +117,11 @@ describe('FoodController', () => { name: 'New Pizza', price: 12, id_category: 1, - ingredients: [1, 2, 3], + ingredients: [ + { id_ingredient: 1, quantity: 1 }, + { id_ingredient: 2, quantity: 2 }, + { id_ingredient: 3, quantity: 3 }, + ], details: [1, 2], }; @@ -155,7 +159,11 @@ describe('FoodController', () => { name: 'Updated Pizza', price: 15, id_category: 1, - ingredients: [1, 2, 3], + ingredients: [ + { id_ingredient: 1, quantity: 1 }, + { id_ingredient: 2, quantity: 2 }, + { id_ingredient: 3, quantity: 3 }, + ], details: [1, 2], }; diff --git a/src/modules/food/food.controller.ts b/src/modules/food/food.controller.ts index 28cdf77..ec72141 100644 --- a/src/modules/food/food.controller.ts +++ b/src/modules/food/food.controller.ts @@ -98,7 +98,7 @@ export class FoodController { if (!food) { throw new NotFoundException(); } - return food[0].food; // Assuming food.foods is of type any[] + return food[0].food; } else { const food = await this.foodService.findById( Number(idRestaurant), @@ -107,7 +107,7 @@ export class FoodController { if (!food) { throw new NotFoundException(); } - return food.foods[0]; // Assuming food.foods is of type any[] + return food.foods[0]; } } catch (error) { if (error instanceof HttpException) { diff --git a/src/modules/food/food.service.ts b/src/modules/food/food.service.ts index 393615f..9fff751 100644 --- a/src/modules/food/food.service.ts +++ b/src/modules/food/food.service.ts @@ -102,9 +102,16 @@ export class FoodService extends DB { ingredients: { $map: { input: '$foods.ingredients', - as: 'ingredientId', + as: 'ingredient', in: { - $arrayElemAt: ['$ingredients.name', '$$ingredientId'], + id_ingredient: '$$ingredient.id_ingredient', + quantity: '$$ingredient.quantity', + name: { + $arrayElemAt: [ + '$ingredients.name', + '$$ingredient.id_ingredient', + ], // Assuming 'ingredients' is the array of ingredients + }, }, }, },