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?
+
+
+ {{ ($goal->exists ? 'Edit' : 'Add') }} Goal
+
+ Goals
+
+
+ Add Goal
+
+
+ TODO: GOAL NAME
+
+
+
+
+
+
+
+