73 lines
2.4 KiB
PHP
73 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Actions\Draw\ClearDraw;
|
|
use App\Actions\Draw\RunDraw;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\ClearDrawRequest;
|
|
use App\Http\Requests\RunDrawRequest;
|
|
use App\Models\Audition;
|
|
use App\Models\Event;
|
|
use Illuminate\Http\Request;
|
|
|
|
use function array_keys;
|
|
use function to_route;
|
|
|
|
class DrawController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$events = Event::with('auditions.flags')->get();
|
|
|
|
// $drawnAuditionsExist is true if any audition->hasFlag('drawn') is true
|
|
$drawnAuditionsExist = Audition::whereHas('flags', function ($query) {
|
|
$query->where('flag_name', 'drawn');
|
|
})->exists();
|
|
|
|
return view('admin.draw.index', compact('events', 'drawnAuditionsExist'));
|
|
}
|
|
|
|
public function store(RunDrawRequest $request)
|
|
{
|
|
// Request will contain audition which is an array of audition IDs all with a value of 1
|
|
// Code below results in a collection of auditions that were checked on the form
|
|
$auditions = Audition::with('flags')->findMany(array_keys($request->input('audition', [])));
|
|
|
|
if ($auditions->contains(fn ($audition) => $audition->hasFlag('drawn'))) {
|
|
return to_route('admin.draw.index')->with('error',
|
|
'Cannot run draw. Some auditions have already been drawn.');
|
|
}
|
|
|
|
app(RunDraw::class)($auditions);
|
|
|
|
return to_route('admin.draw.index')->with('success', 'Draw completed successfully');
|
|
}
|
|
|
|
/**
|
|
* generates the page with checkboxes for each drawn audition with an intent to clear them
|
|
*/
|
|
public function edit(Request $request)
|
|
{
|
|
$drawnAuditions = Audition::whereHas('flags', function ($query) {
|
|
$query->where('flag_name', 'drawn');
|
|
})->get();
|
|
|
|
return view('admin.draw.edit', compact('drawnAuditions'));
|
|
}
|
|
|
|
/**
|
|
* Clears the draw for auditions
|
|
*/
|
|
public function destroy(ClearDrawRequest $request)
|
|
{
|
|
// Request will contain audition which is an array of audition IDs all with a value of 1
|
|
// Code below results in a collection of auditions that were checked on the form
|
|
$auditions = Audition::with('flags')->findMany(array_keys($request->input('audition', [])));
|
|
app(ClearDraw::class)($auditions);
|
|
|
|
return to_route('admin.draw.index')->with('success', 'Draws cleared successfully');
|
|
|
|
}
|
|
}
|