diff --git a/app/Http/Controllers/Tabulation/DoublerDecisionController.php b/app/Http/Controllers/Tabulation/DoublerDecisionController.php index 42c8c24..4eb0b66 100644 --- a/app/Http/Controllers/Tabulation/DoublerDecisionController.php +++ b/app/Http/Controllers/Tabulation/DoublerDecisionController.php @@ -6,20 +6,24 @@ use App\Http\Controllers\Controller; use App\Models\Entry; use App\Models\EntryFlag; use App\Services\DoublerService; +use App\Services\EntryCacheService; class DoublerDecisionController extends Controller { protected $doublerService; + protected $entryService; - public function __construct(DoublerService $doublerService) + public function __construct(DoublerService $doublerService, EntryCacheService $entryService) { $this->doublerService = $doublerService; + $this->entryService = $entryService; } public function accept(Entry $entry) { $doublerInfo = $this->doublerService->getDoublerInfo($entry->student_id); foreach ($doublerInfo as $info) { + $this->entryService->clearEntryCacheForAudition($info['auditionID']); if ($info['entryID'] != $entry->id) { try { EntryFlag::create([ @@ -29,6 +33,7 @@ class DoublerDecisionController extends Controller } catch (\Exception $e) { session()->flash('error', 'Entry ID'.$info['entryID'].' has already been declined.'); } + } } $this->doublerService->refreshDoublerCache(); diff --git a/app/Models/ScoringGuide.php b/app/Models/ScoringGuide.php index 2508639..264a1a8 100644 --- a/app/Models/ScoringGuide.php +++ b/app/Models/ScoringGuide.php @@ -5,9 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; -use phpDocumentor\Reflection\Types\Boolean; -use PhpParser\Node\Expr\Cast\Bool_; -use PhpParser\Node\Scalar\String_; + use function array_diff_key; use function array_key_exists; use function is_null; @@ -15,7 +13,9 @@ use function is_null; class ScoringGuide extends Model { use HasFactory; - protected $guarded = []; + + protected $guarded = []; + protected $with = ['subscores']; public function auditions(): HasMany @@ -30,21 +30,29 @@ class ScoringGuide extends Model /** * Validate a set of subscores. Expects an array in the form of subscore_id => score - * @param array $prospective_score + * * @return string */ - public function validateScores(Array $prospective_score) + public function validateScores(array $prospective_score) { // TODO move to either TabulationService or a specific service for scoreValidation foreach ($this->subscores as $subscore) { - if (! array_key_exists($subscore->id,$prospective_score)) return "A score must be provided for " . $subscore->name; - if (is_null($prospective_score[$subscore->id])) return "A score must be provided for " . $subscore->name; - if ($prospective_score[$subscore->id] > $subscore->max_score) return "The " . $subscore->name . " score must be less than or equal to " . $subscore->max_score; + if (! array_key_exists($subscore->id, $prospective_score)) { + return 'A score must be provided for '.$subscore->name; + } + if (is_null($prospective_score[$subscore->id])) { + return 'A score must be provided for '.$subscore->name; + } + if ($prospective_score[$subscore->id] > $subscore->max_score) { + return 'The '.$subscore->name.' score must be less than or equal to '.$subscore->max_score; + } } $subscore_ids = $this->subscores->pluck('id')->flip()->all(); $diff = array_diff_key($prospective_score, $subscore_ids); - if (!empty($diff)) return "Invalid scores submitted"; + if (! empty($diff)) { + return 'Invalid scores submitted'; + } return 'success'; // TODO this probably needs to be rewritten as a validation rule diff --git a/app/Services/EntryCacheService.php b/app/Services/EntryCacheService.php index 5f2fe6c..5ee0e2c 100644 --- a/app/Services/EntryCacheService.php +++ b/app/Services/EntryCacheService.php @@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Cache; class EntryCacheService { protected $auditionCache; + /** * Create a new class instance. */ @@ -20,14 +21,16 @@ class EntryCacheService /** * Return a collection of all entries for the provided auditionId along with the * student.school for each entry. - * @param $auditionId + * * @return \Illuminate\Database\Eloquent\Collection */ - public function getEntriesForAudition($auditionId) { + public function getEntriesForAudition($auditionId) + { // TODO this invokes a lot of lazy loading. Perhaps cache the data for all entries then draw from that for each audition - $cacheKey = 'audition' . $auditionId . 'entries'; - return Cache::remember($cacheKey, 3600, function () use ($auditionId) { - return Entry::where('audition_id',$auditionId) + $cacheKey = 'audition'.$auditionId.'entries'; + + return Cache::remember($cacheKey, 3600, function () use ($auditionId) { + return Entry::where('audition_id', $auditionId) ->with('student.school') ->get() ->keyBy('id'); @@ -38,7 +41,6 @@ class EntryCacheService * Returns a collection of collections of entries, one collection for each audition. * The outer collection is keyed by the audition ID. The included entries are * with their student.school. - * @return Collection */ public function getAllEntriesByAudition(): Collection { @@ -47,20 +49,22 @@ class EntryCacheService foreach ($auditions as $audition) { $allEntries[$audition->id] = $this->getEntriesForAudition($audition->id); } + return collect($allEntries); } public function getAllEntries() { $cacheKey = 'allEntries'; - return Cache::remember($cacheKey, 5, function() { - return Entry::all(); + + return Cache::remember($cacheKey, 5, function () { + return Entry::all(); }); } public function clearEntryCacheForAudition($auditionId): void { - $cacheKey = 'audition' . $auditionId . 'entries'; + $cacheKey = 'audition'.$auditionId.'entries'; Cache::forget($cacheKey); Cache::forget('allEntries'); } diff --git a/resources/views/components/layout/navbar/menus/setup.blade.php b/resources/views/components/layout/navbar/menus/setup.blade.php index c191594..9b2f1d1 100644 --- a/resources/views/components/layout/navbar/menus/setup.blade.php +++ b/resources/views/components/layout/navbar/menus/setup.blade.php @@ -18,7 +18,7 @@ From: "opacity-100 translate-y-0" To: "opacity-0 translate-y-1" --> - {{-- TODO conver to named routes --}} + {{-- TODO convert to named routes --}}