diff --git a/app/Http/Controllers/GoalController.php b/app/Http/Controllers/GoalController.php index 8e176f5..c1584b5 100644 --- a/app/Http/Controllers/GoalController.php +++ b/app/Http/Controllers/GoalController.php @@ -6,6 +6,7 @@ use App\Models\Goal; use Illuminate\Contracts\View\View; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Auth; class GoalController extends Controller @@ -13,9 +14,18 @@ class GoalController extends Controller /** * Display a listing of the resource. */ - public function index(): View + public function index(Request $request): View { - return view('goals.index'); + if ($request->date) { + $date = Carbon::createFromFormat('Y-m-d', $request->date); + } + else { + $date = Carbon::now(); + } + return view('goals.index') + ->with('date', $date) + ->with('goals', Auth::user()->getGoalsByTime($date)) + ->with('goalOptions', Goal::getGoalOptions()); } /** @@ -77,7 +87,9 @@ class GoalController extends Controller */ public function delete(Goal $goal): View { - return view('goals.delete')->with('goal', $goal); + return view('goals.delete') + ->with('goal', $goal) + ->with('goalOptions', Goal::getGoalOptions()); } /** diff --git a/app/Models/Goal.php b/app/Models/Goal.php index fd21454..3c2cc3f 100644 --- a/app/Models/Goal.php +++ b/app/Models/Goal.php @@ -74,6 +74,7 @@ final class Goal extends Model $options[$key] = [ 'value' => $key, 'label' => "{$nutrient['value']} per day", + 'unit' => $nutrient['unit'], ]; } return $options; diff --git a/app/Models/User.php b/app/Models/User.php index 667603a..5aa02c4 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -6,6 +6,8 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Auth; /** * App\Models\User @@ -69,4 +71,31 @@ final class User extends Authenticatable public function goals(): HasMany { return $this->hasMany(Goal::class); } + + /** + * Get User's past, present, and future goals. + * + * @return \App\Models\Goal[] + */ + public function getGoalsByTime(?Carbon $date = null): array { + $now = $date ?? Carbon::now(); + $goals = ['past' => [], 'present' => [], 'future' => []]; + Goal::all()->where('user_id', Auth::user()->id) + ->each(function ($item) use(&$goals, $now) { + if ($item->to && $now->isAfter($item->to)) { + $goals['past'][$item->id] = $item; + } + elseif ($item->from && $now->isBefore($item->from)) { + $goals['future'][$item->id] = $item; + } + elseif ( + empty($item->from) + || empty($item->to) + || $now->isBetween($item->from, $item->to) + ) { + $goals['present'][$item->id] = $item; + } + }); + return $goals; + } } diff --git a/resources/views/goals/delete.blade.php b/resources/views/goals/delete.blade.php index 096ac93..ac34599 100644 --- a/resources/views/goals/delete.blade.php +++ b/resources/views/goals/delete.blade.php @@ -13,7 +13,7 @@ @method('delete') @csrf
- Are you sure what to delete your {{ $goal->goal }} goal? + Are you sure what to delete your {{ $goalOptions[$goal->goal]['label'] }} goal?
Yes, delete diff --git a/resources/views/goals/index.blade.php b/resources/views/goals/index.blade.php index d58e6c6..924ec96 100644 --- a/resources/views/goals/index.blade.php +++ b/resources/views/goals/index.blade.php @@ -1,7 +1,28 @@