83 lines
2.8 KiB
PHP
83 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Audition;
|
|
use App\Models\BonusScoreDefinition;
|
|
use App\Rules\ValidateAuditionKey;
|
|
use Exception;
|
|
use Illuminate\Http\Request;
|
|
|
|
use function redirect;
|
|
use function to_route;
|
|
|
|
class BonusScoreDefinitionController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$bonusScores = BonusScoreDefinition::with('auditions')->get();
|
|
// Set auditions equal to the collection of auditions that do not have a related bonus score
|
|
$unassignedAuditions = Audition::orderBy('score_order')->doesntHave('bonusScore')->get();
|
|
|
|
return view('admin.bonus-scores.index', compact('bonusScores', 'unassignedAuditions'));
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
$validData = request()->validate([
|
|
'name' => 'required',
|
|
'max_score' => 'required|numeric',
|
|
'weight' => 'required|numeric',
|
|
]);
|
|
|
|
BonusScoreDefinition::create($validData);
|
|
|
|
return to_route('admin.bonus-scores.index')->with('success', 'Bonus Score Created');
|
|
}
|
|
|
|
public function destroy(BonusScoreDefinition $bonusScore)
|
|
{
|
|
if ($bonusScore->auditions()->count() > 0) {
|
|
return to_route('admin.bonus-scores.index')->with('error', 'Bonus Score has auditions attached');
|
|
}
|
|
$bonusScore->delete();
|
|
|
|
return to_route('admin.bonus-scores.index')->with('success', 'Bonus Score Deleted');
|
|
}
|
|
|
|
public function assignAuditions(Request $request)
|
|
{
|
|
$validData = $request->validate([
|
|
'bonus_score_id' => 'required|exists:bonus_score_definitions,id',
|
|
'audition' => 'required|array',
|
|
'audition.*' => ['required', new ValidateAuditionKey()],
|
|
]);
|
|
$bonusScore = BonusScoreDefinition::find($validData['bonus_score_id']);
|
|
|
|
foreach ($validData['audition'] as $auditionId => $value) {
|
|
try {
|
|
$bonusScore->auditions()->attach($auditionId);
|
|
} catch (Exception) {
|
|
return redirect()->route('admin.bonus-scores.index')->with('error',
|
|
'Error assigning auditions to bonus score');
|
|
}
|
|
}
|
|
|
|
return redirect()->route('admin.bonus-scores.index')->with('success', 'Auditions assigned to bonus score');
|
|
}
|
|
|
|
public function unassignAudition(Audition $audition)
|
|
{
|
|
if (! $audition->exists()) {
|
|
return redirect()->route('admin.bonus-scores.index')->with('error', 'Audition not found');
|
|
}
|
|
if (! $audition->bonusScore()->count() > 0) {
|
|
return redirect()->route('admin.bonus-scores.index')->with('error', 'Audition does not have a bonus score');
|
|
}
|
|
$audition->bonusScore()->detach();
|
|
|
|
return redirect()->route('admin.bonus-scores.index')->with('success', 'Audition unassigned from bonus score');
|
|
}
|
|
}
|