From 91fd85ef8325040f315ac0a8032b7bdd2aec7b05 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Fri, 14 May 2021 20:56:13 -0700 Subject: [PATCH] Add date goal handling for user journal --- .../Controllers/JournalEntryController.php | 21 ++++++--------- app/Models/Goal.php | 8 ++++++ app/Models/User.php | 14 +++++----- .../views/journal-entries/index.blade.php | 27 ++++++++++++------- 4 files changed, 41 insertions(+), 29 deletions(-) diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index 166e73b..ae78721 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -42,25 +42,20 @@ class JournalEntryController extends Controller } // Get daily goals data for user. - $goals = Auth::user()->getGoalsByTime($date); - $dailyGoals = []; - foreach (Nutrients::all()->pluck('value') as $nutrient) { - $goal = $goals['present'] - ->where('frequency', 'daily') - ->where('name', $nutrient) - ->first(); - if ($goal) { - $dailyGoals[$goal->name] = round($sums[$goal->name] / $goal->goal * 100); - if ($dailyGoals[$goal->name] > 0) { - $dailyGoals[$goal->name] .= '%'; - } + $goal = Auth::user()->getGoalByDate($date); + $goalProgress = []; + if ($goal) { + foreach (Nutrients::all()->pluck('value') as $nutrient) { + $goalProgress[$nutrient] = round($sums[$nutrient] / $goal->{$nutrient} * 100); + $goalProgress[$nutrient] .= '%'; } } return view('journal-entries.index') ->with('entries', $entries) ->with('sums', $sums) - ->with('dailyGoals', $dailyGoals) + ->with('goal', $goal) + ->with('goalProgress', $goalProgress) ->with('date', $date); } diff --git a/app/Models/Goal.php b/app/Models/Goal.php index 0181374..73b59cf 100644 --- a/app/Models/Goal.php +++ b/app/Models/Goal.php @@ -81,6 +81,7 @@ final class Goal extends Model * Each entry has the following keys: * - value: Day value (used for bitwise operations). * - label: Human-readable name for the day. + * - dow: ISO-8601 numeric representation of the day of the week. */ public static function days(): Collection { @@ -88,30 +89,37 @@ final class Goal extends Model [ 'value' => 1, 'label' => 'monday', + 'dow' => 1, ], [ 'value' => 2, 'label' => 'tuesday', + 'dow' => 2, ], [ 'value' => 4, 'label' => 'wednesday', + 'dow' => 3, ], [ 'value' => 8, 'label' => 'thursday', + 'dow' => 4, ], [ 'value' => 16, 'label' => 'friday', + 'dow' => 5, ], [ 'value' => 32, 'label' => 'saturday', + 'dow' => 6, ], [ 'value' => 64, 'label' => 'sunday', + 'dow' => 7, ], ]); } diff --git a/app/Models/User.php b/app/Models/User.php index 9cfa537..bf9f51a 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -106,14 +106,14 @@ final class User extends Authenticatable implements HasMedia } /** - * Get User's past, present, and future goals. - * - * @todo Refactor or remove as needed. - * - * @return \Illuminate\Support\Collection[] + * Get user's goal (if one exists) for a specific date. */ - public function getGoalsByTime(?Carbon $date = null): array { - return ['past' => new Collection(), 'present' => new Collection(), 'future' => new Collection()]; + public function getGoalByDate(Carbon $date): ?Goal { + $day = Goal::days()->firstWhere('dow', $date->format('N')); + if (!$day) { + throw new \BadMethodCallException("No day with `dow` value {$date->format('N')}."); + } + return $this->goals()->whereRaw("(days & {$day['value']}) != 0")->get()->first(); } /** diff --git a/resources/views/journal-entries/index.blade.php b/resources/views/journal-entries/index.blade.php index 8acf327..8a8d893 100644 --- a/resources/views/journal-entries/index.blade.php +++ b/resources/views/journal-entries/index.blade.php @@ -51,7 +51,7 @@ {{ number_format($sums['calories']) }}
- {{ $dailyGoals['calories'] ?? 'N/A' }} + {{ $goalProgress['calories'] ?? 'N/A' }}
@@ -60,7 +60,7 @@ {{ number_format($sums['fat']) }}g
- {{ $dailyGoals['fat'] ?? 'N/A' }} + {{ $goalProgress['fat'] ?? 'N/A' }}
@@ -69,7 +69,7 @@ {{ number_format($sums['cholesterol']) }}mg
- {{ $dailyGoals['cholesterol'] ?? 'N/A' }} + {{ $goalProgress['cholesterol'] ?? 'N/A' }}
@@ -78,7 +78,7 @@ {{ number_format($sums['sodium']) }}mg
- {{ $dailyGoals['sodium'] ?? 'N/A' }} + {{ $goalProgress['sodium'] ?? 'N/A' }}
@@ -87,7 +87,7 @@ {{ number_format($sums['carbohydrates']) }}g
- {{ $dailyGoals['carbohydrates'] ?? 'N/A' }} + {{ $goalProgress['carbohydrates'] ?? 'N/A' }}
@@ -96,9 +96,18 @@ {{ number_format($sums['protein']) }}g
- {{ $dailyGoals['protein'] ?? 'N/A' }} + {{ $goalProgress['protein'] ?? 'N/A' }}
+

Goal

+ @empty($goal) +
No goal.
+ @else + + {{ $goal->name }} + + @endempty
@foreach(['breakfast', 'lunch', 'dinner', 'snacks'] as $meal) @@ -110,9 +119,9 @@
@foreach(\App\Support\Nutrients::all()->sortBy('weight') as $nutrient) - {{ \App\Support\Nutrients::round($entries->where('meal', $meal)->sum($nutrient['value']), $nutrient['value']) }}{{ $nutrient['unit'] }} - {{ $nutrient['value'] }}@if(!$loop->last), @endif - @endforeach + {{ \App\Support\Nutrients::round($entries->where('meal', $meal)->sum($nutrient['value']), $nutrient['value']) }}{{ $nutrient['unit'] }} + {{ $nutrient['value'] }}@if(!$loop->last), @endif + @endforeach @forelse($entries->where('meal', $meal) as $entry)