From 545e4c0b5e03afa071345e73bc192aa8cd6cb7ea Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Wed, 30 Dec 2020 14:00:04 -0800 Subject: [PATCH] Add recipe list view --- app/Http/Controllers/RecipeController.php | 7 ++-- app/Models/Recipe.php | 11 ++++- resources/views/ingredients/index.blade.php | 24 +++++------ resources/views/recipes/index.blade.php | 45 +++++++++++++++++++++ 4 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 resources/views/recipes/index.blade.php diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index 4de8719..40b5677 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -18,11 +18,12 @@ class RecipeController extends Controller /** * Display a listing of the resource. * - * @return \Illuminate\Http\Response + * @return \Illuminate\Contracts\View\View */ - public function index() + public function index(): View { - // + return view('recipes.index') + ->with('recipes', Recipe::all()->sortBy('name')); } /** diff --git a/app/Models/Recipe.php b/app/Models/Recipe.php index 89c565e..92aab43 100644 --- a/app/Models/Recipe.php +++ b/app/Models/Recipe.php @@ -106,7 +106,16 @@ class Recipe extends Model return $this->sumNutrient(substr($method, 0, -5)); } elseif (in_array($method, $this->nutrientPerServingMethods)) { - return $this->sumNutrient(substr($method, 0, -10)) / $this->servings; + $sum = $this->sumNutrient(substr($method, 0, -10)) / $this->servings; + + // Per-serving calculations are rounded, though actual food label + // rounding standards are more complex. + if ($sum > 1) { + return round($sum); + } + else { + return round($sum, 2); + } } else { return parent::__call($method, $parameters); diff --git a/resources/views/ingredients/index.blade.php b/resources/views/ingredients/index.blade.php index 03d8d0b..5715d2e 100644 --- a/resources/views/ingredients/index.blade.php +++ b/resources/views/ingredients/index.blade.php @@ -1,7 +1,7 @@

- {{ __('List Ingredients') }} + {{ __('Ingredients') }}

@@ -15,34 +15,34 @@
- @foreach ($ingredients as $ingedient) + @foreach ($ingredients as $ingredient)
- {{ $ingedient->name }}@if($ingedient->detail), {{ $ingedient->detail }}@endif + {{ $ingredient->name }}@if($ingredient->detail), {{ $ingredient->detail }}@endif
- @if ($ingedient->unit_weight) - {{ $ingedient->unit_weight }}g each + @if ($ingredient->unit_weight) + {{ $ingredient->unit_weight }}g each @else - {{ $ingedient->cup_weight }}g per cup + {{ $ingredient->cup_weight }}g per cup @endif
Amount per 100g
Calories
-
{{$ingedient->calories}}
+
{{$ingredient->calories}}
Fat
-
{{ $ingedient->fat < 1 ? $ingedient->fat * 1000 . "m" : $ingedient->fat }}g
+
{{ $ingredient->fat < 1 ? $ingredient->fat * 1000 . "m" : $ingredient->fat }}g
Cholesterol
-
{{ $ingedient->cholesterol < 1 ? $ingedient->cholesterol * 1000 . "m" : $ingedient->cholesterol }}g
+
{{ $ingredient->cholesterol < 1 ? $ingredient->cholesterol * 1000 . "m" : $ingredient->cholesterol }}g
Sodium
-
{{ $ingedient->sodium < 1 ? $ingedient->sodium * 1000 . "m" : $ingedient->sodium }}g
+
{{ $ingredient->sodium < 1 ? $ingredient->sodium * 1000 . "m" : $ingredient->sodium }}g
Carbohydrates
-
{{ $ingedient->carbohydrates < 1 ? $ingedient->carbohydrates * 1000 . "m" : $ingedient->carbohydrates }}g
+
{{ $ingredient->carbohydrates < 1 ? $ingredient->carbohydrates * 1000 . "m" : $ingredient->carbohydrates }}g
Protein
-
{{ $ingedient->protein < 1 ? $ingedient->protein * 1000 . "m" : $ingedient->protein }}g
+
{{ $ingredient->protein < 1 ? $ingredient->protein * 1000 . "m" : $ingredient->protein }}g
@endforeach diff --git a/resources/views/recipes/index.blade.php b/resources/views/recipes/index.blade.php new file mode 100644 index 0000000..7803e25 --- /dev/null +++ b/resources/views/recipes/index.blade.php @@ -0,0 +1,45 @@ + + +

+ {{ __('Recipes') }} +

+
+ +
+
+ @if(session()->has('message')) +
+ {{ session()->get('message') }} +
+ @endif +
+
+
+ @foreach ($recipes as $recipe) +
+
+
{{ $recipe->name }}
+
+
+
Amount per serving
+
Calories
+
{{ $recipe->caloriesPerServing() }}
+
Fat
+
{{ $recipe->fatPerServing() < 1 ? $recipe->fatPerServing() * 1000 . "m" : $recipe->fatPerServing() }}g
+
Cholesterol
+
{{ $recipe->cholesterolPerServing() < 1 ? $recipe->cholesterolPerServing() * 1000 . "m" : $recipe->cholesterolPerServing() }}g
+
Sodium
+
{{ $recipe->sodiumPerServing() < 1 ? $recipe->sodiumPerServing() * 1000 . "m" : $recipe->sodiumPerServing() }}g
+
Carbohydrates
+
{{ $recipe->carbohydratesPerServing() < 1 ? $recipe->carbohydratesPerServing() * 1000 . "m" : $recipe->carbohydratesPerServing() }}g
+
Protein
+
{{ $recipe->proteinPerServing() < 1 ? $recipe->proteinPerServing() * 1000 . "m" : $recipe->proteinPerServing() }}g
+
+
+ @endforeach +
+
+
+
+
+