Auditionadmin 29 #40

Merged
okorpheus merged 5 commits from auditionadmin-29 into master 2024-07-17 21:22:23 +00:00
7 changed files with 391 additions and 30 deletions
Showing only changes of commit 12c37b6250 - Show all commits

View File

@ -1,8 +1,8 @@
<?php
namespace App\Actions;
namespace App\Actions\Entries;
use App\Exceptions\CreateEntryException;
use App\Exceptions\ManageEntryException;
use App\Models\Audition;
use App\Models\Entry;
use App\Models\Student;
@ -14,7 +14,7 @@ class CreateEntry
}
/**
* @throws CreateEntryException
* @throws ManageEntryException
*/
public function __invoke(Student|int $student, Audition|int $audition, string|array|null $entry_for = null): void
{
@ -22,7 +22,7 @@ class CreateEntry
}
/**
* @throws CreateEntryException
* @throws ManageEntryException
*/
public function createEntry(Student|int $student, Audition|int $audition, string|array|null $entry_for = null)
{
@ -62,33 +62,33 @@ class CreateEntry
}
/** @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
if (! $student->exists()) {
throw new CreateEntryException('Invalid student provided');
if (! $student || ! $student->exists()) {
throw new ManageEntryException('Invalid student provided');
}
// Make sure the audition is valid
if (! $audition->exists()) {
throw new CreateEntryException('Invalid audition provided');
if (! $audition || ! $audition->exists()) {
throw new ManageEntryException('Invalid audition provided');
}
// A student can't enter the same audition twice
if (Entry::where('student_id', $student->id)->where('audition_id', $audition->id)->exists()) {
throw new CreateEntryException('That student is already entered in that audition');
throw new ManageEntryException('That student is already entered in that audition');
}
// Can't enter a published audition
if ($audition->hasFlag('seats_published')) {
throw new CreateEntryException('Cannot add an entry to an audition where seats are published');
throw new ManageEntryException('Cannot add an entry to an audition where seats are published');
}
if ($audition->hasFlag('advancement_published')) {
throw new CreateEntryException('Cannot add an entry to an audition where advancement is published');
throw new ManageEntryException('Cannot add an entry to an audition where advancement is published');
}
// Verify the grade of the student is in range for the audition
if ($student->grade > $audition->maximum_grade) {
throw new CreateEntryException('The grade of the student exceeds the maximum for that audition');
throw new ManageEntryException('The grade of the student exceeds the maximum for that audition');
}
if ($student->grade < $audition->minimum_grade) {
throw new CreateEntryException('The grade of the student does not meet the minimum for that audition');
throw new ManageEntryException('The grade of the student does not meet the minimum for that audition');
}
}
}

View File

@ -0,0 +1,142 @@
<?php
namespace App\Actions\Entries;
use App\Exceptions\ManageEntryException;
use App\Models\Audition;
use App\Models\Entry;
use function array_key_exists;
class UpdateEntry
{
protected Entry $entry;
public function __construct()
{
}
/**
* @throws ManageEntryException
*/
public function __invoke(Entry $entry, array $updateData): void
{
$this->updateEntry($entry, $updateData);
}
/**
* @throws ManageEntryException
*/
public function updateEntry(Entry|int $entry, array $updateData): void
{
if (is_int($entry)) {
$entry = Entry::find($entry);
}
if (! $entry || ! $entry->exists) {
throw new ManageEntryException('Invalid entry provided');
}
$this->entry = $entry;
if (array_key_exists('for_seating', $updateData)) {
$this->updateForSeating($updateData['for_seating']);
}
if (array_key_exists('for_advancement', $updateData)) {
$this->updateForAdvancement($updateData['for_advancement']);
}
if (array_key_exists('audition', $updateData)) {
$this->updateAudition($updateData['audition']);
}
$this->entry->save();
}
/**
* @throws ManageEntryException
*/
private function updateAudition(Audition|int $audition): void
{
if (is_int($audition)) {
$audition = Audition::find($audition);
}
if (! $audition || ! $audition->exists) {
throw new ManageEntryException('Invalid audition provided');
}
if ($this->entry->audition->hasFlag('seats_published')) {
throw new ManageEntryException('Cannot change the audition for an entry where seating for that entry\'s current audition is published');
}
if ($this->entry->audition->hasFlag('advancement_published')) {
throw new ManageEntryException('Cannot change the audition for an entry where advancement for that entry\'s current audition is published');
}
if ($audition->hasFlag('seats_published')) {
throw new ManageEntryException('Cannot change the entry to an audition with published seating');
}
if ($audition->hasFlag('advancement_published')) {
throw new ManageEntryException('Cannot change the entry to an audition with published advancement');
}
if ($this->entry->student->grade > $audition->maximum_grade) {
throw new ManageEntryException('The grade of the student exceeds the maximum for that audition');
}
if ($this->entry->student->grade < $audition->minimum_grade) {
throw new ManageEntryException('The grade of the student does not meet the minimum for that audition');
}
if ($this->entry->scoreSheets()->count() > 0) {
throw new ManageEntryException('Cannot change the audition for an entry with scores');
}
if (Entry::where('student_id', $this->entry->student_id)->where('audition_id', $audition->id)->exists()) {
throw new ManageEntryException('That student is already entered in that audition');
}
// OK we're allowed to change the audition
$this->entry->audition_id = $audition->id;
// Deal with our draw number
if ($audition->hasFlag('drawn')) {
$draw_number = $audition->entries()->max('draw_number');
$this->entry->draw_number = $draw_number + 1;
} else {
$this->entry->draw_number = null;
}
}
/**
* @throws ManageEntryException
*/
private function updateForSeating($forSeating): void
{
if ($this->entry->for_seating == $forSeating) {
return;
}
if ($forSeating) {
if ($this->entry->audition->hasFlag('seats_published')) {
throw new ManageEntryException('Cannot add seating to an entry in an audition where seats are published');
}
$this->entry->for_seating = 1;
} else {
if ($this->entry->audition->hasFlag('seats_published')) {
throw new ManageEntryException('Cannot remove seating from an entry in an audition where seats are published');
}
$this->entry->for_seating = 0;
}
}
/**
* @throws ManageEntryException
*/
private function updateForAdvancement($forAdvancement): void
{
if ($this->entry->for_advancement == $forAdvancement) {
return;
}
if ($forAdvancement) {
if ($this->entry->audition->hasFlag('advancement_published')) {
throw new ManageEntryException('Cannot add advancement to an entry in an audition where advancement is published');
}
$this->entry->for_advancement = 1;
} else {
if ($this->entry->audition->hasFlag('advancement_published')) {
throw new ManageEntryException('Cannot remove advancement from an entry in an audition where advancement is published');
}
$this->entry->for_advancement = 0;
}
}
}

