Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/modules/food/DTO/food.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -22,7 +32,7 @@ export class FoodDto {
@IsOptional()
@IsArray()
@IsNumber({}, { each: true })
ingredients?: number[];
ingredients?: IngredientDto[];

@IsOptional()
@IsArray()
Expand Down
12 changes: 10 additions & 2 deletions src/modules/food/food.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
};

Expand Down Expand Up @@ -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],
};

Expand Down
4 changes: 2 additions & 2 deletions src/modules/food/food.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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) {
Expand Down
11 changes: 9 additions & 2 deletions src/modules/food/food.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
},
},
},
Expand Down