Auditionadmin 29 #40
|
|
@ -1,7 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/** @noinspection PhpUnhandledExceptionInspection */
|
|
||||||
|
|
||||||
namespace App\Actions;
|
namespace App\Actions;
|
||||||
|
|
||||||
use App\Exceptions\CreateEntryException;
|
use App\Exceptions\CreateEntryException;
|
||||||
|
|
@ -15,13 +13,26 @@ class CreateEntry
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __invoke(Student $student, Audition $audition, string|array|null $entry_for = null): void
|
/**
|
||||||
|
* @throws CreateEntryException
|
||||||
|
*/
|
||||||
|
public function __invoke(Student|int $student, Audition|int $audition, string|array|null $entry_for = null): void
|
||||||
{
|
{
|
||||||
$this->createEntry($student, $audition, $entry_for);
|
$this->createEntry($student, $audition, $entry_for);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createEntry(Student $student, Audition $audition, string|array|null $entry_for = null)
|
/**
|
||||||
|
* @throws CreateEntryException
|
||||||
|
*/
|
||||||
|
public function createEntry(Student|int $student, Audition|int $audition, string|array|null $entry_for = null)
|
||||||
{
|
{
|
||||||
|
if (is_int($student)) {
|
||||||
|
$student = Student::find($student);
|
||||||
|
}
|
||||||
|
if (is_int($audition)) {
|
||||||
|
$audition = Audition::find($audition);
|
||||||
|
}
|
||||||
|
|
||||||
if (! $entry_for) {
|
if (! $entry_for) {
|
||||||
$entry_for = ['seating', 'advancement'];
|
$entry_for = ['seating', 'advancement'];
|
||||||
}
|
}
|
||||||
|
|
@ -50,6 +61,7 @@ class CreateEntry
|
||||||
return $draw_number + 1;
|
return $draw_number + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @noinspection PhpUnhandledExceptionInspection */
|
||||||
private function verifySubmission(Student $student, Audition $audition): void
|
private function verifySubmission(Student $student, Audition $audition): void
|
||||||
{
|
{
|
||||||
// Make sure it's a valid student
|
// Make sure it's a valid student
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Actions\CreateEntry;
|
||||||
|
use App\Exceptions\CreateEntryException;
|
||||||
use App\Models\Audition;
|
use App\Models\Audition;
|
||||||
use App\Models\Entry;
|
use App\Models\Entry;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
@ -25,7 +27,7 @@ class EntryController extends Controller
|
||||||
return view('entries.index', ['entries' => $entries, 'students' => $students, 'auditions' => $auditions]);
|
return view('entries.index', ['entries' => $entries, 'students' => $students, 'auditions' => $auditions]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function store(Request $request)
|
public function store(Request $request, CreateEntry $creator)
|
||||||
{
|
{
|
||||||
if ($request->user()->cannot('create', Entry::class)) {
|
if ($request->user()->cannot('create', Entry::class)) {
|
||||||
abort(403);
|
abort(403);
|
||||||
|
|
@ -37,15 +39,21 @@ class EntryController extends Controller
|
||||||
|
|
||||||
$validData['for_seating'] = $request->get('for_seating') ? 1 : 0;
|
$validData['for_seating'] = $request->get('for_seating') ? 1 : 0;
|
||||||
$validData['for_advancement'] = $request->get('for_advancement') ? 1 : 0;
|
$validData['for_advancement'] = $request->get('for_advancement') ? 1 : 0;
|
||||||
|
$enter_for = [];
|
||||||
|
if ($validData['for_seating']) {
|
||||||
|
$enter_for[] = 'seating';
|
||||||
|
}
|
||||||
|
if ($validData['for_advancement']) {
|
||||||
|
$enter_for[] = 'advancement';
|
||||||
|
}
|
||||||
|
|
||||||
$entry = Entry::create([
|
try {
|
||||||
'student_id' => $validData['student_id'],
|
$creator($validData['student_id'], $validData['audition_id'], $enter_for);
|
||||||
'audition_id' => $validData['audition_id'],
|
} catch (CreateEntryException $ex) {
|
||||||
'for_seating' => $validData['for_seating'],
|
return redirect()->route('entries.index')->with('error', $ex->getMessage());
|
||||||
'for_advancement' => $validData['for_advancement'],
|
}
|
||||||
]);
|
|
||||||
|
|
||||||
return redirect('/entries');
|
return redirect()->route('entries.index')->with('success', 'The entry has been added.');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function destroy(Request $request, Entry $entry)
|
public function destroy(Request $request, Entry $entry)
|
||||||
|
|
|
||||||
|
|
@ -133,8 +133,8 @@ it('shows appropriate flags for entry types when advancement is enabled', functi
|
||||||
|
|
||||||
it('accepts a valid entry', function () {
|
it('accepts a valid entry', function () {
|
||||||
// Arrange
|
// Arrange
|
||||||
$student = Student::factory()->create(['school_id' => $this->school->id]);
|
$student = Student::factory()->create(['school_id' => $this->school->id, 'grade' => 8]);
|
||||||
$audition = Audition::factory()->create();
|
$audition = Audition::factory()->create(['maximum_grade' => 9, 'minimum_grade' => 7]);
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
actingAs($this->user);
|
actingAs($this->user);
|
||||||
$response = post(route('entries.store'), [
|
$response = post(route('entries.store'), [
|
||||||
|
|
@ -144,6 +144,7 @@ it('accepts a valid entry', function () {
|
||||||
/** @noinspection PhpUnhandledExceptionInspection */
|
/** @noinspection PhpUnhandledExceptionInspection */
|
||||||
$response->assertSessionHasNoErrors();
|
$response->assertSessionHasNoErrors();
|
||||||
$response->assertRedirect(route('entries.index'));
|
$response->assertRedirect(route('entries.index'));
|
||||||
|
$response->assertSessionHas('success', 'The entry has been added.');
|
||||||
$this->assertDatabaseHas('entries', [
|
$this->assertDatabaseHas('entries', [
|
||||||
'student_id' => $student->id,
|
'student_id' => $student->id,
|
||||||
'audition_id' => $audition->id,
|
'audition_id' => $audition->id,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue