diff --git a/app/Http/Controllers/ResultsPage.php b/app/Http/Controllers/ResultsPage.php index e7be902..326d5f6 100644 --- a/app/Http/Controllers/ResultsPage.php +++ b/app/Http/Controllers/ResultsPage.php @@ -2,14 +2,18 @@ namespace App\Http\Controllers; +use App\Models\Seat; use App\Services\AuditionCacheService; use App\Services\SeatingService; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Cache; class ResultsPage extends Controller { protected $auditionCacheService; + protected $seatingService; + public function __construct(AuditionCacheService $auditionCacheService, SeatingService $seatingService) { $this->auditionCacheService = $auditionCacheService; @@ -22,11 +26,28 @@ class ResultsPage extends Controller public function __invoke(Request $request) { $publishedAuditions = $this->auditionCacheService->getPublishedAuditions(); + $resultsSeatList = Cache::rememberForever('resultsSeatList', function () use ($publishedAuditions) { + $seatList = []; + foreach ($publishedAuditions as $audition) { + $seats = $this->seatingService->getSeatsForAudition($audition->id); + $ensembles = $this->seatingService->getEnsemblesForEvent($audition->event_id); + foreach ($ensembles as $ensemble) { + if (! $seats->has($ensemble->id)) { // If there are no students seated in this ensemble, skip it + continue; + } + foreach ($seats[$ensemble->id] as $seat) { + /** @var Seat $seat */ + $seatList[$audition->id][] = [ + 'seat' => $ensemble->name.' '.$seat->seat, + 'student' => $seat->entry->student, + ]; + } + } + } - foreach ($publishedAuditions as $audition) { - $audition->seats = $this->seatingService->getSeatsForAudition($audition->id); - } + return $seatList; + }); - return view('results.index', compact('publishedAuditions')); + return view('results.index', compact('publishedAuditions', 'resultsSeatList')); } } diff --git a/app/Http/Controllers/Tabulation/TabulationController.php b/app/Http/Controllers/Tabulation/TabulationController.php index d5afb88..6a2f857 100644 --- a/app/Http/Controllers/Tabulation/TabulationController.php +++ b/app/Http/Controllers/Tabulation/TabulationController.php @@ -10,6 +10,7 @@ use App\Services\DoublerService; use App\Services\SeatingService; use App\Services\TabulationService; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Cache; use function compact; @@ -93,16 +94,22 @@ class TabulationController extends Controller } $audition->addFlag('seats_published'); $request->session()->forget($sessionKey); + Cache::forget('resultsSeatList'); + Cache::forget('publishedAuditions'); + Cache::forget('audition'.$audition->id.'seats'); + // TODO move the previous Cache functions here and in unplublish to the services, need to add an event for publishing an audition as well return redirect()->route('tabulation.audition.seat', ['audition' => $audition->id]); } public function unpublishSeats(Request $request, Audition $audition) { // TODO move this to SeatingService + $audition->removeFlag('seats_published'); + Cache::forget('resultsSeatList'); + Cache::forget('publishedAuditions'); + Cache::forget('audition'.$audition->id.'seats'); $this->seatingService->forgetSeatsForAudition($audition->id); Seat::where('audition_id', $audition->id)->delete(); - $audition->removeFlag('seats_published'); - return redirect()->route('tabulation.audition.seat', ['audition' => $audition->id]); } } diff --git a/app/Models/Audition.php b/app/Models/Audition.php index d4ba075..dfa3057 100644 --- a/app/Models/Audition.php +++ b/app/Models/Audition.php @@ -171,7 +171,7 @@ class Audition extends Model return $this->flags->contains('flag_name', $flag); } - public function addFlag($flag) + public function addFlag($flag): void { if ($this->hasFlag($flag)) { return; @@ -180,7 +180,7 @@ class Audition extends Model $this->flags()->create(['flag_name' => $flag]); } - public function removeFlag($flag) + public function removeFlag($flag): void { // remove related auditionFlag where flag_name = $flag $this->flags()->where('flag_name', $flag)->delete(); diff --git a/app/Services/AuditionCacheService.php b/app/Services/AuditionCacheService.php index 8c93ce7..3c7b261 100644 --- a/app/Services/AuditionCacheService.php +++ b/app/Services/AuditionCacheService.php @@ -70,4 +70,9 @@ class AuditionCacheService return Audition::orderBy('score_order')->get()->filter(fn ($audition) => $audition->hasFlag('seats_published')); }); } + + public function clearPublishedAuditionsCache() + { + Cache::forget('publishedAuditions'); + } } diff --git a/app/Services/SeatingService.php b/app/Services/SeatingService.php index edba7ad..26fdd38 100644 --- a/app/Services/SeatingService.php +++ b/app/Services/SeatingService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Models\Event; use App\Models\Seat; use App\Models\SeatingLimit; use Illuminate\Support\Facades\Cache; @@ -25,7 +26,7 @@ class SeatingService // TODO modifying audition limits should refresh this cache return Cache::remember($this->limitsCacheKey, now()->addDay(), function () { $limits = SeatingLimit::with('ensemble')->get(); - // Sort limits by ensemlbe->rank + // Sort limits by ensemble->rank $limits = $limits->sortBy(function ($limit) { return $limit->ensemble->rank; }); @@ -56,9 +57,9 @@ class SeatingService public function getSeatsForAudition($auditionId) { $cacheKey = 'audition'.$auditionId.'seats'; - + // TODO rework to pull entry info from cache return Cache::remember($cacheKey, now()->addHour(), function () use ($auditionId) { - return Seat::with('entry.student') + return Seat::with('entry.student.school') ->where('audition_id', $auditionId) ->orderBy('seat') ->get() @@ -71,4 +72,17 @@ class SeatingService $cacheKey = 'audition'.$auditionId.'seats'; Cache::forget($cacheKey); } + + public function getEnsemblesForEvent($eventId) + { + static $eventEnsembles = []; + + if (array_key_exists($eventId, $eventEnsembles)) { + return $eventEnsembles[$eventId]; + } + $event = Event::find($eventId); + $eventEnsembles[$eventId] = $event->ensembles; + + return $eventEnsembles[$eventId]; + } } diff --git a/resources/views/components/results/layout.blade.php b/resources/views/components/results/layout.blade.php index e6cd72e..0710a20 100644 --- a/resources/views/components/results/layout.blade.php +++ b/resources/views/components/results/layout.blade.php @@ -35,7 +35,7 @@ -