diff --git a/app/Http/Controllers/Tabulation/TabulationController.php b/app/Http/Controllers/Tabulation/TabulationController.php
index fc7e5b3..231f7b1 100644
--- a/app/Http/Controllers/Tabulation/TabulationController.php
+++ b/app/Http/Controllers/Tabulation/TabulationController.php
@@ -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'));
}
diff --git a/app/Services/TabulationService.php b/app/Services/TabulationService.php
index d6e736e..2dbca7a 100644
--- a/app/Services/TabulationService.php
+++ b/app/Services/TabulationService.php
@@ -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;
+ });
}
}
diff --git a/resources/views/tabulation/status.blade.php b/resources/views/tabulation/status.blade.php
index a637c92..8be7115 100644
--- a/resources/views/tabulation/status.blade.php
+++ b/resources/views/tabulation/status.blade.php
@@ -18,9 +18,9 @@