Cache and event updates.

This commit is contained in:
Matt Young 2024-06-12 23:29:02 -05:00
parent 008cf0e7b0
commit ff85fd1c86
14 changed files with 142 additions and 32 deletions

View File

@ -10,7 +10,7 @@ use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
class ScoringGuideChange class AuditionChange
{ {
use Dispatchable, InteractsWithSockets, SerializesModels; use Dispatchable, InteractsWithSockets, SerializesModels;

View File

@ -0,0 +1,36 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class RoomJudgeChange
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*/
public function __construct()
{
//
}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('channel-name'),
];
}
}

View File

@ -2,6 +2,8 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Events\AuditionChange;
use App\Events\RoomJudgeChange;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Audition; use App\Models\Audition;
use App\Models\Event; use App\Models\Event;
@ -31,6 +33,7 @@ class AuditionController extends Controller
{ {
if(! Auth::user()->is_admin) abort(403); if(! Auth::user()->is_admin) abort(403);
$events = Event::orderBy('name')->get(); $events = Event::orderBy('name')->get();
return view('admin.auditions.create',['events'=> $events]); return view('admin.auditions.create',['events'=> $events]);
} }
@ -57,7 +60,7 @@ class AuditionController extends Controller
'minimum_grade' => $validData['minimum_grade'], 'minimum_grade' => $validData['minimum_grade'],
'maximum_grade' => $validData['maximum_grade'], 'maximum_grade' => $validData['maximum_grade'],
]); ]);
AuditionChange::dispatch();
return redirect('/admin/auditions'); return redirect('/admin/auditions');
} }
@ -91,7 +94,7 @@ class AuditionController extends Controller
'minimum_grade' => $validData['minimum_grade'], 'minimum_grade' => $validData['minimum_grade'],
'maximum_grade' => $validData['maximum_grade'], 'maximum_grade' => $validData['maximum_grade'],
]); ]);
AuditionChange::dispatch();
return redirect('/admin/auditions'); return redirect('/admin/auditions');
} }
@ -103,6 +106,7 @@ class AuditionController extends Controller
$audition = Audition::find($id); $audition = Audition::find($id);
$audition->update(['score_order' => $index]); $audition->update(['score_order' => $index]);
} }
AuditionChange::dispatch();
return response()->json(['status' => 'success']); return response()->json(['status' => 'success']);
} }
@ -117,7 +121,7 @@ class AuditionController extends Controller
'order_in_room' => $audition['room_order'] 'order_in_room' => $audition['room_order']
]); ]);
} }
AuditionChange::dispatch();
return response()->json(['status' => 'success']); return response()->json(['status' => 'success']);
} }
@ -133,7 +137,7 @@ class AuditionController extends Controller
return response()->json(['success' => true]); return response()->json(['success' => true]);
} }
AuditionChange::dispatch();
return response()->json(['success' => false], 404); 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.'); return redirect()->route('adminAuditionIndex')->with('error', 'Cannot delete an audition with entries.');
} }
$audition->delete(); $audition->delete();
AuditionChange::dispatch();
return redirect('/admin/auditions'); return redirect('/admin/auditions');
} }

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Events\RoomJudgeChange;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Audition; use App\Models\Audition;
use App\Models\Room; use App\Models\Room;
@ -47,11 +48,11 @@ class RoomController extends Controller
} else { } else {
return redirect('/admin/rooms/judging_assignments')->with('error', 'Invalid request method.'); return redirect('/admin/rooms/judging_assignments')->with('error', 'Invalid request method.');
} }
RoomJudgeChange::dispatch();
return redirect('/admin/rooms/judging_assignments')->with('success',$message); return redirect('/admin/rooms/judging_assignments')->with('success',$message);
} }
// TODO need to be able to add new rooms. Dispatch RoomJudgeChange when we do.
} }

View File

@ -2,7 +2,7 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Events\ScoringGuideChange; use App\Events\AuditionChange;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\ScoringGuide; use App\Models\ScoringGuide;
use App\Models\SubscoreDefinition; use App\Models\SubscoreDefinition;
@ -38,7 +38,7 @@ class ScoringGuideController extends Controller
'name' => request('name') 'name' => request('name')
]); ]);
ScoringGuideChange::dispatch(); AuditionChange::dispatch();
return redirect('/admin/scoring'); return redirect('/admin/scoring');
} }
@ -65,7 +65,7 @@ class ScoringGuideController extends Controller
$guide->update([ $guide->update([
'name' => request('name') 'name' => request('name')
]); ]);
ScoringGuideChange::dispatch(); AuditionChange::dispatch();
return redirect('/admin/scoring/guides/' . $guide->id . '/edit' )->with('success','Scoring guide updated'); 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_seating' => $for_seating,
'for_advance' => $for_advance, 'for_advance' => $for_advance,
]); ]);
ScoringGuideChange::dispatch(); AuditionChange::dispatch();
return redirect('/admin/scoring/guides/' . $guide->id . '/edit' )->with('success','Subscore added'); 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 = SubscoreDefinition::find($id);
$subscore->update(['display_order' => $index]); $subscore->update(['display_order' => $index]);
} }
ScoringGuideChange::dispatch(); AuditionChange::dispatch();
return response()->json(['status'=>'success']); return response()->json(['status'=>'success']);
} }
@ -122,7 +122,7 @@ class ScoringGuideController extends Controller
$subscore = SubscoreDefinition::find($id); $subscore = SubscoreDefinition::find($id);
$subscore->update(['tiebreak_order' => $index]); $subscore->update(['tiebreak_order' => $index]);
} }
ScoringGuideChange::dispatch(); AuditionChange::dispatch();
return response()->json(['status'=>'success']); return response()->json(['status'=>'success']);
} }

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Events\RoomJudgeChange;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Mail\NewUserPassword; use App\Mail\NewUserPassword;
use App\Models\School; use App\Models\School;
@ -56,7 +57,7 @@ class UserController extends Controller
'judging_preference' => request('judging_preference'), 'judging_preference' => request('judging_preference'),
'school_id' => request('school_id') 'school_id' => request('school_id')
]); ]);
RoomJudgeChange::dispatch();
return redirect('/admin/users'); return redirect('/admin/users');
} }

