From 9ae4b0388a54251bfdd1cb88e5c8b61fb9811d6b Mon Sep 17 00:00:00 2001 From: Matt Young Date: Tue, 1 Jul 2025 11:43:36 -0500 Subject: [PATCH] Enforce unique school names Rewrite admin school controler to use action when creating a school. --- app/Actions/Schools/CreateSchool.php | 6 +++++ .../Controllers/Admin/SchoolController.php | 27 ++++++++----------- .../app/Actions/Schools/CreateSchoolTest.php | 11 ++++++++ 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/app/Actions/Schools/CreateSchool.php b/app/Actions/Schools/CreateSchool.php index dcada3b..f7c618c 100644 --- a/app/Actions/Schools/CreateSchool.php +++ b/app/Actions/Schools/CreateSchool.php @@ -2,6 +2,7 @@ namespace App\Actions\Schools; +use App\Exceptions\AuditionAdminException; use App\Models\School; class CreateSchool @@ -23,6 +24,11 @@ class CreateSchool ?string $state = null, ?string $zip = null ): School { + + if (School::where('name', $name)->exists()) { + throw new AuditionAdminException('The school '.$name.' already exists'); + } + $newSchool = School::create([ 'name' => $name, 'address' => $address, diff --git a/app/Http/Controllers/Admin/SchoolController.php b/app/Http/Controllers/Admin/SchoolController.php index a224006..ccd05fd 100644 --- a/app/Http/Controllers/Admin/SchoolController.php +++ b/app/Http/Controllers/Admin/SchoolController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Admin; +use App\Actions\Schools\CreateSchool; use App\Actions\Schools\SetHeadDirector; use App\Http\Controllers\Controller; use App\Models\AuditLogEntry; @@ -95,28 +96,22 @@ class SchoolController extends Controller public function store() { - request()->validate([ - 'name' => ['required'], + $creator = app(CreateSchool::class); + $validData = request()->validate([ + 'name' => ['required', 'unique:schools,name'], 'address' => ['required'], 'city' => ['required'], 'state' => ['required'], 'zip' => ['required'], ]); - $school = School::create([ - 'name' => request('name'), - 'address' => request('address'), - 'city' => request('city'), - 'state' => request('state'), - 'zip' => request('zip'), - ]); - $message = 'Created school #'.$school->id.' - '.$school->name.' with address
'.$school->address.'
'.$school->city.', '.$school->state.' '.$school->zip; - AuditLogEntry::create([ - 'user' => auth()->user()->email, - 'ip_address' => request()->ip(), - 'message' => $message, - 'affected' => ['schools' => [$school->id]], - ]); + $school = $creator( + $validData['name'], + $validData['address'], + $validData['city'], + $validData['state'], + $validData['zip'], + ); return redirect('/admin/schools')->with('success', 'School '.$school->name.' created'); } diff --git a/tests/Feature/app/Actions/Schools/CreateSchoolTest.php b/tests/Feature/app/Actions/Schools/CreateSchoolTest.php index a05b248..4df728a 100644 --- a/tests/Feature/app/Actions/Schools/CreateSchoolTest.php +++ b/tests/Feature/app/Actions/Schools/CreateSchoolTest.php @@ -1,6 +1,7 @@ and($logEntry->user)->toEqual(auth()->user()->email); }); + +it('will not create a school with a duplicate name', function () { + $schoolName = 'Longfellow Intermediate'; + $this->creator->create( + $schoolName + ); + $this->creator->create( + $schoolName + ); +})->throws(AuditionAdminException::class, 'The school Longfellow Intermediate already exists');