89 lines
2.4 KiB
PHP
89 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Event;
|
|
use App\Models\Seat;
|
|
use App\Models\SeatingLimit;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class SeatingService
|
|
{
|
|
protected $limitsCacheKey = 'acceptanceLimits';
|
|
|
|
protected $tabulationService;
|
|
|
|
/**
|
|
* Create a new class instance.
|
|
*/
|
|
public function __construct(TabulationService $tabulationService)
|
|
{
|
|
$this->tabulationService = $tabulationService;
|
|
}
|
|
|
|
public function getAcceptanceLimits()
|
|
{
|
|
// TODO modifying audition limits should refresh this cache
|
|
return Cache::remember($this->limitsCacheKey, now()->addDay(), function () {
|
|
$limits = SeatingLimit::with('ensemble')->get();
|
|
// Sort limits by ensemble->rank
|
|
$limits = $limits->sortBy(function ($limit) {
|
|
return $limit->ensemble->rank;
|
|
});
|
|
|
|
return $limits->groupBy('audition_id');
|
|
});
|
|
}
|
|
|
|
public function getLimitForAudition($auditionId)
|
|
{
|
|
return $this->getAcceptanceLimits()[$auditionId];
|
|
}
|
|
|
|
public function refreshLimits(): void
|
|
{
|
|
Cache::forget($this->limitsCacheKey);
|
|
}
|
|
|
|
public function getSeatableEntries($auditionId)
|
|
{
|
|
$entries = $this->tabulationService->auditionEntries($auditionId);
|
|
|
|
return $entries->reject(function ($entry) {
|
|
return $entry->hasFlag('declined');
|
|
});
|
|
}
|
|
|
|
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.school')
|
|
->where('audition_id', $auditionId)
|
|
->orderBy('seat')
|
|
->get()
|
|
->groupBy('ensemble_id');
|
|
});
|
|
}
|
|
|
|
public function forgetSeatsForAudition($auditionId)
|
|
{
|
|
$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];
|
|
}
|
|
}
|