diff --git a/app/Http/Controllers/GoalController.php b/app/Http/Controllers/GoalController.php new file mode 100644 index 0000000..d7a5e8b --- /dev/null +++ b/app/Http/Controllers/GoalController.php @@ -0,0 +1,92 @@ +edit(new Goal()); + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request): RedirectResponse + { + return $this->update($request, new Goal()); + } + + /** + * Display the specified resource. + */ + public function show(Goal $goal): View + { + return view('goals.show')->with('goal', $goal); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Goal $goal): View + { + return view('goals.edit') + ->with('goal', $goal) + ->with('attributeOptions', Goal::getAttributeOptions()); + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, Goal $goal): RedirectResponse + { + $attributes = $request->validate([ + 'from' => ['nullable', 'date'], + 'to' => ['nullable', 'date'], + 'attribute' => ['required', 'string'], + 'goal' => ['required', 'numeric'], + ]); + $goal->fill(array_filter($attributes)) + ->user()->associate(Auth::user()); + $goal->save(); + session()->flash('message', "Goal updated!"); + return redirect()->route('goals.show', $goal); + } + + /** + * Confirm removal of specified resource. + */ + public function delete(Goal $goal): View + { + return view('goals.delete')->with('goal', $goal); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(Goal $goal): RedirectResponse + { + $goal->delete(); + return redirect(route('goals.index')) + ->with('message', "Goal deleted!"); + } +} diff --git a/app/Models/Goal.php b/app/Models/Goal.php index 750d914..6f4c8d7 100644 --- a/app/Models/Goal.php +++ b/app/Models/Goal.php @@ -2,9 +2,11 @@ namespace App\Models; +use App\Support\Nutrients; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Support\Str; /** * App\Models\Goal @@ -60,4 +62,23 @@ class Goal extends Model public function user(): BelongsTo { return $this->belongsTo(User::class); } + + /** + * Get options for the "attribute" column. + * + * @return array + */ + public static function getAttributeOptions(): array { + $options = []; + foreach (Nutrients::$all as $nutrient) { + foreach (['daily', 'weekly', 'monthly', 'yearly'] as $frequency) { + $key = "{$nutrient['value']}_{$frequency}"; + $options[$key] = [ + 'value' => $key, + 'label' => Str::ucfirst("{$frequency} {$nutrient['value']}") + ]; + } + } + return $options; + } } diff --git a/resources/views/goals/delete.blade.php b/resources/views/goals/delete.blade.php new file mode 100644 index 0000000..974c58f --- /dev/null +++ b/resources/views/goals/delete.blade.php @@ -0,0 +1,28 @@ + + +

+ Delete {{ $goal->attribute }} Goal? +

+
+ +
+
+
+
+
+ @method('delete') + @csrf +
+ Are you sure what to delete your {{ $goal->attribute }} goal? +
+ + Yes, delete + + No, do not delete +
+
+
+
+
+
diff --git a/resources/views/goals/edit.blade.php b/resources/views/goals/edit.blade.php new file mode 100644 index 0000000..f89fe6e --- /dev/null +++ b/resources/views/goals/edit.blade.php @@ -0,0 +1,70 @@ + + +

+ {{ ($goal->exists ? 'Edit' : 'Add') }} Goal +

+
+ +
+
+
+
+
+ @if ($goal->exists)@method('put')@endif + @csrf +
+
+ +
+ + +
+ + +
+ + +
+ + +
+ + + + + +
+ + +
+ + + +
+
+
+ +
+ + {{ ($goal->exists ? 'Save' : 'Add') }} + +
+
+
+
+
+
+
diff --git a/resources/views/goals/index.blade.php b/resources/views/goals/index.blade.php new file mode 100644 index 0000000..0934b63 --- /dev/null +++ b/resources/views/goals/index.blade.php @@ -0,0 +1,23 @@ + + + + + +
+
+
+
+ TODO: Goals index. +
+
+
+
+
diff --git a/resources/views/goals/show.blade.php b/resources/views/goals/show.blade.php new file mode 100644 index 0000000..a34e08c --- /dev/null +++ b/resources/views/goals/show.blade.php @@ -0,0 +1,29 @@ + + +

+ TODO: GOAL NAME + + + + + + + + + + + +

+
+
+
+
+
+ TODO: GOAL SHOW PAGE +
+
+
+
+
diff --git a/resources/views/layouts/navigation.blade.php b/resources/views/layouts/navigation.blade.php index 268f123..229c0e6 100644 --- a/resources/views/layouts/navigation.blade.php +++ b/resources/views/layouts/navigation.blade.php @@ -62,6 +62,10 @@
{{ Auth::user()->email }}
+
+ Goals +
+
diff --git a/routes/web.php b/routes/web.php index 89af6ab..7a27799 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,7 @@ middleware(['auth']); Route::get('/foods/{food}/delete', [FoodController::class, 'delete'])->middleware(['auth'])->name('foods.delete'); -// Recipes. -Route::resource('recipes', RecipeController::class)->middleware(['auth']); +// Goals. +Route::resource('goals', GoalController::class)->middleware(['auth']); +Route::get('/goals/{goal}/delete', [GoalController::class, 'delete'])->middleware(['auth'])->name('goals.delete'); + +// Ingredient picker. +Route::get('/ingredient-picker/search', [IngredientPickerController::class, 'search'])->middleware(['auth'])->name('ingredient-picker.search'); // Journal entries. Route::get('/journal-entries/create/from-nutrients', [JournalEntryController::class, 'createFromNutrients'])->middleware(['auth'])->name('journal-entries.create.from-nutrients'); @@ -36,8 +41,7 @@ Route::post('/journal-entries/create/from-nutrients', [JournalEntryController::c Route::resource('journal-entries', JournalEntryController::class)->middleware(['auth']); Route::get('/journal-entries/{journalEntry}/delete', [JournalEntryController::class, 'delete'])->middleware(['auth'])->name('journal-entries.delete'); -// Custom. -// TODO: Change this to a custom JSON API endpoint. -Route::get('/ingredient-picker/search', [IngredientPickerController::class, 'search'])->middleware(['auth'])->name('ingredient-picker.search'); +// Recipes. +Route::resource('recipes', RecipeController::class)->middleware(['auth']); require __DIR__.'/auth.php';