auditionadmin/app/Services/SeatingService.php

75 lines
1.9 KiB
PHP

<?php
namespace App\Services;
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 ensemlbe->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';
return Cache::remember($cacheKey, now()->addHour(), function () use ($auditionId) {
return Seat::with('entry.student')
->where('audition_id', $auditionId)
->orderBy('seat')
->get()
->groupBy('ensemble_id');
});
}
public function forgetSeatsForAudition($auditionId)
{
$cacheKey = 'audition'.$auditionId.'seats';
Cache::forget($cacheKey);
}
}