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']) }}