Enforce unique school names
Rewrite admin school controler to use action when creating a school.
This commit is contained in:
parent
bd207f8e4a
commit
9ae4b0388a
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Actions\Schools;
|
namespace App\Actions\Schools;
|
||||||
|
|
||||||
|
use App\Exceptions\AuditionAdminException;
|
||||||
use App\Models\School;
|
use App\Models\School;
|
||||||
|
|
||||||
class CreateSchool
|
class CreateSchool
|
||||||
|
|
@ -23,6 +24,11 @@ class CreateSchool
|
||||||
?string $state = null,
|
?string $state = null,
|
||||||
?string $zip = null
|
?string $zip = null
|
||||||
): School {
|
): School {
|
||||||
|
|
||||||
|
if (School::where('name', $name)->exists()) {
|
||||||
|
throw new AuditionAdminException('The school '.$name.' already exists');
|
||||||
|
}
|
||||||
|
|
||||||
$newSchool = School::create([
|
$newSchool = School::create([
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
'address' => $address,
|
'address' => $address,
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Http\Controllers\Admin;
|
namespace App\Http\Controllers\Admin;
|
||||||
|
|
||||||
|
use App\Actions\Schools\CreateSchool;
|
||||||
use App\Actions\Schools\SetHeadDirector;
|
use App\Actions\Schools\SetHeadDirector;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\AuditLogEntry;
|
use App\Models\AuditLogEntry;
|
||||||
|
|
@ -95,28 +96,22 @@ class SchoolController extends Controller
|
||||||
|
|
||||||
public function store()
|
public function store()
|
||||||
{
|
{
|
||||||
request()->validate([
|
$creator = app(CreateSchool::class);
|
||||||
'name' => ['required'],
|
$validData = request()->validate([
|
||||||
|
'name' => ['required', 'unique:schools,name'],
|
||||||
'address' => ['required'],
|
'address' => ['required'],
|
||||||
'city' => ['required'],
|
'city' => ['required'],
|
||||||
'state' => ['required'],
|
'state' => ['required'],
|
||||||
'zip' => ['required'],
|
'zip' => ['required'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$school = School::create([
|
$school = $creator(
|
||||||
'name' => request('name'),
|
$validData['name'],
|
||||||
'address' => request('address'),
|
$validData['address'],
|
||||||
'city' => request('city'),
|
$validData['city'],
|
||||||
'state' => request('state'),
|
$validData['state'],
|
||||||
'zip' => request('zip'),
|
$validData['zip'],
|
||||||
]);
|
);
|
||||||
$message = 'Created school #'.$school->id.' - '.$school->name.' with address <br>'.$school->address.'<br>'.$school->city.', '.$school->state.' '.$school->zip;
|
|
||||||
AuditLogEntry::create([
|
|
||||||
'user' => auth()->user()->email,
|
|
||||||
'ip_address' => request()->ip(),
|
|
||||||
'message' => $message,
|
|
||||||
'affected' => ['schools' => [$school->id]],
|
|
||||||
]);
|
|
||||||
|
|
||||||
return redirect('/admin/schools')->with('success', 'School '.$school->name.' created');
|
return redirect('/admin/schools')->with('success', 'School '.$school->name.' created');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Actions\Schools\CreateSchool;
|
use App\Actions\Schools\CreateSchool;
|
||||||
|
use App\Exceptions\AuditionAdminException;
|
||||||
use App\Models\School;
|
use App\Models\School;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
|
||||||
|
|
@ -47,3 +48,13 @@ it('logs school creation', function () {
|
||||||
->and($logEntry->user)->toEqual(auth()->user()->email);
|
->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');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue