Use mg base unit for cholesterol and sodium

This commit is contained in:
Christopher C. Wells 2021-02-10 05:55:01 -08:00
parent 0829b69e14
commit 3c5d1c1f00
11 changed files with 67 additions and 35 deletions

View File

@ -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.'],

View File

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

View File

@ -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 = [

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ConvertCholesterolAndSodiumToMg extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::update("UPDATE `foods` SET `cholesterol` = `cholesterol` * 1000;");
DB::update("UPDATE `foods` SET `sodium` = `sodium` * 1000;");
DB::update("UPDATE `journal_entries` SET `cholesterol` = `cholesterol` * 1000;");
DB::update("UPDATE `journal_entries` SET `sodium` = `sodium` * 1000;");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::update("UPDATE `foods` SET `cholesterol` = `cholesterol`/1000;");
DB::update("UPDATE `foods` SET `sodium` = `sodium`/1000;");
DB::update("UPDATE `journal_entries` SET `cholesterol` = `cholesterol`/1000;");
DB::update("UPDATE `journal_entries` SET `sodium` = `sodium`/1000;");
}
}

View File

@ -110,18 +110,17 @@
</div>
<div class="flex flex-col space-y-4 md:flex-row md:space-y-0">
@foreach ($nutrients as $nutrient)
<!-- {{ ucfirst($nutrient) }} -->
@foreach (\App\Support\Nutrients::$all as $nutrient)
<!-- {{ ucfirst($nutrient['value']) }} -->
<div class="flex-auto">
<x-inputs.label for="{{ $nutrient }}"
:value="ucfirst($nutrient) . ' (g)'"/>
<x-inputs.label for="{{ $nutrient['value'] }}"
:value="ucfirst($nutrient['value']) . ($nutrient['unit'] ? ' (' . $nutrient['unit'] . ')' : '')"/>
<x-inputs.input id="{{ $nutrient }}"
class="block w-full mt-1 md:w-5/6"
<x-inputs.input name="{{ $nutrient['value'] }}"
type="number"
class="block w-full mt-1 md:w-5/6"
step="any"
name="{{ $nutrient }}"
:value="old($nutrient, $food->{$nutrient})"/>
:value="old($nutrient['value'], $food->{$nutrient['value']})"/>
</div>
@endforeach
</div>

View File

@ -48,12 +48,12 @@
<hr class="border-gray-500"/>
<div class="flex justify-between">
<div class="font-bold">Cholesterol</div>
<div x-text="`${Math.round(food.cholesterol*1000)}mg`"></div>
<div x-text="`${food.cholesterol}mg`"></div>
</div>
<hr class="border-gray-500"/>
<div class="flex justify-between">
<div class="font-bold">Sodium</div>
<div x-text="`${Math.round(food.sodium*1000)}mg`"></div>
<div x-text="`${food.sodium}mg`"></div>
</div>
<hr class="border-gray-500"/>
<div class="flex justify-between">

View File

@ -52,12 +52,12 @@
<hr class="border-gray-500"/>
<div class="flex justify-between">
<div class="font-bold">Cholesterol</div>
<div>{{ $food->cholesterol }}g</div>
<div>{{ $food->cholesterol }}mg</div>
</div>
<hr class="border-gray-500"/>
<div class="flex justify-between">
<div class="font-bold">Sodium</div>
<div>{{ $food->sodium }}g</div>
<div>{{ $food->sodium }}mg</div>
</div>
<hr class="border-gray-500"/>
<div class="flex justify-between">

View File

@ -53,16 +53,16 @@
<div class="flex flex-col space-y-4 md:flex-row md:space-x-4 md:space-y-0 w-full">
@foreach (\App\Support\Nutrients::$all as $nutrient)
<!-- {{ ucfirst($nutrient) }} -->
<!-- {{ ucfirst($nutrient['value']) }} -->
<div class="flex-auto">
<x-inputs.label for="{{ $nutrient }}"
<x-inputs.label for="{{ $nutrient['value'] }}"
:value="ucfirst($nutrient) . ' (g)'"/>
<x-inputs.input id="{{ $nutrient }}"
<x-inputs.input id="{{ $nutrient['value'] }}"
class="block w-full"
type="number"
step="any"
name="{{ $nutrient }}"
name="{{ $nutrient['value'] }}"
:value="old($nutrient)"/>
</div>
@endforeach

View File

@ -46,9 +46,9 @@
<div class="font-bold border-b border-gray-300">Fat</div>
<div class="text-right border-b border-gray-300">{{ round($entries->sum('fat'), 2) }}g</div>
<div class="font-bold border-b border-gray-300">Cholesterol</div>
<div class="text-right border-b border-gray-300">{{ round($entries->sum('cholesterol'), 2) }}g</div>
<div class="text-right border-b border-gray-300">{{ round($entries->sum('cholesterol'), 2) }}mg</div>
<div class="font-bold border-b border-gray-300">Sodium</div>
<div class="text-right border-b border-gray-300">{{ round($entries->sum('sodium'), 2) }}g</div>
<div class="text-right border-b border-gray-300">{{ round($entries->sum('sodium'), 2) }}mg</div>
<div class="font-bold border-b border-gray-300">Carbohydrates</div>
<div class="text-right border-b border-gray-300">{{ round($entries->sum('carbohydrates'), 2) }}g</div>
<div class="font-bold">Protein</div>
@ -65,8 +65,8 @@
</div>
<span class="text-sm text-gray-500">
@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
</span>
</h3>
@ -85,8 +85,8 @@
<div>
<span class="font-bold">nutrients:</span>
@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
</div>
@if($entry->foods()->exists())

View File

@ -40,12 +40,12 @@
<hr class="border-gray-500"/>
<div class="flex justify-between">
<div class="font-bold">Cholesterol</div>
<div x-text="`${recipe.cholesterolPerServing}g`"></div>
<div x-text="`${recipe.cholesterolPerServing}mg`"></div>
</div>
<hr class="border-gray-500"/>
<div class="flex justify-between">
<div class="font-bold">Sodium</div>
<div x-text="`${recipe.sodiumPerServing}g`"></div>
<div x-text="`${recipe.sodiumPerServing}mg`"></div>
</div>
<hr class="border-gray-500"/>
<div class="flex justify-between">

View File

@ -64,12 +64,12 @@
<hr class="border-gray-500"/>
<div class="flex justify-between">
<div class="font-bold">Cholesterol</div>
<div>{{ $recipe->cholesterolPerServing() }}g</div>
<div>{{ $recipe->cholesterolPerServing() }}mg</div>
</div>
<hr class="border-gray-500"/>
<div class="flex justify-between">
<div class="font-bold">Sodium</div>
<div>{{ $recipe->sodiumPerServing() }}g</div>
<div>{{ $recipe->sodiumPerServing() }}mg</div>
</div>
<hr class="border-gray-500"/>
<div class="flex justify-between">