From 19152f4f634622f2f0af19ca3ff41ff6ab5ca07f Mon Sep 17 00:00:00 2001 From: Matt Young Date: Wed, 17 Jul 2024 11:45:29 -0500 Subject: [PATCH] User createEntry action in user entry creation #29 Creating an entry should check on the status of the draw and respond appropriately --- app/Actions/CreateEntry.php | 20 ++++++++++++++++---- app/Http/Controllers/EntryController.php | 24 ++++++++++++++++-------- tests/Feature/Pages/EntriesIndexTest.php | 5 +++-- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app/Actions/CreateEntry.php b/app/Actions/CreateEntry.php index a3bf35d..1cfacc6 100644 --- a/app/Actions/CreateEntry.php +++ b/app/Actions/CreateEntry.php @@ -1,7 +1,5 @@ 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) { $entry_for = ['seating', 'advancement']; } @@ -50,6 +61,7 @@ class CreateEntry return $draw_number + 1; } + /** @noinspection PhpUnhandledExceptionInspection */ private function verifySubmission(Student $student, Audition $audition): void { // Make sure it's a valid student diff --git a/app/Http/Controllers/EntryController.php b/app/Http/Controllers/EntryController.php index c102318..3180d46 100644 --- a/app/Http/Controllers/EntryController.php +++ b/app/Http/Controllers/EntryController.php @@ -2,6 +2,8 @@ namespace App\Http\Controllers; +use App\Actions\CreateEntry; +use App\Exceptions\CreateEntryException; use App\Models\Audition; use App\Models\Entry; use Illuminate\Http\Request; @@ -25,7 +27,7 @@ class EntryController extends Controller 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)) { abort(403); @@ -37,15 +39,21 @@ class EntryController extends Controller $validData['for_seating'] = $request->get('for_seating') ? 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([ - 'student_id' => $validData['student_id'], - 'audition_id' => $validData['audition_id'], - 'for_seating' => $validData['for_seating'], - 'for_advancement' => $validData['for_advancement'], - ]); + try { + $creator($validData['student_id'], $validData['audition_id'], $enter_for); + } catch (CreateEntryException $ex) { + return redirect()->route('entries.index')->with('error', $ex->getMessage()); + } - return redirect('/entries'); + return redirect()->route('entries.index')->with('success', 'The entry has been added.'); } public function destroy(Request $request, Entry $entry) diff --git a/tests/Feature/Pages/EntriesIndexTest.php b/tests/Feature/Pages/EntriesIndexTest.php index fd47344..6ecc175 100644 --- a/tests/Feature/Pages/EntriesIndexTest.php +++ b/tests/Feature/Pages/EntriesIndexTest.php @@ -133,8 +133,8 @@ it('shows appropriate flags for entry types when advancement is enabled', functi it('accepts a valid entry', function () { // Arrange - $student = Student::factory()->create(['school_id' => $this->school->id]); - $audition = Audition::factory()->create(); + $student = Student::factory()->create(['school_id' => $this->school->id, 'grade' => 8]); + $audition = Audition::factory()->create(['maximum_grade' => 9, 'minimum_grade' => 7]); // Act & Assert actingAs($this->user); $response = post(route('entries.store'), [ @@ -144,6 +144,7 @@ it('accepts a valid entry', function () { /** @noinspection PhpUnhandledExceptionInspection */ $response->assertSessionHasNoErrors(); $response->assertRedirect(route('entries.index')); + $response->assertSessionHas('success', 'The entry has been added.'); $this->assertDatabaseHas('entries', [ 'student_id' => $student->id, 'audition_id' => $audition->id,