mirror of https://github.com/kcal-app/kcal.git
Finalize goal change controller
This commit is contained in:
parent
a310540733
commit
1f5b61304f
|
|
@ -14,8 +14,11 @@ class JournalDateController extends Controller
|
||||||
*/
|
*/
|
||||||
public function updateGoal(Request $request, JournalDate $journalDate): RedirectResponse
|
public function updateGoal(Request $request, JournalDate $journalDate): RedirectResponse
|
||||||
{
|
{
|
||||||
// @todo Implement goal up behavior.
|
$attributes = $request->validate(['goal' => 'exists:App\Models\Goal,id']);
|
||||||
return redirect()->route('journal-entries.show');
|
$journalDate->goal()->associate($attributes['goal'])->save();
|
||||||
|
return redirect()->route('journal-entries.index', [
|
||||||
|
'date' => $journalDate->date->format('Y-m-d')
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ use App\Http\Requests\StoreFromNutrientsJournalEntryRequest;
|
||||||
use App\Http\Requests\StoreJournalEntryRequest;
|
use App\Http\Requests\StoreJournalEntryRequest;
|
||||||
use App\Models\Food;
|
use App\Models\Food;
|
||||||
use App\Models\Goal;
|
use App\Models\Goal;
|
||||||
|
use App\Models\JournalDate;
|
||||||
use App\Models\JournalEntry;
|
use App\Models\JournalEntry;
|
||||||
use App\Models\Recipe;
|
use App\Models\Recipe;
|
||||||
use App\Support\ArrayFormat;
|
use App\Support\ArrayFormat;
|
||||||
|
|
@ -62,12 +63,17 @@ class JournalEntryController extends Controller
|
||||||
return ['value' => $goal->id, 'label' => $goal->name];
|
return ['value' => $goal->id, 'label' => $goal->name];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Get the associated journal date.
|
||||||
|
// @todo Refactor journal date as a relationship on journal entries.
|
||||||
|
$journalDate = JournalDate::getOrCreateJournalDate(Auth::user(), $date);
|
||||||
|
|
||||||
return view('journal-entries.index')
|
return view('journal-entries.index')
|
||||||
->with('entries', $entries)
|
->with('entries', $entries)
|
||||||
->with('sums', $sums)
|
->with('sums', $sums)
|
||||||
->with('currentGoal', $goal)
|
->with('currentGoal', $goal)
|
||||||
->with('goalProgress', $goalProgress)
|
->with('goalProgress', $goalProgress)
|
||||||
->with('goalOptions', $goalOptions)
|
->with('goalOptions', $goalOptions)
|
||||||
|
->with('journalDate', $journalDate)
|
||||||
->with('date', $date);
|
->with('date', $date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ namespace App\Models;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* App\Models\JournalDate
|
* App\Models\JournalDate
|
||||||
|
|
@ -60,4 +61,24 @@ final class JournalDate extends Model
|
||||||
public function user(): BelongsTo {
|
public function user(): BelongsTo {
|
||||||
return $this->belongsTo(User::class);
|
return $this->belongsTo(User::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a journal date for a user and date, creating a new one if necessary.
|
||||||
|
*
|
||||||
|
* @param \App\Models\User $user
|
||||||
|
* User.
|
||||||
|
* @param \Illuminate\Support\Carbon $date
|
||||||
|
* Date.
|
||||||
|
*
|
||||||
|
* @return \App\Models\JournalDate
|
||||||
|
* Journal date for provided user and date.
|
||||||
|
*/
|
||||||
|
public static function getOrCreateJournalDate(User $user, Carbon $date): JournalDate {
|
||||||
|
/** @var \App\Models\JournalDate $journal_date */
|
||||||
|
$journal_date = $user->journalDates()->whereDate('date', '=', $date)->first();
|
||||||
|
if (empty($journal_date)) {
|
||||||
|
$journal_date = JournalDate::make(['date' => $date])->user()->associate($user);
|
||||||
|
}
|
||||||
|
return $journal_date;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -120,11 +120,7 @@ final class User extends Authenticatable implements HasMedia
|
||||||
* method also creates a JournalDate if one does not already exist.
|
* method also creates a JournalDate if one does not already exist.
|
||||||
*/
|
*/
|
||||||
public function getGoalByDate(Carbon $date): ?Goal {
|
public function getGoalByDate(Carbon $date): ?Goal {
|
||||||
/** @var \App\Models\JournalDate $journal_date */
|
$journal_date = JournalDate::getOrCreateJournalDate($this, $date);
|
||||||
$journal_date = $this->journalDates()->whereDate('date', '=', $date)->first();
|
|
||||||
if (empty($journal_date)) {
|
|
||||||
$journal_date = JournalDate::make(['date' => $date])->user()->associate(Auth::user());
|
|
||||||
}
|
|
||||||
if ($journal_date->goal) {
|
if ($journal_date->goal) {
|
||||||
return $journal_date->goal;
|
return $journal_date->goal;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,7 @@
|
||||||
{{ $currentGoal->name }}
|
{{ $currentGoal->name }}
|
||||||
</a>
|
</a>
|
||||||
@endempty
|
@endempty
|
||||||
<form method="POST" action="{{ route('journal-dates.update.goal') }}">
|
<form method="POST" action="{{ route('journal-dates.update.goal', $journalDate) }}">
|
||||||
@csrf
|
@csrf
|
||||||
<x-inputs.select name="goal"
|
<x-inputs.select name="goal"
|
||||||
class="block w-full"
|
class="block w-full"
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ Route::middleware(['auth'])->group(function () {
|
||||||
Route::get('/ingredient-picker/search', [IngredientPickerController::class, 'search'])->name('ingredient-picker.search');
|
Route::get('/ingredient-picker/search', [IngredientPickerController::class, 'search'])->name('ingredient-picker.search');
|
||||||
|
|
||||||
// Journal dates.
|
// Journal dates.
|
||||||
Route::post('/journal-dates/update/goal', [JournalDateController::class, 'updateGoal'])->name('journal-dates.update.goal');
|
Route::post('/journal-dates/{journal_date}/update/goal', [JournalDateController::class, 'updateGoal'])->name('journal-dates.update.goal');
|
||||||
|
|
||||||
// Journal entries.
|
// Journal entries.
|
||||||
Route::get('/journal-entries/create/from-nutrients', [JournalEntryController::class, 'createFromNutrients'])->name('journal-entries.create.from-nutrients');
|
Route::get('/journal-entries/create/from-nutrients', [JournalEntryController::class, 'createFromNutrients'])->name('journal-entries.create.from-nutrients');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue