Add food edit form

This commit is contained in:
Christopher C. Wells 2021-01-04 06:08:12 -08:00
parent 1f8f618774
commit f389ebe594
5 changed files with 71 additions and 130 deletions

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Food; use App\Models\Food;
use App\Support\Nutrients;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -12,8 +13,6 @@ class FoodController extends Controller
{ {
/** /**
* Display a listing of the resource. * Display a listing of the resource.
*
* @return \Illuminate\Contracts\View\View
*/ */
public function index(): View public function index(): View
{ {
@ -28,7 +27,33 @@ class FoodController extends Controller
*/ */
public function create(): View 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([ ->with('serving_units', new Collection([
['value' => 'tsp', 'label' => 'tsp.'], ['value' => 'tsp', 'label' => 'tsp.'],
['value' => 'tbsp', 'label' => 'tbsp.'], ['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([ $attributes = $request->validate([
'name' => 'required|string', 'name' => 'required|string',
@ -55,43 +80,9 @@ class FoodController extends Controller
'carbohydrates' => 'nullable|numeric', 'carbohydrates' => 'nullable|numeric',
'protein' => 'nullable|numeric', 'protein' => 'nullable|numeric',
]); ]);
/** @var \App\Models\Food $food */ $food->fill(array_filter($attributes))->save();
$food = tap(new Food(array_filter($attributes)))->save(); return redirect(route('foods.show', $food))
return back()->with('message', "Food {$food->name} added!"); ->with('message', 'Changes saved!');
}
/**
* 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)
{
//
} }
/** /**

View File

@ -111,7 +111,7 @@ class JournalEntryController extends Controller
} }
$summary = []; $summary = [];
$nutrients = array_fill_keys(Nutrients::$list, 0); $nutrients = array_fill_keys(Nutrients::$all, 0);
if (!empty($foods_selected)) { if (!empty($foods_selected)) {
$foods = Food::findMany($foods_selected)->keyBy('id'); $foods = Food::findMany($foods_selected)->keyBy('id');

View File

@ -9,7 +9,7 @@ use App\Models\Food;
*/ */
class Nutrients class Nutrients
{ {
public static array $list = [ public static array $all = [
'calories', 'calories',
'fat', 'fat',
'cholesterol', 'cholesterol',

View File

@ -1,7 +1,7 @@
<x-app-layout> <x-app-layout>
<x-slot name="header"> <x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight"> <h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Add Food') }} {{ ($food->exists ? 'Save' : 'Add') }} Food
</h2> </h2>
</x-slot> </x-slot>
@ -25,6 +25,13 @@
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg"> <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200"> <div class="p-6 bg-white border-b border-gray-200">
@if ($food->exists)
<form method="POST" action="{{ route('foods.update', $food) }}">
@method('put')
@else
<form method="POST" action="{{ route('foods.store') }}">
@endif
<form method="POST" action="{{ route('foods.store') }}"> <form method="POST" action="{{ route('foods.store') }}">
@csrf @csrf
<div class="flex flex-col space-y-4"> <div class="flex flex-col space-y-4">
@ -37,7 +44,7 @@
class="block mt-1 w-full" class="block mt-1 w-full"
type="text" type="text"
name="name" name="name"
:value="old('name')" :value="old('name', $food->name)"
required/> required/>
</div> </div>
@ -49,7 +56,7 @@
class="block mt-1 w-full" class="block mt-1 w-full"
type="text" type="text"
name="detail" name="detail"
:value="old('detail')"/> :value="old('detail', $food->detail)"/>
</div> </div>
<!-- Brand --> <!-- Brand -->
@ -60,7 +67,7 @@
class="block mt-1 w-full" class="block mt-1 w-full"
type="text" type="text"
name="brand" name="brand"
:value="old('brand')"/> :value="old('brand', $food->brand)"/>
</div> </div>
</div> </div>
@ -75,7 +82,7 @@
step="any" step="any"
name="serving_size" name="serving_size"
size="10" size="10"
:value="old('serving_size')"/> :value="old('serving_size', $food->serving_size)"/>
</div> </div>
<!-- Serving unit --> <!-- Serving unit -->
@ -84,7 +91,7 @@
<x-inputs.select name="serving_unit" <x-inputs.select name="serving_unit"
:options="$serving_units" :options="$serving_units"
:selectedValue="old('serving_unit')"> :selectedValue="old('serving_unit', $food->serving_unit)">
<option value=""></option> <option value=""></option>
</x-inputs.select> </x-inputs.select>
</div> </div>
@ -99,93 +106,30 @@
step="any" step="any"
name="serving_weight" name="serving_weight"
size="10" size="10"
:value="old('serving_weight')"/> :value="old('serving_weight', $food->serving_weight)"/>
</div> </div>
</div> </div>
<div class="grid grid-rows-3 md:grid-rows-2 lg:grid-rows-1 grid-flow-col"> <div class="grid grid-rows-3 md:grid-rows-2 lg:grid-rows-1 grid-flow-col">
<!-- Calories --> @foreach ($nutrients as $nutrient)
<div> <div>
<x-inputs.label for="calories" :value="__('Calories (g)')"/> <x-inputs.label for="{{ $nutrient }}"
:value="ucfirst($nutrient) . ' (g)'"/>
<x-inputs.input id="calories" <x-inputs.input id="{{ $nutrient }}"
class="block mt-1" class="block mt-1"
type="number" type="number"
step="any" step="any"
name="calories" name="{{ $nutrient }}"
size="10" size="10"
:value="old('calories')"/> :value="old($nutrient, $food->{$nutrient})"/>
</div> </div>
@endforeach
<!-- Fat -->
<div>
<x-inputs.label for="fat" :value="__('Fat (g)')"/>
<x-inputs.input id="fat"
class="block mt-1"
type="number"
step="any"
name="fat"
size="10"
:value="old('fat')"/>
</div>
<!-- Cholesterol -->
<div>
<x-inputs.label for="cholesterol" :value="__('Cholesterol (g)')"/>
<x-inputs.input id="cholesterol"
class="block mt-1"
type="number"
step="any"
name="cholesterol"
size="10"
:value="old('cholesterol')"/>
</div>
<!-- Sodium -->
<div>
<x-inputs.label for="sodium" :value="__('Sodium (g)')"/>
<x-inputs.input id="sodium"
class="block mt-1"
type="number"
step="any"
name="sodium"
size="10"
:value="old('sodium')"/>
</div>
<!-- Carbohydrates -->
<div>
<x-inputs.label for="carbohydrates" :value="__('Carbohydrates (g)')"/>
<x-inputs.input id="carbohydrates"
class="block mt-1"
type="number"
step="any"
name="carbohydrates"
size="10"
:value="old('carbohydrates')"/>
</div>
<!-- Protein -->
<div>
<x-inputs.label for="protein" :value="__('Protein (g)')"/>
<x-inputs.input id="protein"
class="block mt-1"
type="number"
step="any"
name="protein"
size="10"
:value="old('protein')"/>
</div>
</div> </div>
</div> </div>
<div class="flex items-center justify-end mt-4"> <div class="flex items-center justify-end mt-4">
<x-inputs.button class="ml-3"> <x-inputs.button class="ml-3">
{{ __('Add') }} {{ ($food->exists ? 'Save' : 'Add') }}
</x-inputs.button> </x-inputs.button>
</div> </div>
</form> </form>

View File

@ -18,7 +18,7 @@
@foreach ($foods as $food) @foreach ($foods as $food)
<div class="p-2 font-light rounded-lg border-2 border-gray-200"> <div class="p-2 font-light rounded-lg border-2 border-gray-200">
<div class="text-2xl lowercase"> <div class="text-2xl lowercase">
{{ $food->name }}@if($food->detail), <span class="text-gray-500">{{ $food->detail }}</span>@endif {{ $food->name }}@if($food->detail), <span class="text-gray-500">{{ $food->detail }}</span>@endif
</div> </div>
@if($food->brand) @if($food->brand)
<div class="text-xl text-gray-600"> <div class="text-xl text-gray-600">
@ -45,6 +45,12 @@
<div class="font-bold">Protein</div> <div class="font-bold">Protein</div>
<div class="text-right">{{ $food->protein < 1 ? $food->protein * 1000 . "m" : $food->protein }}g</div> <div class="text-right">{{ $food->protein < 1 ? $food->protein * 1000 . "m" : $food->protein }}g</div>
</div> </div>
<div class="flex flex-row justify-around">
<a class="text-gray-500 hover:text-gray-700 hover:border-gray-300"
href="{{ route('foods.edit', $food) }}">edit</a>
<a class="text-gray-500 hover:text-gray-700 hover:border-gray-300"
href="{{ route('foods.destroy', $food) }}">delete (TBD)</a>
</div>
</div> </div>
@endforeach @endforeach
</div> </div>