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;
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);