mirror of https://github.com/kcal-app/kcal.git
Refactor nutrients data array as a collection
This commit is contained in:
parent
d3a881c359
commit
2f91c320ef
|
|
@ -95,9 +95,9 @@ class FoodController extends Controller
|
||||||
$attributes['name'] = Str::lower($attributes['name']);
|
$attributes['name'] = Str::lower($attributes['name']);
|
||||||
|
|
||||||
// Default nutrients to zero.
|
// Default nutrients to zero.
|
||||||
foreach (Nutrients::$all as $nutrient) {
|
foreach (Nutrients::all()->pluck('value') as $nutrient) {
|
||||||
if (is_null($attributes[$nutrient['value']])) {
|
if (is_null($attributes[$nutrient])) {
|
||||||
$attributes[$nutrient['value']] = 0;
|
$attributes[$nutrient] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,17 +37,17 @@ class JournalEntryController extends Controller
|
||||||
'date' => $date->toDateString(),
|
'date' => $date->toDateString(),
|
||||||
])->get();
|
])->get();
|
||||||
$sums = [];
|
$sums = [];
|
||||||
foreach (Nutrients::$all as $nutrient) {
|
foreach (Nutrients::all()->pluck('value') as $nutrient) {
|
||||||
$sums[$nutrient['value']] = round($entries->sum($nutrient['value']));
|
$sums[$nutrient] = round($entries->sum($nutrient));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get daily goals data for user.
|
// Get daily goals data for user.
|
||||||
$goals = Auth::user()->getGoalsByTime($date);
|
$goals = Auth::user()->getGoalsByTime($date);
|
||||||
$dailyGoals = [];
|
$dailyGoals = [];
|
||||||
foreach (Nutrients::$all as $nutrient) {
|
foreach (Nutrients::all()->pluck('value') as $nutrient) {
|
||||||
$goal = $goals['present']
|
$goal = $goals['present']
|
||||||
->where('frequency', 'daily')
|
->where('frequency', 'daily')
|
||||||
->where('name', $nutrient['value'])
|
->where('name', $nutrient)
|
||||||
->first();
|
->first();
|
||||||
if ($goal) {
|
if ($goal) {
|
||||||
$dailyGoals[$goal->name] = round($sums[$goal->name] / $goal->goal * 100);
|
$dailyGoals[$goal->name] = round($sums[$goal->name] / $goal->goal * 100);
|
||||||
|
|
@ -154,17 +154,17 @@ class JournalEntryController extends Controller
|
||||||
Number::floatFromString($ingredient['amount']),
|
Number::floatFromString($ingredient['amount']),
|
||||||
$ingredient['unit']
|
$ingredient['unit']
|
||||||
);
|
);
|
||||||
foreach (Nutrients::$all as $nutrient) {
|
foreach (Nutrients::all()->pluck('value') as $nutrient) {
|
||||||
$entries[$entry_key]->{$nutrient['value']} += $item->{$nutrient['value']} * $nutrient_multiplier;
|
$entries[$entry_key]->{$nutrient} += $item->{$nutrient} * $nutrient_multiplier;
|
||||||
}
|
}
|
||||||
$entries[$entry_key]->foods->add($item);
|
$entries[$entry_key]->foods->add($item);
|
||||||
}
|
}
|
||||||
elseif ($ingredient['type'] == Recipe::class) {
|
elseif ($ingredient['type'] == Recipe::class) {
|
||||||
$item = Recipe::whereId($ingredient['id'])->first();
|
$item = Recipe::whereId($ingredient['id'])->first();
|
||||||
foreach (Nutrients::$all as $nutrient) {
|
foreach (Nutrients::all()->pluck('value') as $nutrient) {
|
||||||
$entries[$entry_key]->{$nutrient['value']} += Nutrients::calculateRecipeNutrientAmount(
|
$entries[$entry_key]->{$nutrient} += Nutrients::calculateRecipeNutrientAmount(
|
||||||
$item,
|
$item,
|
||||||
$nutrient['value'],
|
$nutrient,
|
||||||
Number::floatFromString($ingredient['amount']),
|
Number::floatFromString($ingredient['amount']),
|
||||||
$ingredient['unit']
|
$ingredient['unit']
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,7 @@ final class Goal extends Model
|
||||||
*/
|
*/
|
||||||
public static function getNameOptions(): array {
|
public static function getNameOptions(): array {
|
||||||
$options = [];
|
$options = [];
|
||||||
foreach (Nutrients::$all as $nutrient) {
|
foreach (Nutrients::all() as $nutrient) {
|
||||||
$options[$nutrient['value']] = [
|
$options[$nutrient['value']] = [
|
||||||
'value' => $nutrient['value'],
|
'value' => $nutrient['value'],
|
||||||
'label' => $nutrient['label'],
|
'label' => $nutrient['label'],
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,7 @@ final class IngredientAmount extends Model
|
||||||
*/
|
*/
|
||||||
public function getNutrientsSummaryAttribute(): string {
|
public function getNutrientsSummaryAttribute(): string {
|
||||||
$summary = [];
|
$summary = [];
|
||||||
foreach (Nutrients::$all as $nutrient) {
|
foreach (Nutrients::all() as $nutrient) {
|
||||||
$amount = round($this->{$nutrient['value']}(), 2);
|
$amount = round($this->{$nutrient['value']}(), 2);
|
||||||
$summary[] = "{$nutrient['label']}: {$amount}";
|
$summary[] = "{$nutrient['label']}: {$amount}";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,20 +4,12 @@ namespace App\Support;
|
||||||
|
|
||||||
use App\Models\Food;
|
use App\Models\Food;
|
||||||
use App\Models\Recipe;
|
use App\Models\Recipe;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
class Nutrients
|
class Nutrients
|
||||||
{
|
{
|
||||||
public static float $gramsPerOunce = 28.349523125;
|
public static float $gramsPerOunce = 28.349523125;
|
||||||
|
|
||||||
public static array $all = [
|
|
||||||
'calories' => ['value' => 'calories', 'label' => 'calories', 'unit' => null],
|
|
||||||
'carbohydrates' => ['value' => 'carbohydrates', 'label' => 'carbohydrates', 'unit' => 'g'],
|
|
||||||
'cholesterol' => ['value' => 'cholesterol', 'label' => 'cholesterol', 'unit' => 'mg'],
|
|
||||||
'fat' => ['value' => 'fat', 'label' => 'fat', 'unit' => 'g'],
|
|
||||||
'protein' => ['value' => 'protein', 'label' => 'protein', 'unit' => 'g'],
|
|
||||||
'sodium' => ['value' => 'sodium', 'label' => 'sodium', 'unit' => 'mg'],
|
|
||||||
];
|
|
||||||
|
|
||||||
public static array $units = [
|
public static array $units = [
|
||||||
'cup' => ['value' => 'cup', 'label' => 'cup'],
|
'cup' => ['value' => 'cup', 'label' => 'cup'],
|
||||||
'gram' => ['value' => 'gram', 'label' => 'grams'],
|
'gram' => ['value' => 'gram', 'label' => 'grams'],
|
||||||
|
|
@ -27,6 +19,56 @@ class Nutrients
|
||||||
'tsp' => ['value' => 'tsp', 'label' => 'tsp.'],
|
'tsp' => ['value' => 'tsp', 'label' => 'tsp.'],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all trackable "nutrients" (calories are not technically a nutrient).
|
||||||
|
*
|
||||||
|
* Each entry has four keys:
|
||||||
|
* - value: Machine name for the entry.
|
||||||
|
* - label: Human-readable name for the entry.
|
||||||
|
* - unit: Unit of measure for the entry.
|
||||||
|
* - weight: Sort weight for presentation.
|
||||||
|
*/
|
||||||
|
public static function all(): Collection {
|
||||||
|
return new Collection([
|
||||||
|
'calories' => [
|
||||||
|
'value' => 'calories',
|
||||||
|
'label' => 'calories',
|
||||||
|
'unit' => null,
|
||||||
|
'weight' => 0,
|
||||||
|
],
|
||||||
|
'carbohydrates' => [
|
||||||
|
'value' => 'carbohydrates',
|
||||||
|
'label' => 'carbohydrates',
|
||||||
|
'unit' => 'g',
|
||||||
|
'weight' => 40,
|
||||||
|
],
|
||||||
|
'cholesterol' => [
|
||||||
|
'value' => 'cholesterol',
|
||||||
|
'label' => 'cholesterol',
|
||||||
|
'unit' => 'mg',
|
||||||
|
'weight' => 20,
|
||||||
|
],
|
||||||
|
'fat' => [
|
||||||
|
'value' => 'fat',
|
||||||
|
'label' => 'fat',
|
||||||
|
'unit' => 'g',
|
||||||
|
'weight' => 10,
|
||||||
|
],
|
||||||
|
'protein' => [
|
||||||
|
'value' => 'protein',
|
||||||
|
'label' => 'protein',
|
||||||
|
'unit' => 'g',
|
||||||
|
'weight' => 50,
|
||||||
|
],
|
||||||
|
'sodium' => [
|
||||||
|
'value' => 'sodium',
|
||||||
|
'label' => 'sodium',
|
||||||
|
'unit' => 'mg',
|
||||||
|
'weight' => 30,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate a nutrient multiplier for a Food.
|
* Calculate a nutrient multiplier for a Food.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="flex flex-col space-y-4 md:flex-row md:space-y-0">
|
<div class="flex flex-col space-y-4 md:flex-row md:space-y-0">
|
||||||
@foreach (\App\Support\Nutrients::$all as $nutrient)
|
@foreach (\App\Support\Nutrients::all()->sortBy('weight') as $nutrient)
|
||||||
<!-- {{ ucfirst($nutrient['value']) }} -->
|
<!-- {{ ucfirst($nutrient['value']) }} -->
|
||||||
<div class="flex-auto">
|
<div class="flex-auto">
|
||||||
<x-inputs.label for="{{ $nutrient['value'] }}"
|
<x-inputs.label for="{{ $nutrient['value'] }}"
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="flex flex-col space-y-4 md:flex-row md:space-x-4 md:space-y-0 w-full">
|
<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)
|
@foreach (\App\Support\Nutrients::all()->sortBy('weight') as $nutrient)
|
||||||
<!-- {{ ucfirst($nutrient['value']) }} -->
|
<!-- {{ ucfirst($nutrient['value']) }} -->
|
||||||
<div class="flex-auto">
|
<div class="flex-auto">
|
||||||
<x-inputs.label for="{{ $nutrient['value'] }}"
|
<x-inputs.label for="{{ $nutrient['value'] }}"
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,7 @@
|
||||||
<div class="ml-2 w-full"><hr/></div>
|
<div class="ml-2 w-full"><hr/></div>
|
||||||
</div>
|
</div>
|
||||||
<span class="text-sm text-gray-500">
|
<span class="text-sm text-gray-500">
|
||||||
@foreach(\App\Support\Nutrients::$all as $nutrient)
|
@foreach(\App\Support\Nutrients::all()->sortBy('weight') as $nutrient)
|
||||||
{{ round($entries->where('meal', $meal)->sum($nutrient['value']), 2) }}{{ $nutrient['unit'] }}
|
{{ round($entries->where('meal', $meal)->sum($nutrient['value']), 2) }}{{ $nutrient['unit'] }}
|
||||||
{{ $nutrient['value'] }}@if(!$loop->last), @endif
|
{{ $nutrient['value'] }}@if(!$loop->last), @endif
|
||||||
@endforeach
|
@endforeach
|
||||||
|
|
@ -127,7 +127,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<span class="font-bold">nutrients:</span>
|
<span class="font-bold">nutrients:</span>
|
||||||
@foreach(\App\Support\Nutrients::$all as $nutrient)
|
@foreach(\App\Support\Nutrients::all()->sortBy('weight') as $nutrient)
|
||||||
{{ round($entry->{$nutrient['value']}, 2) }}{{ $nutrient['unit'] }}
|
{{ round($entry->{$nutrient['value']}, 2) }}{{ $nutrient['unit'] }}
|
||||||
{{ $nutrient['value'] }}@if(!$loop->last), @endif
|
{{ $nutrient['value'] }}@if(!$loop->last), @endif
|
||||||
@endforeach
|
@endforeach
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue