Audition status screen updated to use Tabulation service

This commit is contained in:
Matt Young 2024-06-13 20:26:07 -05:00
parent b970828b31
commit 6386a204d7
3 changed files with 24 additions and 22 deletions

View File

@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
use App\Models\Audition;
use App\Models\Entry;
use App\Models\ScoreSheet;
use App\Services\TabulationService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use function compact;
@ -14,14 +15,19 @@ use function redirect;
class TabulationController extends Controller
{
protected $tabulationService;
public function __construct(TabulationService $tabulationService)
{
$this->tabulationService = $tabulationService;
}
public function status()
{
$auditions = Audition::with(['entries' => function($query) {
$query->withCount('scoreSheets');
},'room.judges'])->orderBy('score_order')->get();
// $auditions = Audition::with(['entries' => function($query) {
// $query->withCount('scoreSheets');
// },'room.judges'])->orderBy('score_order')->get();
$auditions = $this->tabulationService->getAuditionsWithStatus();
return view('tabulation.status',compact('auditions'));
}

View File

@ -4,6 +4,7 @@ namespace App\Services;
use App\Models\Audition;
use App\Models\Entry;
use App\Models\ScoreSheet;
use Illuminate\Support\Facades\Cache;
use App\Services\AuditionCacheService;
use Illuminate\Support\Facades\DB;
@ -32,8 +33,7 @@ class TabulationService
public function getAuditionsWithStatus()
{
// Create an array with the number of scores for each entry
$scoreCountByEntry = DB::table('score_sheets')
->select('entry_id', DB::raw('count(*) as count'))
$scoreCountByEntry = ScoreSheet::select('entry_id', DB::raw('count(*) as count'))
->groupBy('entry_id')
->get()
->pluck('count','entry_id');
@ -42,20 +42,16 @@ class TabulationService
$auditions = $this->auditionCacheService->getAuditions();
$auditions->load('entries');
$auditions->loadCount('judges');
$auditions->loadCount('entries');
// Eager load the count of related models
$auditions->loadCount(['judges', 'entries']);
foreach ($auditions as $audition) {
$audition->scored_entries_count = 0;
foreach ($audition->entries as $entry) {
if ($audition->judges_count == $scoreCountByEntry[$entry->id]) {
$entry->fully_scored = true;
$audition->scored_entries_count++;
} else {
$entry->fully_scored = false;
}
}
}
return $auditions;
// Iterate over the auditions and calculate the scored_entries_count
return $auditions->map(function ($audition) use ($scoreCountByEntry) {
$audition->scored_entries_count = $audition->entries->reduce(function ($carry, $entry) use ($audition, $scoreCountByEntry) {
$entry->fully_scored = $audition->judges_count == $scoreCountByEntry[$entry->id];
return $carry + ($entry->fully_scored ? 1 : 0);
}, 0);
return $audition;
});
}
}

View File

@ -18,9 +18,9 @@
<x-table.td><a href="/tabulation/auditions/{{ $audition->id }}">
{{ $audition->name }}
</a></x-table.td>
<x-table.td>{{ $audition->scoredEntries()->count() }} / {{ $audition->entries->count() }} Scored</x-table.td>
<x-table.td>{{ $audition->scored_entries_count }} / {{ $audition->entries_count }} Scored</x-table.td>
<td>
@if($audition->scoringIsComplete())
@if( $audition->scored_entries_count == $audition->entries_count)
<x-icons.checkmark color="green"/>
@endif
</td>