You've already forked isop-mirror
feat: implement student management features including listing, editing, and updating student data
This commit is contained in:
@@ -3,10 +3,116 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\StudentData;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class StudentDataController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of all students with their data.
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
// Iba admin môže vidieť zoznam študentov
|
||||
$user = auth()->user();
|
||||
|
||||
if ($user->role !== 'ADMIN') {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
$students = User::where('role', 'STUDENT')
|
||||
->with('studentData')
|
||||
->get();
|
||||
|
||||
return response()->json($students);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific student with their data.
|
||||
*/
|
||||
public function get(int $id)
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
$student = User::find($id);
|
||||
|
||||
if (!$student) {
|
||||
return response()->json([
|
||||
'message' => 'No such student exists.'
|
||||
], 400);
|
||||
}
|
||||
|
||||
if ($student->role !== 'STUDENT') {
|
||||
return response()->json([
|
||||
'message' => 'User is not a student.'
|
||||
], 400);
|
||||
}
|
||||
|
||||
if ($user->role !== 'ADMIN') {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
$student->load('studentData');
|
||||
|
||||
return response()->json($student);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update student's basic information and student data.
|
||||
*/
|
||||
public function update_all(int $id, Request $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
$student = User::find($id);
|
||||
|
||||
if (!$student) {
|
||||
return response()->json([
|
||||
'message' => 'No such student exists.'
|
||||
], 400);
|
||||
}
|
||||
|
||||
if ($student->role !== 'STUDENT') {
|
||||
return response()->json([
|
||||
'message' => 'User is not a student.'
|
||||
], 400);
|
||||
}
|
||||
|
||||
if ($user->role !== 'ADMIN') {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
// Validácia dát
|
||||
$request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'email', 'max:255', 'unique:users,email,' . $id],
|
||||
'phone' => ['nullable', 'string', 'max:20'],
|
||||
'student_data.study_field' => ['nullable', 'string', 'max:255'],
|
||||
'student_data.personal_email' => ['nullable', 'email', 'max:255'],
|
||||
'student_data.address' => ['nullable', 'string', 'max:500'],
|
||||
]);
|
||||
|
||||
// Aktualizácia User údajov
|
||||
$student->update([
|
||||
'name' => $request->name,
|
||||
'email' => $request->email,
|
||||
'phone' => $request->phone,
|
||||
]);
|
||||
|
||||
// Aktualizácia alebo vytvorenie StudentData
|
||||
if ($request->has('student_data')) {
|
||||
$studentData = $student->studentData;
|
||||
|
||||
if ($studentData) {
|
||||
$studentData->update($request->student_data);
|
||||
} else {
|
||||
$student->studentData()->create($request->student_data);
|
||||
}
|
||||
}
|
||||
|
||||
return response()->noContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user