Auditionadmin 29 #40

Merged
okorpheus merged 5 commits from auditionadmin-29 into master 2024-07-17 21:22:23 +00:00
3 changed files with 35 additions and 14 deletions
Showing only changes of commit 19152f4f63 - Show all commits

View File

@ -1,7 +1,5 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
namespace App\Actions;
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);
}
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

View File

@ -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)

View File

@ -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,