View File

@ -2,7 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Services\ScoringGuideCacheService; use App\Services\AuditionCacheService;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Session;
@ -10,14 +10,14 @@ class TestController extends Controller
{ {
protected $scoringGuideCacheService; protected $scoringGuideCacheService;
public function __construct(ScoringGuideCacheService $scoringGuideCacheService) public function __construct(AuditionCacheService $scoringGuideCacheService)
{ {
$this->scoringGuideCacheService = $scoringGuideCacheService; $this->scoringGuideCacheService = $scoringGuideCacheService;
} }
public function flashTest(Request $request) public function flashTest(Request $request)
{ {
$sg = $this->scoringGuideCacheService->getScoringGuides(); $sg = $this->scoringGuideCacheService->getAuditions();
return view('test', compact('sg')); return view('test', compact('sg'));
} }

View File

@ -81,3 +81,6 @@ class UserController extends Controller
return redirect('/my_school'); return redirect('/my_school');
} }
} }
//TODO allow users to modify their profile information. RoomJudgeChange::dispatch(); when they do

View File

@ -2,18 +2,18 @@
namespace App\Listeners; namespace App\Listeners;
use App\Events\ScoringGuideChange; use App\Events\AuditionChange;
use App\Services\ScoringGuideCacheService; use App\Services\AuditionCacheService;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
class RefreshScoringGuideCache class RefreshAuditionCache
{ {
protected $cacheService; protected $cacheService;
/** /**
* Create the event listener. * Create the event listener.
*/ */
public function __construct(ScoringGuideCacheService $cacheService) public function __construct(AuditionCacheService $cacheService)
{ {
$this->cacheService = $cacheService; $this->cacheService = $cacheService;
} }
@ -21,7 +21,7 @@ class RefreshScoringGuideCache
/** /**
* Handle the event. * Handle the event.
*/ */
public function handle(ScoringGuideChange $event): void public function handle(AuditionChange $event): void
{ {
$this->cacheService->refreshCache(); $this->cacheService->refreshCache();
} }

View File

@ -0,0 +1,30 @@
<?php
namespace App\Listeners;
use App\Events\RoomJudgeChange;
use App\Services\AuditionCacheService;
use App\Services\RoomAndJudgeCacheService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class RefreshRoomJudgeCache
{
protected $cacheService;
/**
* Create the event listener.
*/
public function __construct(RoomAndJudgeCacheService $cacheService)
{
$this->cacheService = $cacheService;
}
/**
* Handle the event.
*/
public function handle(RoomJudgeChange $event): void
{
$this->cacheService->refreshCache();
}
}

View File

@ -2,8 +2,8 @@
namespace App\Providers; namespace App\Providers;
use App\Events\ScoringGuideChange; use App\Events\AuditionChange;
use App\Listeners\RefreshScoringGuideCache; use App\Listeners\RefreshAuditionCache;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
@ -23,8 +23,8 @@ class AppServiceProvider extends ServiceProvider
public function boot(): void public function boot(): void
{ {
Event::listen( Event::listen(
ScoringGuideChange::class, AuditionChange::class,
RefreshScoringGuideCache::class RefreshAuditionCache::class
); );
} }
} }

View File

@ -2,12 +2,13 @@
namespace App\Services; namespace App\Services;
use App\Models\Audition;
use App\Models\ScoringGuide; use App\Models\ScoringGuide;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
class ScoringGuideCacheService class AuditionCacheService
{ {
protected $cacheKey = 'scoring_guides'; protected $cacheKey = 'auditions';
/** /**
* Create a new class instance. * Create a new class instance.
*/ */
@ -16,16 +17,16 @@ class ScoringGuideCacheService
// //
} }
public function getScoringGuides() public function getAuditions()
{ {
return Cache::rememberForever($this->cacheKey, function () { return Cache::rememberForever($this->cacheKey, function () {
return ScoringGuide::with('subscores')->get(); return Audition::with(['scoringGuide.subscores'])->get();
}); });
} }
public function refreshCache() public function refreshCache()
{ {
Cache::forget($this->cacheKey); Cache::forget($this->cacheKey);
$this->getScoringGuides(); $this->getAuditions();
} }
} }

View File

@ -0,0 +1,33 @@
<?php
namespace App\Services;
use App\Models\Audition;
use App\Models\Room;
use App\Models\ScoringGuide;
use Illuminate\Support\Facades\Cache;
class RoomAndJudgeCacheService
{
protected $cacheKey = 'roomJudge';
/**
* Create a new class instance.
*/
public function __construct()
{
//
}
public function getAuditions()
{
return Cache::rememberForever($this->cacheKey, function () {
return Room::with(['judges'])->get();
});
}
public function refreshCache()
{
Cache::forget($this->cacheKey);
$this->getAuditions();
}
}

View File

@ -4,7 +4,7 @@
use App\Models\SchoolEmailDomain; use App\Models\SchoolEmailDomain;
use App\Models\ScoreSheet; use App\Models\ScoreSheet;
use App\Models\ScoringGuide; 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\Auth;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Session;