feat: ingredientds
This commit is contained in:
parent
7a907f2b17
commit
8c612452f2
|
|
@ -10,6 +10,6 @@ enum MealPostType: string
|
||||||
case BRUNCH = 'brunch';
|
case BRUNCH = 'brunch';
|
||||||
case SNACK = 'snack';
|
case SNACK = 'snack';
|
||||||
case DRINK = 'drink';
|
case DRINK = 'drink';
|
||||||
case SUPPLEMENT = "supplement";
|
case SUPPLEMENT = 'supplement';
|
||||||
case OTHER = "other";
|
case OTHER = 'other';
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ class MealPostIngredientsRequest extends FormRequest
|
||||||
'meal_posts_id' => ['required', 'exists:meal_posts,id'],
|
'meal_posts_id' => ['required', 'exists:meal_posts,id'],
|
||||||
'position' => ['required', 'integer'],
|
'position' => ['required', 'integer'],
|
||||||
'ingredient' => ['required', 'string'],
|
'ingredient' => ['required', 'string'],
|
||||||
|
'quantity' => ['required', 'integer', 'min:1'],
|
||||||
'unit' => ['required', Rule::enum(IngredientUnit::class)],
|
'unit' => ['required', Rule::enum(IngredientUnit::class)],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,9 +27,10 @@ class MealPostsRequest extends FormRequest
|
||||||
'type' => [$isCreating ? 'required' : 'sometimes', Rule::enum(MealPostType::class)],
|
'type' => [$isCreating ? 'required' : 'sometimes', Rule::enum(MealPostType::class)],
|
||||||
'visibility' => ['sometimes', Rule::enum(MealPostVisibility::class)],
|
'visibility' => ['sometimes', Rule::enum(MealPostVisibility::class)],
|
||||||
'ingredients' => ['sometimes', 'array'],
|
'ingredients' => ['sometimes', 'array'],
|
||||||
'ingredients.*' => ['required', 'array:position,ingredient,unit'],
|
'ingredients.*' => ['required', 'array:position,ingredient,quantity,unit'],
|
||||||
'ingredients.*.position' => ['required', 'integer', 'min:0'],
|
'ingredients.*.position' => ['required', 'integer', 'min:0'],
|
||||||
'ingredients.*.ingredient' => ['required', 'string', 'max:255'],
|
'ingredients.*.ingredient' => ['required', 'string', 'max:255'],
|
||||||
|
'ingredients.*.quantity' => ['required', 'integer', 'min:1'],
|
||||||
'ingredients.*.unit' => ['required', Rule::enum(IngredientUnit::class)],
|
'ingredients.*.unit' => ['required', Rule::enum(IngredientUnit::class)],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ class MealPostIngredientsResource extends JsonResource
|
||||||
'id' => $this->id,
|
'id' => $this->id,
|
||||||
'position' => $this->position,
|
'position' => $this->position,
|
||||||
'ingredient' => $this->ingredient,
|
'ingredient' => $this->ingredient,
|
||||||
|
'quantity' => $this->quantity,
|
||||||
'unit' => $this->unit,
|
'unit' => $this->unit,
|
||||||
'created_at' => $this->created_at,
|
'created_at' => $this->created_at,
|
||||||
'updated_at' => $this->updated_at,
|
'updated_at' => $this->updated_at,
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ class MealPostIngredients extends Model
|
||||||
'meal_posts_id',
|
'meal_posts_id',
|
||||||
'position',
|
'position',
|
||||||
'ingredient',
|
'ingredient',
|
||||||
|
'quantity',
|
||||||
'unit',
|
'unit',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
@ -23,6 +24,7 @@ class MealPostIngredients extends Model
|
||||||
protected function casts(): array
|
protected function casts(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
'quantity' => 'integer',
|
||||||
'unit' => IngredientUnit::class,
|
'unit' => IngredientUnit::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ return new class extends Migration
|
||||||
public function up(): void
|
public function up(): void
|
||||||
{
|
{
|
||||||
Schema::table('meal_posts', function (Blueprint $table) {
|
Schema::table('meal_posts', function (Blueprint $table) {
|
||||||
$table->string("type")->default('breakfast');
|
$table->string('type')->default('breakfast');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -23,11 +23,13 @@ it('creates a meal post with ingredients', function () {
|
||||||
[
|
[
|
||||||
'position' => 1,
|
'position' => 1,
|
||||||
'ingredient' => 'Riz',
|
'ingredient' => 'Riz',
|
||||||
|
'quantity' => 150,
|
||||||
'unit' => IngredientUnit::GRAM->value,
|
'unit' => IngredientUnit::GRAM->value,
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'position' => 2,
|
'position' => 2,
|
||||||
'ingredient' => 'Saumon',
|
'ingredient' => 'Saumon',
|
||||||
|
'quantity' => 120,
|
||||||
'unit' => IngredientUnit::GRAM->value,
|
'unit' => IngredientUnit::GRAM->value,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
@ -36,6 +38,7 @@ it('creates a meal post with ingredients', function () {
|
||||||
$response
|
$response
|
||||||
->assertCreated()
|
->assertCreated()
|
||||||
->assertJsonPath('data.ingredients.0.ingredient', 'Riz')
|
->assertJsonPath('data.ingredients.0.ingredient', 'Riz')
|
||||||
|
->assertJsonPath('data.ingredients.0.quantity', 150)
|
||||||
->assertJsonPath('data.ingredients.0.unit', IngredientUnit::GRAM->value)
|
->assertJsonPath('data.ingredients.0.unit', IngredientUnit::GRAM->value)
|
||||||
->assertJsonPath('data.ingredients.1.ingredient', 'Saumon');
|
->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'),
|
'meal_posts_id' => $response->json('data.id'),
|
||||||
'position' => 1,
|
'position' => 1,
|
||||||
'ingredient' => 'Riz',
|
'ingredient' => 'Riz',
|
||||||
|
'quantity' => 150,
|
||||||
'unit' => IngredientUnit::GRAM->value,
|
'unit' => IngredientUnit::GRAM->value,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
@ -59,6 +63,7 @@ it('validates meal post ingredients', function () {
|
||||||
[
|
[
|
||||||
'position' => -1,
|
'position' => -1,
|
||||||
'ingredient' => '',
|
'ingredient' => '',
|
||||||
|
'quantity' => 0,
|
||||||
'unit' => 'cup',
|
'unit' => 'cup',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
@ -69,6 +74,7 @@ it('validates meal post ingredients', function () {
|
||||||
->assertJsonValidationErrors([
|
->assertJsonValidationErrors([
|
||||||
'ingredients.0.position',
|
'ingredients.0.position',
|
||||||
'ingredients.0.ingredient',
|
'ingredients.0.ingredient',
|
||||||
|
'ingredients.0.quantity',
|
||||||
'ingredients.0.unit',
|
'ingredients.0.unit',
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
@ -80,11 +86,13 @@ it('replaces meal post ingredients when updating a meal post', function () {
|
||||||
[
|
[
|
||||||
'position' => 1,
|
'position' => 1,
|
||||||
'ingredient' => 'Riz',
|
'ingredient' => 'Riz',
|
||||||
|
'quantity' => 150,
|
||||||
'unit' => IngredientUnit::GRAM,
|
'unit' => IngredientUnit::GRAM,
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'position' => 2,
|
'position' => 2,
|
||||||
'ingredient' => 'Saumon',
|
'ingredient' => 'Saumon',
|
||||||
|
'quantity' => 120,
|
||||||
'unit' => IngredientUnit::GRAM,
|
'unit' => IngredientUnit::GRAM,
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
@ -96,11 +104,13 @@ it('replaces meal post ingredients when updating a meal post', function () {
|
||||||
[
|
[
|
||||||
'position' => 1,
|
'position' => 1,
|
||||||
'ingredient' => 'Pates',
|
'ingredient' => 'Pates',
|
||||||
|
'quantity' => 200,
|
||||||
'unit' => IngredientUnit::GRAM->value,
|
'unit' => IngredientUnit::GRAM->value,
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'position' => 2,
|
'position' => 2,
|
||||||
'ingredient' => 'Huile olive',
|
'ingredient' => 'Huile olive',
|
||||||
|
'quantity' => 1,
|
||||||
'unit' => IngredientUnit::TABLESPOON->value,
|
'unit' => IngredientUnit::TABLESPOON->value,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
@ -119,6 +129,7 @@ it('replaces meal post ingredients when updating a meal post', function () {
|
||||||
'meal_posts_id' => $mealPost->id,
|
'meal_posts_id' => $mealPost->id,
|
||||||
'position' => 1,
|
'position' => 1,
|
||||||
'ingredient' => 'Pates',
|
'ingredient' => 'Pates',
|
||||||
|
'quantity' => 200,
|
||||||
'unit' => IngredientUnit::GRAM->value,
|
'unit' => IngredientUnit::GRAM->value,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
@ -131,6 +142,7 @@ it('keeps existing ingredients when updating a meal post without ingredients', f
|
||||||
$mealPost->ingredients()->create([
|
$mealPost->ingredients()->create([
|
||||||
'position' => 1,
|
'position' => 1,
|
||||||
'ingredient' => 'Riz',
|
'ingredient' => 'Riz',
|
||||||
|
'quantity' => 150,
|
||||||
'unit' => IngredientUnit::GRAM,
|
'unit' => IngredientUnit::GRAM,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
@ -157,6 +169,7 @@ it('removes meal post ingredients when updating with an empty ingredients list',
|
||||||
$mealPost->ingredients()->create([
|
$mealPost->ingredients()->create([
|
||||||
'position' => 1,
|
'position' => 1,
|
||||||
'ingredient' => 'Riz',
|
'ingredient' => 'Riz',
|
||||||
|
'quantity' => 150,
|
||||||
'unit' => IngredientUnit::GRAM,
|
'unit' => IngredientUnit::GRAM,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue