99 lines
2.6 KiB
PHP
99 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Actions\Students\CreateStudent;
|
|
use App\Http\Requests\StudentStoreRequest;
|
|
use App\Models\Audition;
|
|
use App\Models\Student;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use function abort;
|
|
use function redirect;
|
|
|
|
class StudentController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
if (! Auth::user()->school_id) {
|
|
return redirect()->route('dashboard');
|
|
}
|
|
$students = Auth::user()->students()->withCount('entries')->get();
|
|
$auditions = Audition::all();
|
|
|
|
$shirtSizes = Student::$shirtSizes;
|
|
|
|
return view('students.index',
|
|
['students' => $students, 'auditions' => $auditions, 'shirtSizes' => $shirtSizes]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(StudentStoreRequest $request)
|
|
{
|
|
$creator = app(CreateStudent::class);
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
$creator([
|
|
'first_name' => $request['first_name'],
|
|
'last_name' => $request['last_name'],
|
|
'grade' => $request['grade'],
|
|
'optional_data' => $request->optional_data,
|
|
]);
|
|
|
|
/** @codeCoverageIgnoreEnd */
|
|
return redirect('/students')->with('success', 'Student Created');
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(Request $request, Student $student)
|
|
{
|
|
if ($request->user()->cannot('update', $student)) {
|
|
abort(403);
|
|
}
|
|
|
|
$shirtSizes = Student::$shirtSizes;
|
|
|
|
return view('students.edit', ['student' => $student, 'shirtSizes' => $shirtSizes]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(StudentStoreRequest $request, Student $student)
|
|
{
|
|
if ($request->user()->cannot('update', $student)) {
|
|
abort(403);
|
|
}
|
|
|
|
$student->update([
|
|
'first_name' => $request['first_name'],
|
|
'last_name' => $request['last_name'],
|
|
'grade' => $request['grade'],
|
|
'optional_data' => $request->optional_data,
|
|
]);
|
|
|
|
return redirect('/students')->with('success', 'Student updated successfully.');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Request $request, Student $student)
|
|
{
|
|
if ($request->user()->cannot('delete', $student)) {
|
|
abort(403);
|
|
}
|
|
|
|
$student->delete();
|
|
|
|
return redirect(route('students.index'));
|
|
}
|
|
}
|