Update judge score entry to use EnterScore action instead of doing the work in the controller

This commit is contained in:
Matt Young 2024-07-14 17:06:57 -05:00
parent bdaf18b6c3
commit 4a7f8c13d1
1 changed files with 50 additions and 36 deletions

View File

@ -2,10 +2,14 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Actions\Tabulation\EnterScore;
use App\Exceptions\AuditionServiceException;
use App\Exceptions\ScoreEntryException;
use App\Models\Audition; use App\Models\Audition;
use App\Models\Entry; use App\Models\Entry;
use App\Models\JudgeAdvancementVote; use App\Models\JudgeAdvancementVote;
use App\Models\ScoreSheet; use App\Models\ScoreSheet;
use App\Services\AuditionService;
use Exception; use Exception;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
@ -17,6 +21,13 @@ use function url;
class JudgingController extends Controller class JudgingController extends Controller
{ {
protected AuditionService $auditionService;
public function __construct(AuditionService $auditionService)
{
$this->auditionService = $auditionService;
}
public function index() public function index()
{ {
$rooms = Auth::user()->judgingAssignments; $rooms = Auth::user()->judgingAssignments;
@ -62,32 +73,33 @@ class JudgingController extends Controller
return view('judging.entry_score_sheet', compact('entry', 'oldSheet', 'oldVote')); 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)) { if ($request->user()->cannot('judge', $entry->audition)) {
abort(403, 'You are not assigned to judge this entry'); abort(403, 'You are not assigned to judge this entry');
} }
// TODO extract to a service class
$scoringGuide = $entry->audition->scoringGuide()->with('subscores')->first(); // Validate form data
$scoreValidation = $scoringGuide->validateScores($request->input('score')); try {
if ($scoreValidation != 'success') { $subscores = $this->auditionService->getSubscores($entry->audition);
return redirect(url()->previous())->with('error', $scoreValidation)->with('oldScores', $request->all()); } catch (AuditionServiceException $e) {
} return redirect()->back()->with('error', 'Unable to get subscores - '.$e->getMessage());
$scoreSheetArray = [];
foreach ($scoringGuide->subscores as $subscore) {
$scoreSheetArray[$subscore->id] = [
'score' => $request->input('score')[$subscore->id],
'subscore_id' => $subscore->id,
'subscore_name' => $subscore->name,
];
} }
ScoreSheet::create([ $validationChecks = [];
'user_id' => Auth::user()->id, foreach ($subscores as $subscore) {
'entry_id' => $entry->id, $validationChecks['score'.'.'.$subscore->id] = 'required|integer|max:'.$subscore->max_score;
'subscores' => $scoreSheetArray, }
]); $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); $this->advancementVote($request, $entry);
return redirect('/judging/audition/'.$entry->audition_id)->with('success', 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)) { if ($request->user()->cannot('judge', $entry->audition)) {
abort(403, 'You are not assigned to judge this entry'); abort(403, 'You are not assigned to judge this entry');
} }
$scoreSheet = ScoreSheet::where('user_id', Auth::id())->where('entry_id', $entry->id)->first(); $scoreSheet = ScoreSheet::where('user_id', Auth::id())->where('entry_id', $entry->id)->first();
if (! $scoreSheet) { 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); Gate::authorize('update', $scoreSheet);
$scoringGuide = $entry->audition->scoringGuide()->with('subscores')->first(); // Validate form data
$scoreValidation = $scoringGuide->validateScores($request->input('score')); try {
if ($scoreValidation != 'success') { $subscores = $this->auditionService->getSubscores($entry->audition);
return redirect(url()->previous())->with('error', $scoreValidation)->with('oldScores', $request->all()); } catch (AuditionServiceException $e) {
} return redirect()->back()->with('error', 'Error getting subscores - '.$e->getMessage());
$scoreSheetArray = [];
foreach ($scoringGuide->subscores as $subscore) {
$scoreSheetArray[$subscore->id] = [
'score' => $request->input('score')[$subscore->id],
'subscore_id' => $subscore->id,
'subscore_name' => $subscore->name,
];
} }
$scoreSheet->update([ $validationChecks = [];
'subscores' => $scoreSheetArray, 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); $this->advancementVote($request, $entry);