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;
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!');
}
/**

View File

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

View File

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

View File

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

View File

@ -18,7 +18,7 @@
@foreach ($foods as $food)
<div class="p-2 font-light rounded-lg border-2 border-gray-200">
<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>
@if($food->brand)
<div class="text-xl text-gray-600">
@ -45,6 +45,12 @@
<div class="font-bold">Protein</div>
<div class="text-right">{{ $food->protein < 1 ? $food->protein * 1000 . "m" : $food->protein }}g</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>
@endforeach
</div>