Move functions out of ScoreSheet Model

Closes #35
This commit is contained in:
Matt Young 2024-07-17 20:25:57 -05:00
parent 1373ea8012
commit 8b4fd6870c
4 changed files with 21 additions and 30 deletions

View File

@ -4,7 +4,7 @@ namespace App\Http\Controllers\Admin;
use App\Actions\Entries\CreateEntry; use App\Actions\Entries\CreateEntry;
use App\Actions\Entries\UpdateEntry; use App\Actions\Entries\UpdateEntry;
use App\Actions\Tabulation\CalculateEntryScore; use App\Actions\Tabulation\CalculateScoreSheetTotal;
use App\Exceptions\ManageEntryException; use App\Exceptions\ManageEntryException;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Audition; use App\Models\Audition;
@ -12,6 +12,7 @@ use App\Models\Entry;
use App\Models\School; use App\Models\School;
use App\Models\Seat; use App\Models\Seat;
use App\Models\Student; use App\Models\Student;
use App\Services\ScoreService;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
@ -123,7 +124,7 @@ class EntryController extends Controller
return redirect(route('admin.entries.index'))->with('success', 'The entry has been added.'); return redirect(route('admin.entries.index'))->with('success', 'The entry has been added.');
} }
public function edit(Entry $entry, CalculateEntryScore $calculator) public function edit(Entry $entry, CalculateScoreSheetTotal $calculator, ScoreService $scoreService)
{ {
if ($entry->audition->hasFlag('seats_published')) { if ($entry->audition->hasFlag('seats_published')) {
return to_route('admin.entries.index')->with('error', return to_route('admin.entries.index')->with('error',
@ -138,9 +139,13 @@ class EntryController extends Controller
$students = Student::with('school')->orderBy('last_name')->orderBy('first_name')->get(); $students = Student::with('school')->orderBy('last_name')->orderBy('first_name')->get();
$auditions = Audition::orderBy('score_order')->get(); $auditions = Audition::orderBy('score_order')->get();
$scores = $entry->scoreSheets()->with('audition', 'judge')->get(); $scores = $entry->scoreSheets()->with('audition', 'judge')->get();
$scores->each(fn ($score) => $score->entry = $entry); foreach ($scores as $score) {
$score->entry = $entry;
$score->valid = $scoreService->isScoreSheetValid($score);
$score->seating_total_score = $calculator('seating', $entry, $score->judge)[0];
$score->advancement_total_score = $calculator('advancement', $entry, $score->judge)[0];
}
// return view('admin.entries.edit', ['entry' => $entry, 'students' => $students, 'auditions' => $auditions]);
return view('admin.entries.edit', compact('entry', 'students', 'auditions', 'scores')); return view('admin.entries.edit', compact('entry', 'students', 'auditions', 'scores'));
} }

View File

@ -2,11 +2,9 @@
namespace App\Models; namespace App\Models;
use App\Actions\Tabulation\CalculateScoreSheetTotal;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOneThrough; use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Support\Facades\App;
class ScoreSheet extends Model class ScoreSheet extends Model
{ {
@ -39,23 +37,4 @@ class ScoreSheet extends Model
'audition_id' // Local key on the intermediate model (Entry) 'audition_id' // Local key on the intermediate model (Entry)
); );
} }
public function getSubscore($id)
{
return $this->subscores[$id]['score'] ?? false;
}
public function isValid()
{
$judges = $this->audition->judges;
return $judges->contains('id', $this->judge->id);
}
public function totalScore($mode)
{
$calculator = App::make(CalculateScoreSheetTotal::class);
return $calculator($mode, $this->entry, $this->judge);
}
} }

View File

@ -3,7 +3,7 @@
namespace App\Services; namespace App\Services;
use App\Models\Entry; use App\Models\Entry;
use App\Models\User; use App\Models\ScoreSheet;
class ScoreService class ScoreService
{ {
@ -14,6 +14,7 @@ class ScoreService
{ {
} }
public function isEntryFullyScored(Entry $entry): bool public function isEntryFullyScored(Entry $entry): bool
{ {
$requiredJudges = $entry->audition->judges()->count(); $requiredJudges = $entry->audition->judges()->count();
@ -21,4 +22,11 @@ class ScoreService
return $requiredJudges === $scoreSheets; return $requiredJudges === $scoreSheets;
} }
public function isScoreSheetValid(ScoreSheet $sheet)
{
$judges = $sheet->audition->judges;
return $judges->contains('id', $sheet->judge->id);
}
} }

View File

@ -84,7 +84,6 @@
<x-card.list.body> <x-card.list.body>
<div class="grid sm:grid-cols-3 space-3 m-3"> <div class="grid sm:grid-cols-3 space-3 m-3">
@foreach($scores as $score) @foreach($scores as $score)
@php($score->isValid())
<div class="border p-3"> <div class="border p-3">
<div class="grid grid-cols-2 border-b"> <div class="grid grid-cols-2 border-b">
<span class="font-semibold text-sm">{{ $score->judge->full_name() }}</span> <span class="font-semibold text-sm">{{ $score->judge->full_name() }}</span>
@ -106,16 +105,16 @@
<p class="grid grid-cols-2 border-b"> <p class="grid grid-cols-2 border-b">
<span <span
class="font-semibold text-sm">{{ auditionSetting('auditionAbbreviation') }} Total</span> class="font-semibold text-sm">{{ auditionSetting('auditionAbbreviation') }} Total</span>
<span class="text-right font-semibold">{{ $score->totalScore('seating')[0] }}</span> <span class="text-right font-semibold">{{ $score->seating_total_score }}</span>
</p> </p>
@if( auditionSetting('advanceTo')) @if( auditionSetting('advanceTo'))
<p class="grid grid-cols-2 border-b"> <p class="grid grid-cols-2 border-b">
<span class="font-semibold text-sm">{{ auditionSetting('advanceTo') }} Total</span> <span class="font-semibold text-sm">{{ auditionSetting('advanceTo') }} Total</span>
<span class="text-right font-semibold">{{ $score->totalScore('advancement')[0] }}</span> <span class="text-right font-semibold">{{ $score->advancement_total_score }}</span>
</p> </p>
@endif @endif
@if(! $score->isValid()) @if(! $score->valid))
<div class="bg-red-500 text-white p-2 rounded mt-2"> <div class="bg-red-500 text-white p-2 rounded mt-2">
<p class="text-sm">This score is invalid</p> <p class="text-sm">This score is invalid</p>
</div> </div>