diff --git a/app/Http/Controllers/IngredientController.php b/app/Http/Controllers/IngredientController.php index a0ce9af..a62195a 100644 --- a/app/Http/Controllers/IngredientController.php +++ b/app/Http/Controllers/IngredientController.php @@ -49,7 +49,7 @@ class IngredientController extends Controller ]); /** @var \App\Models\Ingredient $ingredient */ $ingredient = tap(new Ingredient(array_filter($attributes)))->save(); - return redirect()->back()->with('message', "Ingredient {$ingredient->name} added!"); + return back()->with('message', "Ingredient {$ingredient->name} added!"); } /** diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index 592385f..c742651 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -2,9 +2,14 @@ namespace App\Http\Controllers; +use App\Models\Ingredient; +use App\Models\IngredientAmount; use App\Models\Recipe; use Illuminate\Contracts\View\View; +use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\DB; class RecipeController extends Controller { @@ -25,18 +30,72 @@ class RecipeController extends Controller */ public function create(): View { - return view('recipes.create'); + $ingredients = Ingredient::all(['id', 'name', 'detail'])->collect() + ->map(function ($ingredient) { + return [ + 'value' => $ingredient->id, + 'label' => "{$ingredient->name}" . ($ingredient->detail ? ", {$ingredient->detail}" : ""), + ]; + }); + return view('recipes.create') + ->with('ingredients', $ingredients) + ->with('ingredient_units', new Collection([ + ['value' => 'tsp', 'label' => 'tsp.'], + ['value' => 'tbsp', 'label' => 'tbsp.'], + ['value' => 'cup', 'label' => 'cup'], + ['value' => 'g', 'label' => 'g'], + ])); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response + * @return \Illuminate\Http\RedirectResponse */ - public function store(Request $request) + public function store(Request $request): RedirectResponse { - // + $input = $request->validate([ + 'name' => 'required|string', + 'description' => 'required|string', + 'servings' => 'required|numeric', + 'ingredients_amount' => 'required|array', + 'ingredients_amount.*' => 'required|numeric|min:0', + 'ingredients_unit' => 'required|array', + 'ingredients_unit.*' => 'nullable|string', + 'ingredients' => 'required|array', + 'ingredients.*' => 'required|exists:App\Models\Ingredient,id', + ]); + + $recipe = new Recipe([ + 'name' => $input['name'], + 'description' => $input['description'], + 'servings' => (int) $input['servings'], + ]); + + try { + DB::transaction(function () use ($recipe, $input) { + if (!$recipe->save()) { + return; + } + $ingredient_amounts = []; + foreach ($input['ingredients_amount'] as $key => $amount) { + $ingredient_amounts[$key] = new IngredientAmount([ + 'amount' => (float) $amount, + 'unit' => $input['ingredients_unit'][$key], + 'weight' => (int) $key, + ]); + $ingredient_amounts[$key]->recipe()->associate($recipe); + $ingredient_amounts[$key]->ingredient()->associate($input['ingredients'][$key]); + } + $recipe->ingredientAmounts()->saveMany($ingredient_amounts); + }); + } catch (\Exception $e) { + DB::rollBack(); + return back()->withInput()->withErrors("Failed to add recipe due to database error: {$e->getMessage()}."); + } + + return back()->with('message', "Recipe {$recipe->name} added!"); } /** diff --git a/app/View/Components/Inputs/Select.php b/app/View/Components/Inputs/Select.php new file mode 100644 index 0000000..f3e5b83 --- /dev/null +++ b/app/View/Components/Inputs/Select.php @@ -0,0 +1,35 @@ +options = $options; + $this->selectedValue = $selectedValue; + } + + public function render(): View + { + return view('components.inputs.select') + ->with('options', $this->options) + ->with('selectedValue', $this->selectedValue); + } + +} diff --git a/resources/views/components/button.blade.php b/resources/views/components/inputs/button.blade.php similarity index 100% rename from resources/views/components/button.blade.php rename to resources/views/components/inputs/button.blade.php diff --git a/resources/views/components/input.blade.php b/resources/views/components/inputs/input.blade.php similarity index 100% rename from resources/views/components/input.blade.php rename to resources/views/components/inputs/input.blade.php diff --git a/resources/views/components/label.blade.php b/resources/views/components/inputs/label.blade.php similarity index 100% rename from resources/views/components/label.blade.php rename to resources/views/components/inputs/label.blade.php diff --git a/resources/views/components/inputs/select.blade.php b/resources/views/components/inputs/select.blade.php new file mode 100644 index 0000000..c624561 --- /dev/null +++ b/resources/views/components/inputs/select.blade.php @@ -0,0 +1,9 @@ + diff --git a/resources/views/components/form/textarea.blade.php b/resources/views/components/inputs/textarea.blade.php similarity index 100% rename from resources/views/components/form/textarea.blade.php rename to resources/views/components/inputs/textarea.blade.php diff --git a/resources/views/ingredients/create.blade.php b/resources/views/ingredients/create.blade.php index 77596da..daf6cd3 100644 --- a/resources/views/ingredients/create.blade.php +++ b/resources/views/ingredients/create.blade.php @@ -26,125 +26,125 @@