From ff85fd1c863c63303c45aad327af62b0ae0daed4 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Wed, 12 Jun 2024 23:29:02 -0500 Subject: [PATCH] Cache and event updates. --- ...ringGuideChange.php => AuditionChange.php} | 2 +- app/Events/RoomJudgeChange.php | 36 +++++++++++++++++++ .../Controllers/Admin/AuditionController.php | 13 ++++--- app/Http/Controllers/Admin/RoomController.php | 5 +-- .../Admin/ScoringGuideController.php | 12 +++---- app/Http/Controllers/Admin/UserController.php | 3 +- app/Http/Controllers/TestController.php | 6 ++-- app/Http/Controllers/UserController.php | 3 ++ ...uideCache.php => RefreshAuditionCache.php} | 10 +++--- app/Listeners/RefreshRoomJudgeCache.php | 30 ++++++++++++++++ app/Providers/AppServiceProvider.php | 8 ++--- ...heService.php => AuditionCacheService.php} | 11 +++--- app/Services/RoomAndJudgeCacheService.php | 33 +++++++++++++++++ resources/views/test.blade.php | 2 +- 14 files changed, 142 insertions(+), 32 deletions(-) rename app/Events/{ScoringGuideChange.php => AuditionChange.php} (96%) create mode 100644 app/Events/RoomJudgeChange.php rename app/Listeners/{RefreshScoringGuideCache.php => RefreshAuditionCache.php} (76%) create mode 100644 app/Listeners/RefreshRoomJudgeCache.php rename app/Services/{ScoringGuideCacheService.php => AuditionCacheService.php} (63%) create mode 100644 app/Services/RoomAndJudgeCacheService.php diff --git a/app/Events/ScoringGuideChange.php b/app/Events/AuditionChange.php similarity index 96% rename from app/Events/ScoringGuideChange.php rename to app/Events/AuditionChange.php index b265ad9..0f24699 100644 --- a/app/Events/ScoringGuideChange.php +++ b/app/Events/AuditionChange.php @@ -10,7 +10,7 @@ use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; -class ScoringGuideChange +class AuditionChange { use Dispatchable, InteractsWithSockets, SerializesModels; diff --git a/app/Events/RoomJudgeChange.php b/app/Events/RoomJudgeChange.php new file mode 100644 index 0000000..5d9e8c6 --- /dev/null +++ b/app/Events/RoomJudgeChange.php @@ -0,0 +1,36 @@ + + */ + public function broadcastOn(): array + { + return [ + new PrivateChannel('channel-name'), + ]; + } +} diff --git a/app/Http/Controllers/Admin/AuditionController.php b/app/Http/Controllers/Admin/AuditionController.php index 448edd2..fcf00ab 100644 --- a/app/Http/Controllers/Admin/AuditionController.php +++ b/app/Http/Controllers/Admin/AuditionController.php @@ -2,6 +2,8 @@ namespace App\Http\Controllers\Admin; +use App\Events\AuditionChange; +use App\Events\RoomJudgeChange; use App\Http\Controllers\Controller; use App\Models\Audition; use App\Models\Event; @@ -31,6 +33,7 @@ class AuditionController extends Controller { if(! Auth::user()->is_admin) abort(403); $events = Event::orderBy('name')->get(); + return view('admin.auditions.create',['events'=> $events]); } @@ -57,7 +60,7 @@ class AuditionController extends Controller 'minimum_grade' => $validData['minimum_grade'], 'maximum_grade' => $validData['maximum_grade'], ]); - + AuditionChange::dispatch(); return redirect('/admin/auditions'); } @@ -91,7 +94,7 @@ class AuditionController extends Controller 'minimum_grade' => $validData['minimum_grade'], 'maximum_grade' => $validData['maximum_grade'], ]); - + AuditionChange::dispatch(); return redirect('/admin/auditions'); } @@ -103,6 +106,7 @@ class AuditionController extends Controller $audition = Audition::find($id); $audition->update(['score_order' => $index]); } + AuditionChange::dispatch(); return response()->json(['status' => 'success']); } @@ -117,7 +121,7 @@ class AuditionController extends Controller 'order_in_room' => $audition['room_order'] ]); } - + AuditionChange::dispatch(); return response()->json(['status' => 'success']); } @@ -133,7 +137,7 @@ class AuditionController extends Controller return response()->json(['success' => true]); } - + AuditionChange::dispatch(); return response()->json(['success' => false], 404); } @@ -145,6 +149,7 @@ class AuditionController extends Controller return redirect()->route('adminAuditionIndex')->with('error', 'Cannot delete an audition with entries.'); } $audition->delete(); + AuditionChange::dispatch(); return redirect('/admin/auditions'); } diff --git a/app/Http/Controllers/Admin/RoomController.php b/app/Http/Controllers/Admin/RoomController.php index 4791ce6..117892a 100644 --- a/app/Http/Controllers/Admin/RoomController.php +++ b/app/Http/Controllers/Admin/RoomController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Admin; +use App\Events\RoomJudgeChange; use App\Http\Controllers\Controller; use App\Models\Audition; use App\Models\Room; @@ -47,11 +48,11 @@ class RoomController extends Controller } else { return redirect('/admin/rooms/judging_assignments')->with('error', 'Invalid request method.'); } - + RoomJudgeChange::dispatch(); return redirect('/admin/rooms/judging_assignments')->with('success',$message); } - +// TODO need to be able to add new rooms. Dispatch RoomJudgeChange when we do. } diff --git a/app/Http/Controllers/Admin/ScoringGuideController.php b/app/Http/Controllers/Admin/ScoringGuideController.php index a211648..4838028 100644 --- a/app/Http/Controllers/Admin/ScoringGuideController.php +++ b/app/Http/Controllers/Admin/ScoringGuideController.php @@ -2,7 +2,7 @@ namespace App\Http\Controllers\Admin; -use App\Events\ScoringGuideChange; +use App\Events\AuditionChange; use App\Http\Controllers\Controller; use App\Models\ScoringGuide; use App\Models\SubscoreDefinition; @@ -38,7 +38,7 @@ class ScoringGuideController extends Controller 'name' => request('name') ]); - ScoringGuideChange::dispatch(); + AuditionChange::dispatch(); return redirect('/admin/scoring'); } @@ -65,7 +65,7 @@ class ScoringGuideController extends Controller $guide->update([ 'name' => request('name') ]); - ScoringGuideChange::dispatch(); + AuditionChange::dispatch(); return redirect('/admin/scoring/guides/' . $guide->id . '/edit' )->with('success','Scoring guide updated'); } @@ -97,7 +97,7 @@ class ScoringGuideController extends Controller 'for_seating' => $for_seating, 'for_advance' => $for_advance, ]); - ScoringGuideChange::dispatch(); + AuditionChange::dispatch(); return redirect('/admin/scoring/guides/' . $guide->id . '/edit' )->with('success','Subscore added'); } @@ -109,7 +109,7 @@ class ScoringGuideController extends Controller $subscore = SubscoreDefinition::find($id); $subscore->update(['display_order' => $index]); } - ScoringGuideChange::dispatch(); + AuditionChange::dispatch(); return response()->json(['status'=>'success']); } @@ -122,7 +122,7 @@ class ScoringGuideController extends Controller $subscore = SubscoreDefinition::find($id); $subscore->update(['tiebreak_order' => $index]); } - ScoringGuideChange::dispatch(); + AuditionChange::dispatch(); return response()->json(['status'=>'success']); } diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index b43e7a5..ad1beb9 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Admin; +use App\Events\RoomJudgeChange; use App\Http\Controllers\Controller; use App\Mail\NewUserPassword; use App\Models\School; @@ -56,7 +57,7 @@ class UserController extends Controller 'judging_preference' => request('judging_preference'), 'school_id' => request('school_id') ]); - + RoomJudgeChange::dispatch(); return redirect('/admin/users'); } diff --git a/app/Http/Controllers/TestController.php b/app/Http/Controllers/TestController.php index 2bf33cc..7fdc58d 100644 --- a/app/Http/Controllers/TestController.php +++ b/app/Http/Controllers/TestController.php @@ -2,7 +2,7 @@ namespace App\Http\Controllers; -use App\Services\ScoringGuideCacheService; +use App\Services\AuditionCacheService; use Illuminate\Http\Request; use Illuminate\Support\Facades\Session; @@ -10,14 +10,14 @@ class TestController extends Controller { protected $scoringGuideCacheService; - public function __construct(ScoringGuideCacheService $scoringGuideCacheService) + public function __construct(AuditionCacheService $scoringGuideCacheService) { $this->scoringGuideCacheService = $scoringGuideCacheService; } public function flashTest(Request $request) { - $sg = $this->scoringGuideCacheService->getScoringGuides(); + $sg = $this->scoringGuideCacheService->getAuditions(); return view('test', compact('sg')); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 1f5671b..9d60447 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -81,3 +81,6 @@ class UserController extends Controller return redirect('/my_school'); } } + + +//TODO allow users to modify their profile information. RoomJudgeChange::dispatch(); when they do diff --git a/app/Listeners/RefreshScoringGuideCache.php b/app/Listeners/RefreshAuditionCache.php similarity index 76% rename from app/Listeners/RefreshScoringGuideCache.php rename to app/Listeners/RefreshAuditionCache.php index 7f88d01..6e45277 100644 --- a/app/Listeners/RefreshScoringGuideCache.php +++ b/app/Listeners/RefreshAuditionCache.php @@ -2,18 +2,18 @@ namespace App\Listeners; -use App\Events\ScoringGuideChange; -use App\Services\ScoringGuideCacheService; +use App\Events\AuditionChange; +use App\Services\AuditionCacheService; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; -class RefreshScoringGuideCache +class RefreshAuditionCache { protected $cacheService; /** * Create the event listener. */ - public function __construct(ScoringGuideCacheService $cacheService) + public function __construct(AuditionCacheService $cacheService) { $this->cacheService = $cacheService; } @@ -21,7 +21,7 @@ class RefreshScoringGuideCache /** * Handle the event. */ - public function handle(ScoringGuideChange $event): void + public function handle(AuditionChange $event): void { $this->cacheService->refreshCache(); } diff --git a/app/Listeners/RefreshRoomJudgeCache.php b/app/Listeners/RefreshRoomJudgeCache.php new file mode 100644 index 0000000..08294c1 --- /dev/null +++ b/app/Listeners/RefreshRoomJudgeCache.php @@ -0,0 +1,30 @@ +cacheService = $cacheService; + } + + /** + * Handle the event. + */ + public function handle(RoomJudgeChange $event): void + { + $this->cacheService->refreshCache(); + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 1be54dc..ff19d4b 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,8 +2,8 @@ namespace App\Providers; -use App\Events\ScoringGuideChange; -use App\Listeners\RefreshScoringGuideCache; +use App\Events\AuditionChange; +use App\Listeners\RefreshAuditionCache; use Illuminate\Support\Facades\Event; use Illuminate\Support\ServiceProvider; @@ -23,8 +23,8 @@ class AppServiceProvider extends ServiceProvider public function boot(): void { Event::listen( - ScoringGuideChange::class, - RefreshScoringGuideCache::class + AuditionChange::class, + RefreshAuditionCache::class ); } } diff --git a/app/Services/ScoringGuideCacheService.php b/app/Services/AuditionCacheService.php similarity index 63% rename from app/Services/ScoringGuideCacheService.php rename to app/Services/AuditionCacheService.php index b37cdde..b32c4aa 100644 --- a/app/Services/ScoringGuideCacheService.php +++ b/app/Services/AuditionCacheService.php @@ -2,12 +2,13 @@ namespace App\Services; +use App\Models\Audition; use App\Models\ScoringGuide; use Illuminate\Support\Facades\Cache; -class ScoringGuideCacheService +class AuditionCacheService { - protected $cacheKey = 'scoring_guides'; + protected $cacheKey = 'auditions'; /** * Create a new class instance. */ @@ -16,16 +17,16 @@ class ScoringGuideCacheService // } - public function getScoringGuides() + public function getAuditions() { return Cache::rememberForever($this->cacheKey, function () { - return ScoringGuide::with('subscores')->get(); + return Audition::with(['scoringGuide.subscores'])->get(); }); } public function refreshCache() { Cache::forget($this->cacheKey); - $this->getScoringGuides(); + $this->getAuditions(); } } diff --git a/app/Services/RoomAndJudgeCacheService.php b/app/Services/RoomAndJudgeCacheService.php new file mode 100644 index 0000000..942cd5b --- /dev/null +++ b/app/Services/RoomAndJudgeCacheService.php @@ -0,0 +1,33 @@ +cacheKey, function () { + return Room::with(['judges'])->get(); + }); + } + + public function refreshCache() + { + Cache::forget($this->cacheKey); + $this->getAuditions(); + } +} diff --git a/resources/views/test.blade.php b/resources/views/test.blade.php index e6faabe..dcbdd68 100644 --- a/resources/views/test.blade.php +++ b/resources/views/test.blade.php @@ -4,7 +4,7 @@ use App\Models\SchoolEmailDomain; use App\Models\ScoreSheet; use App\Models\ScoringGuide; - use App\Models\User;use App\Services\ScoringGuideCacheService;use App\Settings; + use App\Models\User;use App\Services\AuditionCacheService;use App\Settings; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Session;