View File

@ -4,6 +4,6 @@ namespace App\Exceptions;
use Exception;
class CreateEntryException extends Exception
class ManageEntryException extends Exception
{
}

View File

@ -2,9 +2,9 @@
namespace App\Http\Controllers\Admin;
use App\Actions\CreateEntry;
use App\Actions\Entries\CreateEntry;
use App\Actions\Tabulation\CalculateEntryScore;
use App\Exceptions\CreateEntryException;
use App\Exceptions\ManageEntryException;
use App\Http\Controllers\Controller;
use App\Models\Audition;
use App\Models\Entry;
@ -111,7 +111,7 @@ class EntryController extends Controller
try {
$creator($validData['student_id'], $validData['audition_id'], $enter_for);
} catch (CreateEntryException $ex) {
} catch (ManageEntryException $ex) {
return redirect()->route('admin.entries.index')->with('error', $ex->getMessage());
}

View File

@ -2,8 +2,8 @@
namespace App\Http\Controllers;
use App\Actions\CreateEntry;
use App\Exceptions\CreateEntryException;
use App\Actions\Entries\CreateEntry;
use App\Exceptions\ManageEntryException;
use App\Models\Audition;
use App\Models\Entry;
use Illuminate\Http\Request;
@ -53,7 +53,7 @@ class EntryController extends Controller
try {
$creator($validData['student_id'], $validData['audition_id'], $enter_for);
} catch (CreateEntryException $ex) {
} catch (ManageEntryException $ex) {
return redirect()->route('entries.index')->with('error', $ex->getMessage());
}

