From f1d3ba349cc91df4a6b6ee9e35f6c4e653e171d3 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Tue, 16 Jul 2024 03:01:11 -0500 Subject: [PATCH] Bonus Score Admin Entry #20 Implement bonus scores Admin bonus score entry complete --- app/Actions/Tabulation/EnterBonusScore.php | 2 + .../Tabulation/BonusScoreController.php | 43 ++++++++++++++++++- .../tabulation/bonus-score-sheet.blade.php | 14 +++--- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/app/Actions/Tabulation/EnterBonusScore.php b/app/Actions/Tabulation/EnterBonusScore.php index 67f8570..2706f34 100644 --- a/app/Actions/Tabulation/EnterBonusScore.php +++ b/app/Actions/Tabulation/EnterBonusScore.php @@ -19,6 +19,7 @@ class EnterBonusScore public function __invoke(User $judge, Entry $entry, int $score): void { + $getRelatedEntries = App::make(GetBonusScoreRelatedEntries::class); $this->basicValidations($judge, $entry); $this->validateJudgeValidity($judge, $entry, $score); @@ -33,6 +34,7 @@ class EnterBonusScore 'score' => $score, ]); } + } protected function getRelatedEntries(Entry $entry): Collection diff --git a/app/Http/Controllers/Tabulation/BonusScoreController.php b/app/Http/Controllers/Tabulation/BonusScoreController.php index c1191c6..57cfbaa 100644 --- a/app/Http/Controllers/Tabulation/BonusScoreController.php +++ b/app/Http/Controllers/Tabulation/BonusScoreController.php @@ -2,10 +2,14 @@ namespace App\Http\Controllers\Tabulation; +use App\Actions\Tabulation\EnterBonusScore; use App\Actions\Tabulation\GetBonusScoreRelatedEntries; +use App\Exceptions\ScoreEntryException; use App\Http\Controllers\Controller; use App\Models\BonusScore; use App\Models\Entry; +use App\Models\User; +use Illuminate\Support\Facades\DB; use function request; @@ -42,9 +46,46 @@ class BonusScoreController extends Controller compact('entry', 'bonusScoreDefinition', 'assignedJudges', 'existingScores', 'relatedEntries')); } - public function saveEntryBonusScoreSheet() + public function saveEntryBonusScoreSheet(Entry $entry, GetBonusScoreRelatedEntries $getRelatedEntries, EnterBonusScore $saveBonusScore) { + $validData = request()->validate([ + 'judge_id' => 'required|exists:users,id', + 'entry_id' => 'required|exists:entries,id', + 'score' => 'nullable|numeric', + ]); + $judge = User::find($validData['judge_id']); + $entry = Entry::find($validData['entry_id']); + $relatedEntries = $getRelatedEntries($entry); + try { + DB::beginTransaction(); + + // Delete existing bonus scores for the entries by the judge + foreach ($relatedEntries as $related) { + BonusScore::where('entry_id', $related->id)->where('user_id', $judge->id)->delete(); + } + + // If no score was submitted, were going to just stop at deleting the scores + if (! $validData['score'] == null) { + // Set the new score + try { + $saveBonusScore($judge, $entry, $validData['score']); + } catch (ScoreEntryException $ex) { + DB::rollBack(); + + return redirect()->route('bonus-scores.entryBonusScoreSheet', + ['entry_id' => $entry->id])->with('error', 'Error entering score - '.$ex->getMessage()); + } + } + + DB::commit(); + } catch (\Exception) { + DB::rollBack(); + + return redirect()->route('bonus-scores.entryBonusScoreSheet', ['entry_id' => $entry->id])->with('error', 'Error entering score - '.$ex->getMessage()); + } + + return redirect()->route('bonus-scores.entryBonusScoreSheet', ['entry_id' => $entry->id])->with('success', 'New bonus score entered'); } public function destroyBonusScore() diff --git a/resources/views/tabulation/bonus-score-sheet.blade.php b/resources/views/tabulation/bonus-score-sheet.blade.php index cdf63aa..1d49969 100644 --- a/resources/views/tabulation/bonus-score-sheet.blade.php +++ b/resources/views/tabulation/bonus-score-sheet.blade.php @@ -46,23 +46,25 @@ Enter Score
- NOTE: Entering score will delete any existing scores for that entry by that judge +

NOTE: Entering score will delete any existing scores for that entry by that judge

+

Submitting the form with no score value will delete scores by that judge

- - + + Judge + @foreach($assignedJudges as $judge) @endforeach - + Scored Audition @foreach($relatedEntries as $related) @endforeach - - Enter Score + + Enter Score