95 lines
2.7 KiB
PHP
95 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Audition;
|
|
use App\Models\ScoringGuide;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
class AuditionCacheService
|
|
{
|
|
protected $cacheKey = 'auditions';
|
|
|
|
/**
|
|
* Create a new class instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Return or fill cache of auditions including the audition,
|
|
* scoringGuide.subscores, judges, judges_count, and entries_count
|
|
*/
|
|
public function getAuditions($mode = 'seating'): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
$auditions = Cache::remember($this->cacheKey, 3600, function () {
|
|
if (App::environment('local')) {
|
|
Session::flash('success', 'Audition Cache Updated');
|
|
}
|
|
|
|
return Audition::with(['scoringGuide.subscores', 'judges'])
|
|
->withCount('judges')
|
|
->withCount('entries')
|
|
->withCount(['entries as seating_entries_count' => function (Builder $query) {
|
|
$query->where('for_seating', true);
|
|
}])
|
|
->withCount(['entries as advancement_entries_count' => function (Builder $query) {
|
|
$query->where('for_advancement', true);
|
|
}])
|
|
->orderBy('score_order')
|
|
->get()
|
|
->keyBy('id');
|
|
});
|
|
|
|
switch ($mode) {
|
|
case 'seating':
|
|
return $auditions->filter(fn ($audition) => $audition->for_seating);
|
|
case 'advancement':
|
|
return $auditions->filter(fn ($audition) => $audition->for_advancement);
|
|
default:
|
|
return $auditions;
|
|
}
|
|
}
|
|
|
|
public function getAudition($id): Audition
|
|
{
|
|
return $this->getAuditions()->firstWhere('id', $id);
|
|
}
|
|
|
|
public function refreshCache(): void
|
|
{
|
|
Cache::forget($this->cacheKey);
|
|
$this->getAuditions();
|
|
}
|
|
|
|
public function clearCache(): void
|
|
{
|
|
if (App::environment('local')) {
|
|
Session::flash('success', 'Audition Cache Cleared');
|
|
}
|
|
Cache::forget($this->cacheKey);
|
|
}
|
|
|
|
public function getPublishedAuditions()
|
|
{
|
|
$cacheKey = 'publishedAuditions';
|
|
|
|
return Cache::remember(
|
|
$cacheKey,
|
|
now()->addHour(),
|
|
function () {
|
|
return Audition::with('flags')->orderBy('score_order')->get()->filter(fn ($audition) => $audition->hasFlag('seats_published'));
|
|
});
|
|
}
|
|
|
|
public function clearPublishedAuditionsCache(): void
|
|
{
|
|
Cache::forget('publishedAuditions');
|
|
}
|
|
}
|