View File

@ -1,7 +1,7 @@
<?php
use App\Actions\CreateEntry;
use App\Exceptions\CreateEntryException;
use App\Actions\Entries\CreateEntry;
use App\Exceptions\ManageEntryException;
use App\Models\Audition;
use App\Models\Entry;
use App\Models\Student;
@ -18,12 +18,12 @@ it('throws an exception if the student does not exist', function () {
$audition = Audition::factory()->create();
$student = Student::factory()->make();
$this->createEntry->__invoke($student, $audition);
})->throws(CreateEntryException::class, 'Invalid student provided');
})->throws(ManageEntryException::class, 'Invalid student provided');
it('throws an exception if the audition does not exist', function () {
$audition = Audition::factory()->make();
$student = Student::factory()->create();
$this->createEntry->__invoke($student, $audition);
})->throws(CreateEntryException::class, 'Invalid audition provided');
})->throws(ManageEntryException::class, 'Invalid audition provided');
it('throws an exception if the student is already entered in the audition', function () {
// Arrange
$audition = Audition::factory()->create();
@ -34,7 +34,7 @@ it('throws an exception if the student is already entered in the audition', func
]);
// Act & Assert
$this->createEntry->createEntry($student, $audition);
})->throws(CreateEntryException::class, 'That student is already entered in that audition');
})->throws(ManageEntryException::class, 'That student is already entered in that audition');
it('throws an exception if seats are published for the audition', function () {
// Arrange
$audition = Audition::factory()->create();
@ -42,7 +42,7 @@ it('throws an exception if seats are published for the audition', function () {
$audition->addFlag('seats_published');
// Act & Assert
$this->createEntry->createEntry($student, $audition);
})->throws(CreateEntryException::class, 'Cannot add an entry to an audition where seats are published');
})->throws(ManageEntryException::class, 'Cannot add an entry to an audition where seats are published');
it('throws an exception if advancement is published for the audition', function () {
// Arrange
$audition = Audition::factory()->create();
@ -50,21 +50,21 @@ it('throws an exception if advancement is published for the audition', function
$audition->addFlag('advancement_published');
// Act & Assert
$this->createEntry->createEntry($student, $audition);
})->throws(CreateEntryException::class, 'Cannot add an entry to an audition where advancement is published');
})->throws(ManageEntryException::class, 'Cannot add an entry to an audition where advancement is published');
it('throws an exception if the grade of the student exceeds the maximum grade for the audition', function () {
// Arrange
$audition = Audition::factory()->create(['minimum_grade' => 8, 'maximum_grade' => 9]);
$student = Student::factory()->create(['grade' => 11]);
// Act & Assert
$this->createEntry->createEntry($student, $audition);
})->throws(CreateEntryException::class, 'The grade of the student exceeds the maximum for that audition');
})->throws(ManageEntryException::class, 'The grade of the student exceeds the maximum for that audition');
it('throws an exception if the grade of the student does not meet the minimum grade for the audition', function () {
// Arrange
$audition = Audition::factory()->create(['minimum_grade' => 8, 'maximum_grade' => 9]);
$student = Student::factory()->create(['grade' => 7]);
// Act & Assert
$this->createEntry->createEntry($student, $audition);
})->throws(CreateEntryException::class, 'The grade of the student does not meet the minimum for that audition');
})->throws(ManageEntryException::class, 'The grade of the student does not meet the minimum for that audition');
it('returns an entry object', function () {
// Arrange
$audition = Audition::factory()->create(['minimum_grade' => 8, 'maximum_grade' => 9]);

View File

@ -0,0 +1,219 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
use App\Actions\Entries\UpdateEntry;
use App\Exceptions\ManageEntryException;
use App\Models\Audition;
use App\Models\Entry;
use App\Models\ScoreSheet;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\App;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->updater = App::make(UpdateEntry::class);
});
it('throws an error if an invalid entry is provided', function () {
$this->updater->updateEntry(2, []);
})->throws(ManageEntryException::class, 'Invalid entry provided');
it('throws an error if we try to remove for_seating while seating is published', function () {
// Arrange
$entry = Entry::factory()->create();
$entry->audition->addFlag('seats_published');
$data = ['for_seating' => 0];
// Act & Assert
$this->updater->updateEntry($entry, $data);
})->throws('Cannot remove seating from an entry in an audition where seats are published');
it('throws an error if we try to add for_seating while seating is published', function () {
// Arrange
$entry = Entry::factory()->advanceOnly()->create();
$entry->audition->addFlag('seats_published');
$data = ['for_seating' => 1];
// Act & Assert
$this->updater->updateEntry($entry, $data);
})->throws('Cannot add seating to an entry in an audition where seats are published');
it('allows us to remove for_seating if seating is not published', function () {
// Arrange
$entry = Entry::factory()->create();
$data = ['for_seating' => 0];
// Act
$this->updater->updateEntry($entry, $data);
// Assert
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'for_seating' => 0]);
});
it('allows us to add for_seating if seating is not published', function () {
// Arrange
$entry = Entry::factory()->advanceOnly()->create();
$data = ['for_seating' => 1];
// Act
$this->updater->updateEntry($entry, $data);
// Assert
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'for_seating' => 1]);
});
it('throws an error if we try to remove for_advancement while seating is published', function () {
// Arrange
$entry = Entry::factory()->create();
$entry->audition->addFlag('advancement_published');
$data = ['for_advancement' => 0];
// Act & Assert
$this->updater->updateEntry($entry, $data);
})->throws('Cannot remove advancement from an entry in an audition where advancement is published');
it('throws an error if we try to add for_advancement while advancement is published', function () {
// Arrange
$entry = Entry::factory()->seatingOnly()->create();
$entry->audition->addFlag('advancement_published');
$data = ['for_advancement' => 1];
// Act & Assert
$this->updater->updateEntry($entry, $data);
})->throws('Cannot add advancement to an entry in an audition where advancement is published');
it('allows us to remove for_advancement if advancement is not published', function () {
// Arrange
$entry = Entry::factory()->create();
$data = ['for_advancement' => 0];
// Act
$this->updater->updateEntry($entry, $data);
// Assert
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'for_advancement' => 0]);
});
it('allows us to add for_advancement if advancement is not published', function () {
// Arrange
$entry = Entry::factory()->seatingOnly()->create();
$data = ['for_advancement' => 1];
// Act
$this->updater->updateEntry($entry, $data);
// Assert
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'for_advancement' => 1]);
});
it('throws an exception if an attempt to change to an invalid audition is made', function () {
$entry = Entry::factory()->create();
$data = ['audition' => 2];
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'Invalid audition provided');
it('cannot change auditions if our current audition advancement is published', function () {
$entry = Entry::factory()->create();
$entry->audition->addFlag('advancement_published');
$otherAudition = Audition::factory()->create();
$data = ['audition' => $otherAudition];
// Act
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class,
'Cannot change the audition for an entry where advancement for that entry\'s current audition is published');
it('cannot change auditions if our current audition seating is published', function () {
$entry = Entry::factory()->create();
$entry->audition->addFlag('seats_published');
$otherAudition = Audition::factory()->create();
$data = ['audition' => $otherAudition];
// Act
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class,
'Cannot change the audition for an entry where seating for that entry\'s current audition is published');
it('cannot change auditions if our proposed audition advancement is published', function () {
$entry = Entry::factory()->create();
$otherAudition = Audition::factory()->create();
$otherAudition->addFlag('advancement_published');
$data = ['audition' => $otherAudition];
// Act
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'Cannot change the entry to an audition with published advancement');
it('cannot change auditions if our proposed audition seating is published', function () {
$entry = Entry::factory()->create();
$otherAudition = Audition::factory()->create();
$otherAudition->addFlag('seats_published');
$data = ['audition' => $otherAudition];
// Act
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'Cannot change the entry to an audition with published seating');
it('will not let us switch to an audition that is too old for the student', function () {
$entry = Entry::factory()->create();
$otherAudition = Audition::factory()->create(['minimum_grade' => 8, 'maximum_grade' => 9]);
$entry->student->update(['grade' => 7]);
$data = ['audition' => $otherAudition];
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'The grade of the student does not meet the minimum for that audition');
it('will not let us switch to an audition that is too young for the student', function () {
$entry = Entry::factory()->create();
$otherAudition = Audition::factory()->create(['minimum_grade' => 8, 'maximum_grade' => 9]);
$entry->student->update(['grade' => 11]);
$data = ['audition' => $otherAudition];
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'The grade of the student exceeds the maximum for that audition');
it('will not let us change auditions for an entry with scores', function () {
$entry = Entry::factory()->create();
$judge = User::factory()->create();
ScoreSheet::create([
'entry_id' => $entry->id,
'user_id' => $judge->id,
'subscores' => 100,
]);
$otherAudition = Audition::factory()->create([
'minimum_grade' => $entry->student->grade, 'maximum_grade' => $entry->student->grade,
]);
$data = ['audition' => $otherAudition];
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'Cannot change the audition for an entry with scores');
it('will not let us change to an audition that the student is already entered in', function () {
$entry = Entry::factory()->create();
$otherAudition = Audition::factory()->create([
'minimum_grade' => $entry->student->grade, 'maximum_grade' => $entry->student->grade,
]);
Entry::create([
'student_id' => $entry->student->id,
'audition_id' => $otherAudition->id,
]);
$data = ['audition' => $otherAudition];
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'That student is already entered in that audition');
it('allows us to switch auditions', function () {
// Arrange
$entry = Entry::factory()->create();
$originalAudition = $entry->audition;
$otherAudition = Audition::factory()->create([
'minimum_grade' => $entry->student->grade, 'maximum_grade' => $entry->student->grade,
]);
$data = ['audition' => $otherAudition];
// Act & Assert
$this->updater->updateEntry($entry, $data);
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'audition_id' => $otherAudition->id]);
$this->assertDatabaseMissing('entries',
['student_id' => $entry->student->id, 'audition_id' => $originalAudition->id]);
});
it('sets the draw number null if the new audition is not drawn', function () {
// Arrange
$entry = Entry::factory()->create(['draw_number' => 3]);
$newAudition = Audition::factory()->create([
'minimum_grade' => $entry->student->grade, 'maximum_grade' => $entry->student->grade,
]);
$data = ['audition' => $newAudition];
// Act
$this->updater->updateEntry($entry, $data);
// Assert
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'draw_number' => null]);
});
it('sets the draw number last if the new audition is not drawn', function () {
// Arrange
$entry = Entry::factory()->create(['draw_number' => 3]);
$newAudition = Audition::factory()->create([
'minimum_grade' => $entry->student->grade, 'maximum_grade' => $entry->student->grade,
]);
$newAudition->addFlag('drawn');
foreach (range(1, 6) as $dn) {
Entry::factory()->create(['audition_id' => $newAudition->id, 'draw_number' => $dn]);
}
$data = ['audition' => $newAudition];
// Act
$this->updater->updateEntry($entry, $data);
// Assert
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'draw_number' => 7]);
});