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
+
{{ $subheading }}
+ @endif +