From 4a7f8c13d17fbb4af32093ed9c1f1157841b229c Mon Sep 17 00:00:00 2001 From: Matt Young Date: Sun, 14 Jul 2024 17:06:57 -0500 Subject: [PATCH] Update judge score entry to use EnterScore action instead of doing the work in the controller --- app/Http/Controllers/JudgingController.php | 86 +++++++++++++--------- 1 file changed, 50 insertions(+), 36 deletions(-) diff --git a/app/Http/Controllers/JudgingController.php b/app/Http/Controllers/JudgingController.php index 0f0a6ee..292617e 100644 --- a/app/Http/Controllers/JudgingController.php +++ b/app/Http/Controllers/JudgingController.php @@ -2,10 +2,14 @@ namespace App\Http\Controllers; +use App\Actions\Tabulation\EnterScore; +use App\Exceptions\AuditionServiceException; +use App\Exceptions\ScoreEntryException; use App\Models\Audition; use App\Models\Entry; use App\Models\JudgeAdvancementVote; use App\Models\ScoreSheet; +use App\Services\AuditionService; use Exception; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -17,6 +21,13 @@ use function url; class JudgingController extends Controller { + protected AuditionService $auditionService; + + public function __construct(AuditionService $auditionService) + { + $this->auditionService = $auditionService; + } + public function index() { $rooms = Auth::user()->judgingAssignments; @@ -62,32 +73,33 @@ class JudgingController extends Controller return view('judging.entry_score_sheet', compact('entry', 'oldSheet', 'oldVote')); } - public function saveScoreSheet(Request $request, Entry $entry) + public function saveScoreSheet(Request $request, Entry $entry, EnterScore $enterScore) { if ($request->user()->cannot('judge', $entry->audition)) { abort(403, 'You are not assigned to judge this entry'); } - // TODO extract to a service class - $scoringGuide = $entry->audition->scoringGuide()->with('subscores')->first(); - $scoreValidation = $scoringGuide->validateScores($request->input('score')); - if ($scoreValidation != 'success') { - return redirect(url()->previous())->with('error', $scoreValidation)->with('oldScores', $request->all()); - } - $scoreSheetArray = []; - foreach ($scoringGuide->subscores as $subscore) { - $scoreSheetArray[$subscore->id] = [ - 'score' => $request->input('score')[$subscore->id], - 'subscore_id' => $subscore->id, - 'subscore_name' => $subscore->name, - ]; + + // Validate form data + try { + $subscores = $this->auditionService->getSubscores($entry->audition); + } catch (AuditionServiceException $e) { + return redirect()->back()->with('error', 'Unable to get subscores - '.$e->getMessage()); } - ScoreSheet::create([ - 'user_id' => Auth::user()->id, - 'entry_id' => $entry->id, - 'subscores' => $scoreSheetArray, - ]); + $validationChecks = []; + foreach ($subscores as $subscore) { + $validationChecks['score'.'.'.$subscore->id] = 'required|integer|max:'.$subscore->max_score; + } + $validatedData = $request->validate($validationChecks); + // Enter the score + try { + $enterScore(Auth::user(), $entry, $validatedData['score']); + } catch (ScoreEntryException $e) { + return redirect()->back()->with('error', 'Error saving score - '.$e->getMessage()); + } + + // Deal with an advancement vote if needed $this->advancementVote($request, $entry); return redirect('/judging/audition/'.$entry->audition_id)->with('success', @@ -95,34 +107,36 @@ class JudgingController extends Controller } - public function updateScoreSheet(Request $request, Entry $entry) + public function updateScoreSheet(Request $request, Entry $entry, EnterScore $enterScore) { if ($request->user()->cannot('judge', $entry->audition)) { abort(403, 'You are not assigned to judge this entry'); } $scoreSheet = ScoreSheet::where('user_id', Auth::id())->where('entry_id', $entry->id)->first(); if (! $scoreSheet) { - return redirect()->back()->with('error', 'Attempt to edit non existent entry'); + return redirect()->back()->with('error', 'Attempt to edit non existent score sheet'); } Gate::authorize('update', $scoreSheet); - $scoringGuide = $entry->audition->scoringGuide()->with('subscores')->first(); - $scoreValidation = $scoringGuide->validateScores($request->input('score')); - if ($scoreValidation != 'success') { - return redirect(url()->previous())->with('error', $scoreValidation)->with('oldScores', $request->all()); - } - $scoreSheetArray = []; - foreach ($scoringGuide->subscores as $subscore) { - $scoreSheetArray[$subscore->id] = [ - 'score' => $request->input('score')[$subscore->id], - 'subscore_id' => $subscore->id, - 'subscore_name' => $subscore->name, - ]; + // Validate form data + try { + $subscores = $this->auditionService->getSubscores($entry->audition); + } catch (AuditionServiceException $e) { + return redirect()->back()->with('error', 'Error getting subscores - '.$e->getMessage()); } - $scoreSheet->update([ - 'subscores' => $scoreSheetArray, - ]); + $validationChecks = []; + foreach ($subscores as $subscore) { + $validationChecks['score'.'.'.$subscore->id] = 'required|integer|max:'.$subscore->max_score; + } + $validatedData = $request->validate($validationChecks); + + // Enter the score + try { + $enterScore(Auth::user(), $entry, $validatedData['score'], $scoreSheet); + } catch (ScoreEntryException $e) { + return redirect()->back()->with('error', 'Error updating score - '.$e->getMessage()); + } $this->advancementVote($request, $entry);