From 3c5d1c1f00bca64ae6e19595e9431afba0d34a66 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Wed, 10 Feb 2021 05:55:01 -0800 Subject: [PATCH] Use mg base unit for cholesterol and sodium --- app/Http/Controllers/FoodController.php | 1 - .../Controllers/JournalEntryController.php | 4 +-- app/Support/Nutrients.php | 12 +++---- ...6_convert_cholesterol_and_sodium_to_mg.php | 34 +++++++++++++++++++ resources/views/foods/edit.blade.php | 15 ++++---- resources/views/foods/index.blade.php | 4 +-- resources/views/foods/show.blade.php | 4 +-- .../create-from-nutrients.blade.php | 8 ++--- .../views/journal-entries/index.blade.php | 12 +++---- resources/views/recipes/index.blade.php | 4 +-- resources/views/recipes/show.blade.php | 4 +-- 11 files changed, 67 insertions(+), 35 deletions(-) create mode 100644 database/migrations/2021_02_10_134156_convert_cholesterol_and_sodium_to_mg.php diff --git a/app/Http/Controllers/FoodController.php b/app/Http/Controllers/FoodController.php index 5718274..eea1a2f 100644 --- a/app/Http/Controllers/FoodController.php +++ b/app/Http/Controllers/FoodController.php @@ -62,7 +62,6 @@ class FoodController extends Controller return view('foods.edit') ->with('food', $food) ->with('food_tags', $food_tags) - ->with('nutrients', Nutrients::$all) ->with('serving_units', new Collection([ ['value' => 'tsp', 'label' => 'tsp.'], ['value' => 'tbsp', 'label' => 'tbsp.'], diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index 4f14e6d..e469f5d 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -128,14 +128,14 @@ class JournalEntryController extends Controller $ingredient['unit'] ); foreach (Nutrients::$all as $nutrient) { - $entries[$entry_key]->{$nutrient} += $item->{$nutrient} * $nutrient_multiplier; + $entries[$entry_key]->{$nutrient['value']} += $item->{$nutrient['value']} * $nutrient_multiplier; } $entries[$entry_key]->foods->add($item); } elseif ($ingredient['type'] == Recipe::class) { $item = Recipe::whereId($ingredient['id'])->first(); foreach (Nutrients::$all as $nutrient) { - $entries[$entry_key]->{$nutrient} += $item->{"{$nutrient}PerServing"}() * Number::floatFromString($ingredient['amount']); + $entries[$entry_key]->{$nutrient['value']} += $item->{"{$nutrient['value']}PerServing"}() * Number::floatFromString($ingredient['amount']); } $entries[$entry_key]->recipes->add($item); } diff --git a/app/Support/Nutrients.php b/app/Support/Nutrients.php index eec783f..afef205 100644 --- a/app/Support/Nutrients.php +++ b/app/Support/Nutrients.php @@ -10,12 +10,12 @@ use App\Models\Food; class Nutrients { public static array $all = [ - 'calories', - 'fat', - 'cholesterol', - 'sodium', - 'carbohydrates', - 'protein', + ['value' => 'calories', 'unit' => null], + ['value' => 'fat', 'unit' => 'g'], + ['value' => 'cholesterol', 'unit' => 'mg'], + ['value' => 'sodium', 'unit' => 'mg'], + ['value' => 'carbohydrates', 'unit' => 'g'], + ['value' => 'protein', 'unit' => 'g'], ]; public static array $units = [ diff --git a/database/migrations/2021_02_10_134156_convert_cholesterol_and_sodium_to_mg.php b/database/migrations/2021_02_10_134156_convert_cholesterol_and_sodium_to_mg.php new file mode 100644 index 0000000..6d4fcfe --- /dev/null +++ b/database/migrations/2021_02_10_134156_convert_cholesterol_and_sodium_to_mg.php @@ -0,0 +1,34 @@ +
- @foreach ($nutrients as $nutrient) - + @foreach (\App\Support\Nutrients::$all as $nutrient) +
- + - + :value="old($nutrient['value'], $food->{$nutrient['value']})"/>
@endforeach
diff --git a/resources/views/foods/index.blade.php b/resources/views/foods/index.blade.php index f568b5f..2ab3525 100644 --- a/resources/views/foods/index.blade.php +++ b/resources/views/foods/index.blade.php @@ -48,12 +48,12 @@
Cholesterol
-
+

Sodium
-
+

diff --git a/resources/views/foods/show.blade.php b/resources/views/foods/show.blade.php index d9c06cb..f97a2a3 100644 --- a/resources/views/foods/show.blade.php +++ b/resources/views/foods/show.blade.php @@ -52,12 +52,12 @@
Cholesterol
-
{{ $food->cholesterol }}g
+
{{ $food->cholesterol }}mg

Sodium
-
{{ $food->sodium }}g
+
{{ $food->sodium }}mg

diff --git a/resources/views/journal-entries/create-from-nutrients.blade.php b/resources/views/journal-entries/create-from-nutrients.blade.php index a07d434..1dad5c8 100644 --- a/resources/views/journal-entries/create-from-nutrients.blade.php +++ b/resources/views/journal-entries/create-from-nutrients.blade.php @@ -53,16 +53,16 @@
@foreach (\App\Support\Nutrients::$all as $nutrient) - +
- -
@endforeach diff --git a/resources/views/journal-entries/index.blade.php b/resources/views/journal-entries/index.blade.php index b766534..b4a1a46 100644 --- a/resources/views/journal-entries/index.blade.php +++ b/resources/views/journal-entries/index.blade.php @@ -46,9 +46,9 @@
Fat
{{ round($entries->sum('fat'), 2) }}g
Cholesterol
-
{{ round($entries->sum('cholesterol'), 2) }}g
+
{{ round($entries->sum('cholesterol'), 2) }}mg
Sodium
-
{{ round($entries->sum('sodium'), 2) }}g
+
{{ round($entries->sum('sodium'), 2) }}mg
Carbohydrates
{{ round($entries->sum('carbohydrates'), 2) }}g
Protein
@@ -65,8 +65,8 @@
@foreach(\App\Support\Nutrients::$all as $nutrient) - {{ round($entries->where('meal', $meal)->sum($nutrient), 2) }}g - {{ $nutrient }}@if(!$loop->last), @endif + {{ round($entries->where('meal', $meal)->sum($nutrient['value']), 2) }}{{ $nutrient['unit'] }} + {{ $nutrient['value'] }}@if(!$loop->last), @endif @endforeach @@ -85,8 +85,8 @@
nutrients: @foreach(\App\Support\Nutrients::$all as $nutrient) - {{ round($entry->{$nutrient}, 2) }}g - {{ $nutrient }}@if(!$loop->last), @endif + {{ round($entry->{$nutrient['value']}, 2) }}{{ $nutrient['unit'] }} + {{ $nutrient['value'] }}@if(!$loop->last), @endif @endforeach
@if($entry->foods()->exists()) diff --git a/resources/views/recipes/index.blade.php b/resources/views/recipes/index.blade.php index 6585d48..9ff2c16 100644 --- a/resources/views/recipes/index.blade.php +++ b/resources/views/recipes/index.blade.php @@ -40,12 +40,12 @@
Cholesterol
-
+

Sodium
-
+

diff --git a/resources/views/recipes/show.blade.php b/resources/views/recipes/show.blade.php index 130674d..c84a8c0 100644 --- a/resources/views/recipes/show.blade.php +++ b/resources/views/recipes/show.blade.php @@ -64,12 +64,12 @@
Cholesterol
-
{{ $recipe->cholesterolPerServing() }}g
+
{{ $recipe->cholesterolPerServing() }}mg

Sodium
-
{{ $recipe->sodiumPerServing() }}g
+
{{ $recipe->sodiumPerServing() }}mg