Admin page for students working

This commit is contained in:
Matt Young 2024-06-02 22:26:44 -05:00
parent cfd9775ca4
commit ddcf4a46c4
7 changed files with 132 additions and 2 deletions

View File

@ -3,6 +3,8 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Audition;
use App\Models\School;
use App\Models\Student; use App\Models\Student;
use App\Models\User; use App\Models\User;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -18,4 +20,64 @@ class StudentController extends Controller
$students = Student::orderBy('last_name')->orderBy('first_name')->paginate(15); $students = Student::orderBy('last_name')->orderBy('first_name')->paginate(15);
return view('admin.students.index', ['students' => $students]); return view('admin.students.index', ['students' => $students]);
} }
public function create()
{
if (! Auth::user()->is_admin) abort(403);
$minGrade = Audition::min('minimum_grade');
$maxGrade = Audition::max('maximum_grade');
$schools = School::orderBy('name')->get();
return view('admin.students.create',['schools' => $schools, 'minGrade' => $minGrade, 'maxGrade' => $maxGrade]);
}
public function store()
{
if (! Auth::user()->is_admin) abort(403);
request()->validate([
'first_name' => ['required'],
'last_name' => ['required'],
'grade' => ['required', 'integer'],
'school_id' => ['required', 'exists:schools,id']
]);
$student = Student::create([
'first_name' => request('first_name'),
'last_name' => request('last_name'),
'grade' => request('grade'),
'school_id' => request('school_id')
]);
return redirect('/admin/students');
}
public function edit(Student $student)
{
if (! Auth::user()->is_admin) abort(403);
$minGrade = Audition::min('minimum_grade');
$maxGrade = Audition::max('maximum_grade');
$schools = School::orderBy('name')->get();
return view('admin.students.edit', ['student' => $student, 'schools' => $schools, 'minGrade' => $minGrade, 'maxGrade' => $maxGrade]);
}
public function update(Request $request, Student $student)
{
if (! Auth::user()->is_admin) abort(403);
request()->validate([
'first_name' => ['required'],
'last_name' => ['required'],
'grade' => ['required', 'integer'],
'school_id' => ['required', 'exists:schools,id']
]);
$student->update([
'first_name' => request('first_name'),
'last_name' => request('last_name'),
'grade' => request('grade'),
'school_id' => request('school_id')
]);
return redirect('/admin/students');
// TODO if a students grade is changed, we need to be sure they are still eligible for the auditions in which they are entered.
}
} }

View File

@ -92,6 +92,9 @@ class StudentController extends Controller
'grade' => request('grade') 'grade' => request('grade')
]); ]);
// TODO if a students grade is changed, we need to be sure they are still eligible for the auditions in which they are entered.
return redirect('/students'); return redirect('/students');
} }

View File

@ -0,0 +1,30 @@
<x-layout.app>
<x-card.card class="mx-auto max-w-xl">
<x-card.heading>Create Student</x-card.heading>
<x-form.form method="POST" action="/admin/students">
<x-form.body-grid columns="12">
<x-form.field name="first_name" label_text="First Name" colspan="5" />
<x-form.field name="last_name" label_text="Last Name" colspan="5" />
<x-form.select name="grade" colspan="2">
<x-slot:label>Grade</x-slot:label>
@php($n = $minGrade)
@while($n <= $maxGrade)
<option value="{{ $n }}">{{ $n }}</option>
@php($n++);
@endwhile
</x-form.select>
<x-form.select name="school_id" colspan="12">
<x-slot:label>School</x-slot:label>
@foreach ($schools as $school)
<option value="{{ $school->id }}"> {{ $school->name }}</option>
@endforeach
</x-form.select>
</x-form.body-grid>
<x-form.footer>
<x-form.button>Create Student</x-form.button>
</x-form.footer>
</x-form.form>
</x-card.card>
</x-layout.app>

View File

@ -0,0 +1,30 @@
<x-layout.app>
<x-card.card class="mx-auto max-w-xl">
<x-card.heading>Edit Student</x-card.heading>
<x-form.form method="PATCH" action="/admin/students/{{ $student->id }}">
<x-form.body-grid columns="12">
<x-form.field name="first_name" label_text="First Name" colspan="5" value="{{ $student->first_name }}" />
<x-form.field name="last_name" label_text="Last Name" colspan="5" value="{{ $student->last_name }}" />
<x-form.select name="grade" colspan="2">
<x-slot:label>Grade</x-slot:label>
@php($n = $minGrade)
@while($n <= $maxGrade)
<option value="{{ $n }}" {{ ($n == $student->grade ? 'selected':'') }}>{{ $n }}</option>
@php($n++);
@endwhile
</x-form.select>
<x-form.select name="school_id" colspan="12">
<x-slot:label>School</x-slot:label>
@foreach ($schools as $school)
<option value="{{ $school->id }}" {{ ($school->id == $student->school_id ? 'selected':'') }}> {{ $school->name }}</option>
@endforeach
</x-form.select>
</x-form.body-grid>
<x-form.footer>
<x-form.button>Ecit Student</x-form.button>
</x-form.footer>
</x-form.form>
</x-card.card>
</x-layout.app>

View File

@ -34,3 +34,4 @@
{{ $students->links('vendor.pagination.simple-audition') }} {{ $students->links('vendor.pagination.simple-audition') }}
</div> </div>
</x-layout.app> </x-layout.app>
{{--TODO add options to filter the page--}}

View File

@ -1,6 +1,6 @@
<x-layout.app> <x-layout.app>
<x-card.card class="mx-auto max-w-lg"> <x-card.card class="mx-auto max-w-lg">
<x-card.heading>Edit User</x-card.heading> <x-card.heading>Create User</x-card.heading>
<x-form.form method="POST" action="/admin/users"> <x-form.form method="POST" action="/admin/users">
<x-form.body-grid> <x-form.body-grid>
<x-form.field name="first_name" label_text="First Name" colspan="3" /> <x-form.field name="first_name" label_text="First Name" colspan="3" />
@ -19,7 +19,7 @@
</x-form.select> </x-form.select>
</x-form.body-grid> </x-form.body-grid>
<x-form.footer> <x-form.footer>
<x-form.button>Update User</x-form.button> <x-form.button>Create User</x-form.button>
</x-form.footer> </x-form.footer>
</x-form.form> </x-form.form>
</x-card.card> </x-card.card>

View File

@ -21,6 +21,10 @@ Route::middleware(['auth','verified',CheckIfAdmin::class])->prefix('admin/')->gr
// Admin Student Routes // Admin Student Routes
Route::prefix('students')->controller(\App\Http\Controllers\Admin\StudentController::class)->group(function() { Route::prefix('students')->controller(\App\Http\Controllers\Admin\StudentController::class)->group(function() {
Route::get('/','index'); Route::get('/','index');
Route::get('/create','create');
Route::post('/','store');
Route::get('/{student}/edit','edit');
Route::patch('/{student}','update');
}); });
// Admin School Routes // Admin School Routes