diff --git a/app/Enums/MealPostType.php b/app/Enums/MealPostType.php index b76340a..f3188b3 100644 --- a/app/Enums/MealPostType.php +++ b/app/Enums/MealPostType.php @@ -10,6 +10,6 @@ enum MealPostType: string case BRUNCH = 'brunch'; case SNACK = 'snack'; case DRINK = 'drink'; - case SUPPLEMENT = "supplement"; - case OTHER = "other"; + case SUPPLEMENT = 'supplement'; + case OTHER = 'other'; } diff --git a/app/Http/Requests/MealPostIngredientsRequest.php b/app/Http/Requests/MealPostIngredientsRequest.php index fa0b13e..f48b56d 100644 --- a/app/Http/Requests/MealPostIngredientsRequest.php +++ b/app/Http/Requests/MealPostIngredientsRequest.php @@ -14,6 +14,7 @@ class MealPostIngredientsRequest extends FormRequest 'meal_posts_id' => ['required', 'exists:meal_posts,id'], 'position' => ['required', 'integer'], 'ingredient' => ['required', 'string'], + 'quantity' => ['required', 'integer', 'min:1'], 'unit' => ['required', Rule::enum(IngredientUnit::class)], ]; } diff --git a/app/Http/Requests/MealPostsRequest.php b/app/Http/Requests/MealPostsRequest.php index 2d3525b..a8d2b6b 100644 --- a/app/Http/Requests/MealPostsRequest.php +++ b/app/Http/Requests/MealPostsRequest.php @@ -27,9 +27,10 @@ class MealPostsRequest extends FormRequest 'type' => [$isCreating ? 'required' : 'sometimes', Rule::enum(MealPostType::class)], 'visibility' => ['sometimes', Rule::enum(MealPostVisibility::class)], 'ingredients' => ['sometimes', 'array'], - 'ingredients.*' => ['required', 'array:position,ingredient,unit'], + 'ingredients.*' => ['required', 'array:position,ingredient,quantity,unit'], 'ingredients.*.position' => ['required', 'integer', 'min:0'], 'ingredients.*.ingredient' => ['required', 'string', 'max:255'], + 'ingredients.*.quantity' => ['required', 'integer', 'min:1'], 'ingredients.*.unit' => ['required', Rule::enum(IngredientUnit::class)], ]; } diff --git a/app/Http/Resources/MealPostIngredientsResource.php b/app/Http/Resources/MealPostIngredientsResource.php index c5980d3..00d0a7e 100644 --- a/app/Http/Resources/MealPostIngredientsResource.php +++ b/app/Http/Resources/MealPostIngredientsResource.php @@ -15,6 +15,7 @@ class MealPostIngredientsResource extends JsonResource 'id' => $this->id, 'position' => $this->position, 'ingredient' => $this->ingredient, + 'quantity' => $this->quantity, 'unit' => $this->unit, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, diff --git a/app/Models/MealPostIngredients.php b/app/Models/MealPostIngredients.php index 619f5a7..7542151 100644 --- a/app/Models/MealPostIngredients.php +++ b/app/Models/MealPostIngredients.php @@ -12,6 +12,7 @@ class MealPostIngredients extends Model 'meal_posts_id', 'position', 'ingredient', + 'quantity', 'unit', ]; @@ -23,6 +24,7 @@ class MealPostIngredients extends Model protected function casts(): array { return [ + 'quantity' => 'integer', 'unit' => IngredientUnit::class, ]; } diff --git a/database/migrations/2026_05_17_171607_add_type_to_meal_posts.php b/database/migrations/2026_05_17_171607_add_type_to_meal_posts.php index f9aa042..8c942ee 100644 --- a/database/migrations/2026_05_17_171607_add_type_to_meal_posts.php +++ b/database/migrations/2026_05_17_171607_add_type_to_meal_posts.php @@ -12,7 +12,7 @@ return new class extends Migration public function up(): void { Schema::table('meal_posts', function (Blueprint $table) { - $table->string("type")->default('breakfast'); + $table->string('type')->default('breakfast'); }); } diff --git a/database/migrations/2026_05_18_124718_add_quantity_to_meal_ingredient.php b/database/migrations/2026_05_18_124718_add_quantity_to_meal_ingredient.php new file mode 100644 index 0000000..cdc66b4 --- /dev/null +++ b/database/migrations/2026_05_18_124718_add_quantity_to_meal_ingredient.php @@ -0,0 +1,28 @@ +unsignedInteger('quantity')->default(1); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('meal_post_ingredients', function (Blueprint $table) { + $table->dropColumn('quantity'); + }); + } +}; diff --git a/tests/Feature/MealPostIngredientsTest.php b/tests/Feature/MealPostIngredientsTest.php index 95199a4..806d542 100644 --- a/tests/Feature/MealPostIngredientsTest.php +++ b/tests/Feature/MealPostIngredientsTest.php @@ -23,11 +23,13 @@ it('creates a meal post with ingredients', function () { [ 'position' => 1, 'ingredient' => 'Riz', + 'quantity' => 150, 'unit' => IngredientUnit::GRAM->value, ], [ 'position' => 2, 'ingredient' => 'Saumon', + 'quantity' => 120, 'unit' => IngredientUnit::GRAM->value, ], ], @@ -36,6 +38,7 @@ it('creates a meal post with ingredients', function () { $response ->assertCreated() ->assertJsonPath('data.ingredients.0.ingredient', 'Riz') + ->assertJsonPath('data.ingredients.0.quantity', 150) ->assertJsonPath('data.ingredients.0.unit', IngredientUnit::GRAM->value) ->assertJsonPath('data.ingredients.1.ingredient', 'Saumon'); @@ -43,6 +46,7 @@ it('creates a meal post with ingredients', function () { 'meal_posts_id' => $response->json('data.id'), 'position' => 1, 'ingredient' => 'Riz', + 'quantity' => 150, 'unit' => IngredientUnit::GRAM->value, ]); }); @@ -59,6 +63,7 @@ it('validates meal post ingredients', function () { [ 'position' => -1, 'ingredient' => '', + 'quantity' => 0, 'unit' => 'cup', ], ], @@ -69,6 +74,7 @@ it('validates meal post ingredients', function () { ->assertJsonValidationErrors([ 'ingredients.0.position', 'ingredients.0.ingredient', + 'ingredients.0.quantity', 'ingredients.0.unit', ]); }); @@ -80,11 +86,13 @@ it('replaces meal post ingredients when updating a meal post', function () { [ 'position' => 1, 'ingredient' => 'Riz', + 'quantity' => 150, 'unit' => IngredientUnit::GRAM, ], [ 'position' => 2, 'ingredient' => 'Saumon', + 'quantity' => 120, 'unit' => IngredientUnit::GRAM, ], ]); @@ -96,11 +104,13 @@ it('replaces meal post ingredients when updating a meal post', function () { [ 'position' => 1, 'ingredient' => 'Pates', + 'quantity' => 200, 'unit' => IngredientUnit::GRAM->value, ], [ 'position' => 2, 'ingredient' => 'Huile olive', + 'quantity' => 1, 'unit' => IngredientUnit::TABLESPOON->value, ], ], @@ -119,6 +129,7 @@ it('replaces meal post ingredients when updating a meal post', function () { 'meal_posts_id' => $mealPost->id, 'position' => 1, 'ingredient' => 'Pates', + 'quantity' => 200, 'unit' => IngredientUnit::GRAM->value, ]); }); @@ -131,6 +142,7 @@ it('keeps existing ingredients when updating a meal post without ingredients', f $mealPost->ingredients()->create([ 'position' => 1, 'ingredient' => 'Riz', + 'quantity' => 150, 'unit' => IngredientUnit::GRAM, ]); @@ -157,6 +169,7 @@ it('removes meal post ingredients when updating with an empty ingredients list', $mealPost->ingredients()->create([ 'position' => 1, 'ingredient' => 'Riz', + 'quantity' => 150, 'unit' => IngredientUnit::GRAM, ]);