diff --git a/app/Http/Controllers/EntryController.php b/app/Http/Controllers/EntryController.php index 5ed88ea..cc2c9db 100644 --- a/app/Http/Controllers/EntryController.php +++ b/app/Http/Controllers/EntryController.php @@ -2,6 +2,8 @@ namespace App\Http\Controllers; +use App\Models\Audition; +use App\Models\Entry; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -11,6 +13,25 @@ class EntryController extends Controller public function index() { $entries = Auth::user()->entries()->with(['student','audition'])->get(); - return view('entries.index',['entries' => $entries]); + $auditions = Audition::all(); + $students = Auth::user()->students; + + return view('entries.index',['entries' => $entries, 'students' => $students, 'auditions' => $auditions]); + } + + public function store(Request $request) + { + // TODO write custom rule to verify the combination of student and audition is unique + $request->validate([ + 'student_id' => ['required', 'exists:students,id'], + 'audition_id' => ['required', 'exists:auditions,id'] + ]); + + $entry = Entry::create([ + 'student_id' => request('student_id'), + 'audition_id' => request('audition_id') + ]); + + return redirect('/entries'); } } diff --git a/app/Http/Controllers/StudentController.php b/app/Http/Controllers/StudentController.php index c7d05dd..b745f37 100644 --- a/app/Http/Controllers/StudentController.php +++ b/app/Http/Controllers/StudentController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\Audition; use App\Models\School; use App\Models\Student; use App\Models\User; @@ -20,7 +21,8 @@ class StudentController extends Controller public function index() { $students = Auth::user()->students()->with('entries')->get(); - return view('students.index',['students' => $students]); + $auditions = Audition::all(); + return view('students.index',['students' => $students, 'auditions' => $auditions]); } /** diff --git a/app/Models/Entry.php b/app/Models/Entry.php index a152d60..5c486ff 100644 --- a/app/Models/Entry.php +++ b/app/Models/Entry.php @@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; class Entry extends Model { use HasFactory; + protected $guarded = []; public function student(): BelongsTo { diff --git a/app/Policies/StudentPolicy.php b/app/Policies/StudentPolicy.php index 781ef75..2e0fcf0 100644 --- a/app/Policies/StudentPolicy.php +++ b/app/Policies/StudentPolicy.php @@ -2,6 +2,7 @@ namespace App\Policies; +use App\Models\Entry; use App\Models\Student; use App\Models\User; use Illuminate\Auth\Access\Response; @@ -9,15 +10,6 @@ use function is_null; class StudentPolicy { - // TODO Blanket admin policy is not appropriate for students as it may break things in the audition process - /** - * Grant admin users access to all functions - */ - public function before(User $user, string $ability): bool|null - { - if($user->is_admin) return true; - return null; - } /** * Determine whether the user can view any models. */ @@ -39,6 +31,7 @@ class StudentPolicy */ public function create(User $user): bool { + if($user->is_admin) return true; return ! is_null($user->school_id); } @@ -47,6 +40,8 @@ class StudentPolicy */ public function update(User $user, Student $student): bool { + if (Entry::where('student_id','=',$student->id)->exists()) return false; // Don't allow deletion of a student with entries + if($user->is_admin) return true; return $user->school_id == $student->school_id; } diff --git a/resources/views/components/form/select.blade.php b/resources/views/components/form/select.blade.php new file mode 100644 index 0000000..41c581d --- /dev/null +++ b/resources/views/components/form/select.blade.php @@ -0,0 +1,37 @@ +@props([ + 'label' => false, + 'name', + 'colspan' => '1' +]) +@php + $colspan_classes = [ + '1' => '', + '2' => 'sm:col-span-2', + '3' => 'sm:col-span-3', + '4' => 'sm:col-span-4', + '5' => 'sm:col-span-5', + '6' => 'sm:col-span-6', + '7' => 'sm:col-span-7', + '8' => 'sm:col-span-8', + '9' => 'sm:col-span-9', + '10' => 'sm:col-span-10', + '11' => 'sm:col-span-11', + '12' => 'sm:col-span-12' + ]; + $label_attribs = [ + 'class' => 'block text-sm font-medium leading-6 text-gray-900', + 'for' => $name + ]; + $select_attribs = [ + 'class' => 'mt-2 block w-full rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-900 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-indigo-600 sm:text-sm sm:leading-6', + 'id' => $name, + 'name' => $name +] +@endphp + +