feat: ingredientds
Laravel CI-CD / Tests Unitaires (push) Successful in 19s Details
Laravel CI-CD / Build & Push Docker (push) Successful in 43s Details
Laravel CI-CD / Deploy via Portainer (push) Successful in 1s Details

This commit is contained in:
Leon Morival 2026-05-18 14:51:25 +02:00
parent 7a907f2b17
commit 8c612452f2
8 changed files with 50 additions and 4 deletions

View File

@ -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';
}

View File

@ -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)],
];
}

View File

@ -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)],
];
}

View File

@ -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,

View File

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

View File

@ -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');
});
}

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('meal_post_ingredients', function (Blueprint $table) {
$table->unsignedInteger('quantity')->default(1);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('meal_post_ingredients', function (Blueprint $table) {
$table->dropColumn('quantity');
});
}
};

View File

@ -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,
]);