Update SchoolController to use actions.
This commit is contained in:
parent
5ebef46be7
commit
d7134a948b
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\Schools\AssignUserToSchool;
|
||||
use App\Actions\Schools\CreateSchool;
|
||||
use App\Actions\Schools\SetHeadDirector;
|
||||
use App\Exceptions\AuditionAdminException;
|
||||
use App\Mail\NewUserPassword;
|
||||
|
|
@ -29,70 +31,41 @@ class SchoolController extends Controller
|
|||
if ($request->user()->cannot('create', School::class)) {
|
||||
abort(403);
|
||||
}
|
||||
request()->validate([
|
||||
'name' => ['required', 'min:3', 'max:30'],
|
||||
$validData = request()->validate([
|
||||
'name' => ['required', 'min:3', 'max:30', 'unique:schools,name'],
|
||||
'address' => ['required'],
|
||||
'city' => ['required'],
|
||||
'state' => ['required', 'min:2', 'max:2'],
|
||||
'zip' => ['required', 'min:5', 'max:10'],
|
||||
]);
|
||||
|
||||
$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 <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]],
|
||||
]);
|
||||
$creator = app(CreateSchool::class);
|
||||
|
||||
if (! Auth::user()->school) {
|
||||
Auth::user()->update([
|
||||
'school_id' => $school->id,
|
||||
]);
|
||||
$message = 'Set user '.auth()->user()->full_name().' ('.auth()->user()->email.') as a director at '.$school->name.'(#'.$school->id.')';
|
||||
AuditLogEntry::create([
|
||||
'user' => auth()->user()->email,
|
||||
'ip_address' => request()->ip(),
|
||||
'message' => $message,
|
||||
'affected' => [
|
||||
'users' => [auth()->user()->id],
|
||||
'schools' => [$school->id],
|
||||
],
|
||||
]);
|
||||
SchoolEmailDomain::create([
|
||||
'school_id' => $school->id,
|
||||
'domain' => Auth::user()->emailDomain(),
|
||||
]);
|
||||
$message = 'Added '.auth()->user()->emailDomain().' as an email domain for '.$school->name.' (#'.$school->id.')';
|
||||
AuditLogEntry::create([
|
||||
'user' => auth()->user()->email,
|
||||
'ip_address' => request()->ip(),
|
||||
'message' => $message,
|
||||
'affected' => [
|
||||
'schools' => [$school->id],
|
||||
],
|
||||
]);
|
||||
auth()->user()->refresh();
|
||||
try {
|
||||
$headSetter->setHeadDirector(auth()->user());
|
||||
} catch (AuditionAdminException $e) {
|
||||
redirect(route('schools.show', $school))->with('error', 'Could not set as head director');
|
||||
}
|
||||
$school = $creator(
|
||||
$validData['name'],
|
||||
$validData['address'],
|
||||
$validData['city'],
|
||||
$validData['state'],
|
||||
$validData['zip'],
|
||||
);
|
||||
|
||||
$assigner = app(AssignUserToSchool::class);
|
||||
$assigner(auth()->user(), $school);
|
||||
|
||||
auth()->user()->refresh();
|
||||
try {
|
||||
$headSetter->setHeadDirector(auth()->user());
|
||||
} catch (AuditionAdminException $e) {
|
||||
redirect(route('schools.show', $school))->with('error', 'Could not set as head director');
|
||||
}
|
||||
|
||||
return redirect('/schools/'.$school->id);
|
||||
}
|
||||
|
||||
public function show(Request $request, School $school)
|
||||
{
|
||||
public function show(
|
||||
Request $request,
|
||||
School $school
|
||||
) {
|
||||
if ($request->user()->cannot('view', $school)) {
|
||||
abort(403);
|
||||
}
|
||||
|
|
@ -100,8 +73,9 @@ class SchoolController extends Controller
|
|||
return view('schools.show', ['school' => $school]);
|
||||
}
|
||||
|
||||
public function create(Request $request)
|
||||
{
|
||||
public function create(
|
||||
Request $request
|
||||
) {
|
||||
if ($request->user()->cannot('create', School::class)) {
|
||||
abort(403);
|
||||
}
|
||||
|
|
@ -109,8 +83,10 @@ class SchoolController extends Controller
|
|||
return view('schools.create');
|
||||
}
|
||||
|
||||
public function edit(Request $request, School $school)
|
||||
{
|
||||
public function edit(
|
||||
Request $request,
|
||||
School $school
|
||||
) {
|
||||
if ($request->user()->cannot('update', $school)) {
|
||||
abort(403);
|
||||
}
|
||||
|
|
@ -118,8 +94,10 @@ class SchoolController extends Controller
|
|||
return view('schools.edit', ['school' => $school]);
|
||||
}
|
||||
|
||||
public function update(Request $request, School $school)
|
||||
{
|
||||
public function update(
|
||||
Request $request,
|
||||
School $school
|
||||
) {
|
||||
if ($request->user()->cannot('update', $school)) {
|
||||
abort(403);
|
||||
}
|
||||
|
|
@ -158,8 +136,9 @@ class SchoolController extends Controller
|
|||
return redirect('/schools/create');
|
||||
}
|
||||
|
||||
public function addDirector(School $school)
|
||||
{
|
||||
public function addDirector(
|
||||
School $school
|
||||
) {
|
||||
|
||||
if (auth()->user()->school_id !== $school->id) {
|
||||
return redirect()->back()->with('error', 'No adding directors to another school');
|
||||
|
|
@ -193,8 +172,11 @@ class SchoolController extends Controller
|
|||
return redirect()->back()->with('success', 'Director added');
|
||||
}
|
||||
|
||||
public function setHeadDirector(School $school, User $user, SetHeadDirector $headSetter)
|
||||
{
|
||||
public function setHeadDirector(
|
||||
School $school,
|
||||
User $user,
|
||||
SetHeadDirector $headSetter
|
||||
) {
|
||||
if (auth()->user()->school_id !== $school->id) {
|
||||
return redirect()->back()->with('error', 'No setting the head director for another school');
|
||||
}
|
||||
|
|
@ -213,8 +195,9 @@ class SchoolController extends Controller
|
|||
return redirect()->back()->with('success', 'New head director set');
|
||||
}
|
||||
|
||||
public function addDomain(School $school)
|
||||
{
|
||||
public function addDomain(
|
||||
School $school
|
||||
) {
|
||||
if (auth()->user()->school_id !== $school->id) {
|
||||
return redirect()->back()->with('error', 'No adding domains for another school');
|
||||
}
|
||||
|
|
@ -239,8 +222,9 @@ class SchoolController extends Controller
|
|||
return redirect()->back()->with('success', 'Domain added');
|
||||
}
|
||||
|
||||
public function deleteDomain(SchoolEmailDomain $domain)
|
||||
{
|
||||
public function deleteDomain(
|
||||
SchoolEmailDomain $domain
|
||||
) {
|
||||
if (auth()->user()->school_id !== $domain->school_id) {
|
||||
return redirect()->back()->with('error', 'No deleting domains for another school');
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue