diff --git a/app/Http/Controllers/FoodController.php b/app/Http/Controllers/FoodController.php index 2ef8df0..7c914cb 100644 --- a/app/Http/Controllers/FoodController.php +++ b/app/Http/Controllers/FoodController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Food; +use App\Support\Nutrients; use Illuminate\Contracts\View\View; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; @@ -12,8 +13,6 @@ class FoodController extends Controller { /** * Display a listing of the resource. - * - * @return \Illuminate\Contracts\View\View */ public function index(): View { @@ -28,7 +27,33 @@ class FoodController extends Controller */ public function create(): View { - return view('foods.create') + return $this->edit(new Food()); + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request): RedirectResponse + { + return $this->update($request, new Food()); + } + + /** + * Display the specified resource. + */ + public function show(Food $food): View + { + return view('foods.show')->with('food', $food); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Food $food): View + { + return view('foods.edit') + ->with('food', $food) + ->with('nutrients', Nutrients::$all) ->with('serving_units', new Collection([ ['value' => 'tsp', 'label' => 'tsp.'], ['value' => 'tbsp', 'label' => 'tbsp.'], @@ -36,10 +61,10 @@ class FoodController extends Controller ])); } - /**newly - * Store a newly created resource in storage. + /** + * Update the specified resource in storage. */ - public function store(Request $request): RedirectResponse + public function update(Request $request, Food $food): RedirectResponse { $attributes = $request->validate([ 'name' => 'required|string', @@ -55,43 +80,9 @@ class FoodController extends Controller 'carbohydrates' => 'nullable|numeric', 'protein' => 'nullable|numeric', ]); - /** @var \App\Models\Food $food */ - $food = tap(new Food(array_filter($attributes)))->save(); - return back()->with('message', "Food {$food->name} added!"); - } - - /** - * Display the specified resource. - * - * @param \App\Models\Food $food - * @return \Illuminate\Contracts\View\View - */ - public function show(Food $food): View - { - return view('foods.show')->with('food', $food); - } - - /** - * Show the form for editing the specified resource. - * - * @param \App\Models\Food $food - * @return \Illuminate\Http\Response - */ - public function edit(Food $food) - { - // - } - - /** - * Update the specified resource in storage. - * - * @param \Illuminate\Http\Request $request - * @param \App\Models\Food $food - * @return \Illuminate\Http\Response - */ - public function update(Request $request, Food $food) - { - // + $food->fill(array_filter($attributes))->save(); + return redirect(route('foods.show', $food)) + ->with('message', 'Changes saved!'); } /** diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index f01bf3e..93dd2c5 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -111,7 +111,7 @@ class JournalEntryController extends Controller } $summary = []; - $nutrients = array_fill_keys(Nutrients::$list, 0); + $nutrients = array_fill_keys(Nutrients::$all, 0); if (!empty($foods_selected)) { $foods = Food::findMany($foods_selected)->keyBy('id'); diff --git a/app/Support/Nutrients.php b/app/Support/Nutrients.php index 1d5af41..445cf04 100644 --- a/app/Support/Nutrients.php +++ b/app/Support/Nutrients.php @@ -9,7 +9,7 @@ use App\Models\Food; */ class Nutrients { - public static array $list = [ + public static array $all = [ 'calories', 'fat', 'cholesterol', diff --git a/resources/views/foods/create.blade.php b/resources/views/foods/edit.blade.php similarity index 55% rename from resources/views/foods/create.blade.php rename to resources/views/foods/edit.blade.php index ed16926..6485ced 100644 --- a/resources/views/foods/create.blade.php +++ b/resources/views/foods/edit.blade.php @@ -1,7 +1,7 @@

- {{ __('Add Food') }} + {{ ($food->exists ? 'Save' : 'Add') }} Food

@@ -25,6 +25,13 @@
+ @if ($food->exists) +
+ @method('put') + @else + + @endif + @csrf
@@ -37,7 +44,7 @@ class="block mt-1 w-full" type="text" name="name" - :value="old('name')" + :value="old('name', $food->name)" required/>
@@ -49,7 +56,7 @@ class="block mt-1 w-full" type="text" name="detail" - :value="old('detail')"/> + :value="old('detail', $food->detail)"/>
@@ -60,7 +67,7 @@ class="block mt-1 w-full" type="text" name="brand" - :value="old('brand')"/> + :value="old('brand', $food->brand)"/>
@@ -75,7 +82,7 @@ step="any" name="serving_size" size="10" - :value="old('serving_size')"/> + :value="old('serving_size', $food->serving_size)"/> @@ -84,7 +91,7 @@ + :selectedValue="old('serving_unit', $food->serving_unit)"> @@ -99,93 +106,30 @@ step="any" name="serving_weight" size="10" - :value="old('serving_weight')"/> + :value="old('serving_weight', $food->serving_weight)"/>
- -
- + @foreach ($nutrients as $nutrient) +
+ - -
- - -
- - - -
- - -
- - - -
- - -
- - - -
- - -
- - - -
- - -
- - - -
+ +
+ @endforeach
- {{ __('Add') }} + {{ ($food->exists ? 'Save' : 'Add') }}
diff --git a/resources/views/foods/index.blade.php b/resources/views/foods/index.blade.php index 824eb6d..55243d4 100644 --- a/resources/views/foods/index.blade.php +++ b/resources/views/foods/index.blade.php @@ -18,7 +18,7 @@ @foreach ($foods as $food)
- {{ $food->name }}@if($food->detail), {{ $food->detail }}@endif + {{ $food->name }}@if($food->detail), {{ $food->detail }}@endif
@if($food->brand)
@@ -45,6 +45,12 @@
Protein
{{ $food->protein < 1 ? $food->protein * 1000 . "m" : $food->protein }}g
+
@endforeach