diff --git a/app/Http/Controllers/TestController.php b/app/Http/Controllers/TestController.php index ed9878f..64095f4 100644 --- a/app/Http/Controllers/TestController.php +++ b/app/Http/Controllers/TestController.php @@ -3,22 +3,24 @@ namespace App\Http\Controllers; use App\Services\AuditionCacheService; +use App\Services\TabulationService; use Illuminate\Http\Request; use Illuminate\Support\Facades\Session; class TestController extends Controller { protected $scoringGuideCacheService; + protected $tabulationService; - public function __construct(AuditionCacheService $scoringGuideCacheService) + public function __construct(AuditionCacheService $scoringGuideCacheService, TabulationService $tabulationService) { $this->scoringGuideCacheService = $scoringGuideCacheService; + $this->tabulationService = $tabulationService; } public function flashTest(Request $request) { - $auditions = $this->scoringGuideCacheService->getAuditions(); - $auditionEleven = $this->scoringGuideCacheService->getAudition(11); - return view('test', compact('auditions','auditionEleven')); + $auditions = $this->tabulationService->getAuditionsWithStatus(); + return view('test', compact('auditions')); } } diff --git a/app/Models/Audition.php b/app/Models/Audition.php index e00bfe1..bf330be 100644 --- a/app/Models/Audition.php +++ b/app/Models/Audition.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; use phpDocumentor\Reflection\Types\Boolean; @@ -139,11 +140,18 @@ class Audition extends Model } /** - * @return Collection + * @return BelongsToMany|\App\Models\User[] */ public function judges() { - return $this->room->judges; + return $this->belongsToMany( + User::class, // The related model + 'room_user', // The intermediate table + 'room_id', // The foreign key on the intermediate table + 'user_id', // The related key on the intermediate table + 'room_id', // The local key + 'id' // The local ke + ); } public function scoredEntries() diff --git a/app/Models/RoomUser.php b/app/Models/RoomUser.php index 061368b..d493039 100644 --- a/app/Models/RoomUser.php +++ b/app/Models/RoomUser.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; class RoomUser extends Model { + protected $table = 'room_user'; public function user(): BelongsTo { return $this->belongsTo(User::class); diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ff19d4b..dfd4d07 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -4,6 +4,8 @@ namespace App\Providers; use App\Events\AuditionChange; use App\Listeners\RefreshAuditionCache; +use App\Services\AuditionCacheService; +use App\Services\TabulationService; use Illuminate\Support\Facades\Event; use Illuminate\Support\ServiceProvider; @@ -14,7 +16,13 @@ class AppServiceProvider extends ServiceProvider */ public function register(): void { - // + $this->app->singleton(AuditionCacheService::class, function ($app) { + return new AuditionCacheService(); + }); + + $this->app->singleton(TabulationService::class, function($app) { + return new TabulationService($app->make(AuditionCacheService::class)); + }); } /** diff --git a/app/Services/AuditionCacheService.php b/app/Services/AuditionCacheService.php index f90e9d2..f9a5cfd 100644 --- a/app/Services/AuditionCacheService.php +++ b/app/Services/AuditionCacheService.php @@ -17,10 +17,10 @@ class AuditionCacheService // } - public function getAuditions() + public function getAuditions(): \Illuminate\Database\Eloquent\Collection { return Cache::rememberForever($this->cacheKey, function () { - return Audition::with(['scoringGuide.subscores'])->get(); + return Audition::with(['scoringGuide.subscores'])->get()->keyBy('id'); }); } diff --git a/app/Services/TabulationService.php b/app/Services/TabulationService.php index b25c6d4..ed674ec 100644 --- a/app/Services/TabulationService.php +++ b/app/Services/TabulationService.php @@ -2,13 +2,57 @@ namespace App\Services; +use App\Models\Audition; +use App\Models\Entry; +use Illuminate\Support\Facades\Cache; +use App\Services\AuditionCacheService; +use Illuminate\Support\Facades\DB; + + class TabulationService { + protected $cacheKey = 'entries'; + protected $auditionCacheService; /** * Create a new class instance. */ - public function __construct() + public function __construct(AuditionCacheService $scoringGuideCacheService) { - // + $this->auditionCacheService = $scoringGuideCacheService; + } + + public function getScoredEntries() + { + return Cache::remember($this->cacheKey, 10, function () { + $entries = Entry::with(['scoreSheets'])->get(); + return $entries->keyBy('id'); + }); + } + + public function getAuditionsWithStatus() + { + $scoreCountByEntry = DB::table('score_sheets') + ->select('entry_id', DB::raw('count(*) as count')) + ->groupBy('entry_id') + ->get() + ->pluck('count','entry_id'); + $auditions = $this->auditionCacheService->getAuditions(); + $auditions->load(['entries' => function ($query) { + $query->select('id'); + }]); + $auditions->loadCount('judges'); + $auditions->loadCount('entries'); + + foreach ($auditions as $audition) + { + // Get the count of entries that have been scored + $audition->scored_entries = $audition->entries->filter(function ($entry) use ($scoreCountByEntry) { + return $entry->score_sheets_count == $scoreCountByEntry[$entry->id]; + }); + // WHY WILL THIS NOT WORK????? + + } + + return $auditions; } } diff --git a/resources/views/test.blade.php b/resources/views/test.blade.php index 052c43b..004afd5 100644 --- a/resources/views/test.blade.php +++ b/resources/views/test.blade.php @@ -13,12 +13,17 @@ Test Page @php - dump($auditionEleven); - $auditions->load('entries.scoreSheets'); dump($auditions); + @endphp - @foreach($auditions as $a) - {{$a->entries->count()}}
+ + @foreach($auditions as $audition) + @php + dd($audition->scored_entries); + @endphp + {{ $audition->name }} has {{ $audition->entries_count }} entries. {{ $audition->scored_entry_count }} are fully scored.
@endforeach + +