From d95185d6e8d709806995cc79f9bb59fe93a59c88 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Tue, 28 May 2024 01:21:39 -0500 Subject: [PATCH] Work on Schools --- app/Http/Controllers/SchoolController.php | 86 ++++++++++++++++++- app/Models/School.php | 2 + app/Models/SchoolEmailDomain.php | 2 + app/Models/User.php | 9 +- ...2230_create_school_email_domains_table.php | 1 + .../views/components/auth/form-card.blade.php | 1 + .../views/components/info-card/card.blade.php | 12 +++ .../components/info-card/header.blade.php | 6 ++ .../views/components/info-card/row.blade.php | 6 ++ .../views/components/layout/navbar.blade.php | 4 +- resources/views/my_school.blade.php | 12 +++ resources/views/profile.blade.php | 21 ----- resources/views/schools/create.blade.php | 23 +++++ resources/views/schools/edit.blade.php | 23 +++++ resources/views/schools/show.blade.php | 37 ++++++++ routes/web.php | 10 +++ 16 files changed, 230 insertions(+), 25 deletions(-) create mode 100644 resources/views/components/info-card/card.blade.php create mode 100644 resources/views/components/info-card/header.blade.php create mode 100644 resources/views/components/info-card/row.blade.php create mode 100644 resources/views/my_school.blade.php create mode 100644 resources/views/schools/create.blade.php create mode 100644 resources/views/schools/edit.blade.php create mode 100644 resources/views/schools/show.blade.php diff --git a/app/Http/Controllers/SchoolController.php b/app/Http/Controllers/SchoolController.php index a2a12b2..515bba2 100644 --- a/app/Http/Controllers/SchoolController.php +++ b/app/Http/Controllers/SchoolController.php @@ -2,9 +2,93 @@ namespace App\Http\Controllers; +use App\Models\School; +use App\Models\SchoolEmailDomain; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Auth; +use function dd; +use function redirect; +use function request; class SchoolController extends Controller { - // + public function store() + { + request()->validate([ + 'name' => ['required', 'min:3', 'max:30'], + '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'), + ]); + + // TODO allow for an audition administrator that is not connected to school and needs to create a school without associating with it + + if (! Auth::user()->school) { + Auth::user()->update([ + 'school_id' => $school->id + ]); + + SchoolEmailDomain::create([ + 'school_id' => $school->id, + 'domain' => Auth::user()->emailDomain() + ]); + } + + + return redirect('/schools/' . $school->id); + } + + public function show(School $school) + { + return view('schools.show', ['school' => $school]); + } + + public function create() + { + return view('schools.create'); + } + + public function edit(School $school) + { + // TODO Restrict the editing of schools to directors or admin + return view('schools.edit', ['school' => $school]); + } + + public function update(School $school) + { + request()->validate([ + 'name' => ['required', 'min:3', 'max:30'], + 'address' => ['required'], + 'city' => ['required'], + 'state' => ['required', 'min:2', 'max:2'], + 'zip' => ['required', 'min:5', 'max:10'], + ]); + + $school->update([ + 'name' => request('name'), + 'address' => request('address'), + 'city' => request('city'), + 'state' => request('state'), + 'zip' => request('zip'), + ]); + // TODO Handle redirect after updating school more elegantly + return redirect('/schools/' . $school->id); + } + + public function my_school() + { + if (Auth::user()->school) { + return redirect('/schools/' . Auth::user()->school->id); + } + return redirect('/schools/create'); + } } diff --git a/app/Models/School.php b/app/Models/School.php index 7e19159..ffb7cf2 100644 --- a/app/Models/School.php +++ b/app/Models/School.php @@ -10,6 +10,8 @@ class School extends Model { use HasFactory; + protected $guarded = []; + public function directors(): HasMany { return $this->hasMany(User::class); diff --git a/app/Models/SchoolEmailDomain.php b/app/Models/SchoolEmailDomain.php index edb881e..1f5b40e 100644 --- a/app/Models/SchoolEmailDomain.php +++ b/app/Models/SchoolEmailDomain.php @@ -8,4 +8,6 @@ use Illuminate\Database\Eloquent\Model; class SchoolEmailDomain extends Model { use HasFactory; + protected $guarded = []; + public $timestamps = false; } diff --git a/app/Models/User.php b/app/Models/User.php index a279ef8..476c2e8 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -25,7 +25,8 @@ class User extends Authenticatable implements MustVerifyEmail 'cell_phone', 'email', 'password', - 'profile_image_url' + 'profile_image_url', + 'school_id' ]; /** @@ -56,6 +57,12 @@ class User extends Authenticatable implements MustVerifyEmail return $this->first_name . ' ' . $this->last_name; } + public function emailDomain(): string + { + $pos = strpos($this->email, '@'); + return substr($this->email, $pos+1); + } + public function school(): BelongsTo { return $this->belongsTo(School::class); diff --git a/database/migrations/2024_05_28_022230_create_school_email_domains_table.php b/database/migrations/2024_05_28_022230_create_school_email_domains_table.php index a4755b5..98ffefb 100644 --- a/database/migrations/2024_05_28_022230_create_school_email_domains_table.php +++ b/database/migrations/2024_05_28_022230_create_school_email_domains_table.php @@ -16,6 +16,7 @@ return new class extends Migration $table->id(); $table->foreignIdFor(School::class)->constrained()->onDelete('cascade')->onUpdate('cascade'); $table->string('domain'); + $table->unique(['school_id','domain']); }); } diff --git a/resources/views/components/auth/form-card.blade.php b/resources/views/components/auth/form-card.blade.php index ecfc39b..8fd8c8a 100644 --- a/resources/views/components/auth/form-card.blade.php +++ b/resources/views/components/auth/form-card.blade.php @@ -20,6 +20,7 @@ @if($buttons) {{ $buttons }} @else + Cancel {{ $submitButtonText }} @endif diff --git a/resources/views/components/info-card/card.blade.php b/resources/views/components/info-card/card.blade.php new file mode 100644 index 0000000..3e956fe --- /dev/null +++ b/resources/views/components/info-card/card.blade.php @@ -0,0 +1,12 @@ +@props(['heading' => false, 'subheading' => false]) +
+ @if($heading) + @include('components.info-card.header', ['heading' => $heading, 'subheading' => $subheading]) + @endif + +
+
+ {{ $slot }} +
+
+
diff --git a/resources/views/components/info-card/header.blade.php b/resources/views/components/info-card/header.blade.php new file mode 100644 index 0000000..d4af6d1 --- /dev/null +++ b/resources/views/components/info-card/header.blade.php @@ -0,0 +1,6 @@ +
+

{{ $heading }}

+ @if($subheading) +

{{ $subheading }}

+ @endif +
diff --git a/resources/views/components/info-card/row.blade.php b/resources/views/components/info-card/row.blade.php new file mode 100644 index 0000000..b951e74 --- /dev/null +++ b/resources/views/components/info-card/row.blade.php @@ -0,0 +1,6 @@ +@props(['row_name' => '']) + +
+
{{ $row_name }}
+
{{ $slot }}
+
diff --git a/resources/views/components/layout/navbar.blade.php b/resources/views/components/layout/navbar.blade.php index 62c2aa2..a2218ce 100644 --- a/resources/views/components/layout/navbar.blade.php +++ b/resources/views/components/layout/navbar.blade.php @@ -91,8 +91,8 @@ Your Profile -{{-- Settings--}} + Your School
@csrf