You've already forked isop-mirror
feat: add student deletion functionality with confirmation dialog
This commit is contained in:
@@ -26,7 +26,7 @@ class RegisteredUserController extends Controller
|
||||
$password = bin2hex(random_bytes(16));
|
||||
|
||||
$request->validate([
|
||||
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class],
|
||||
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:' . User::class],
|
||||
'first_name' => ['required', 'string', 'max:64'],
|
||||
'last_name' => ['required', 'string', 'max:64'],
|
||||
'phone' => ['required', 'string', 'max:13'],
|
||||
@@ -56,14 +56,14 @@ class RegisteredUserController extends Controller
|
||||
'password' => Hash::make($password),
|
||||
]);
|
||||
|
||||
if($user->role === "STUDENT") {
|
||||
if ($user->role === "STUDENT") {
|
||||
StudentData::create([
|
||||
'user_id' => $user->id,
|
||||
'address' => $request->student_data['address'],
|
||||
'personal_email' => $request->student_data['personal_email'],
|
||||
'study_field' => $request->student_data['study_field'],
|
||||
]);
|
||||
} else if($user->role === "EMPLOYER") {
|
||||
} else if ($user->role === "EMPLOYER") {
|
||||
Company::create([
|
||||
'name' => $request->company_data['name'],
|
||||
'address' => $request->company_data['address'],
|
||||
@@ -79,7 +79,8 @@ class RegisteredUserController extends Controller
|
||||
return response()->noContent();
|
||||
}
|
||||
|
||||
public function reset_password(Request $request): Response {
|
||||
public function reset_password(Request $request): Response
|
||||
{
|
||||
$request->validate([
|
||||
'email' => ['required', 'string', 'lowercase', 'email', 'max:255'],
|
||||
]);
|
||||
@@ -97,4 +98,22 @@ class RegisteredUserController extends Controller
|
||||
|
||||
return response()->noContent();
|
||||
}
|
||||
|
||||
public function reset_password_2(Request $request): Response
|
||||
{
|
||||
$request->validate([
|
||||
'id' => ['required', 'string', 'lowercase', 'email', 'max:255'],
|
||||
'password' => ['required', 'string', 'lowercase', 'email', 'max:255'],
|
||||
]);
|
||||
|
||||
$user = User::whereEmail($request->email)->first();
|
||||
if (!$user) {
|
||||
return response(status: 400);
|
||||
}
|
||||
|
||||
$user->password = Hash::make($request->password);
|
||||
$user->save();
|
||||
|
||||
return response()->noContent();
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,9 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\StudentData;
|
||||
use App\Models\User;
|
||||
use App\Models\InternshipStatus;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class StudentDataController extends Controller
|
||||
{
|
||||
@@ -168,4 +170,68 @@ class StudentDataController extends Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a student and all related data.
|
||||
*/
|
||||
public function delete(int $id)
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
// Admin kontrola
|
||||
if ($user->role !== 'ADMIN') {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
// 1. Získaj internship IDs
|
||||
$internshipIds = $student->internships()->pluck('id')->toArray();
|
||||
|
||||
// 2. Vymaž internship statuses
|
||||
if (!empty($internshipIds)) {
|
||||
InternshipStatus::whereIn('internship_id', $internshipIds)->delete();
|
||||
}
|
||||
|
||||
// 3. Vymaž internships
|
||||
$student->internships()->delete();
|
||||
|
||||
// 4. Vymaž student_data
|
||||
if ($student->studentData) {
|
||||
$student->studentData()->delete();
|
||||
}
|
||||
|
||||
// 5. Vymaž usera
|
||||
$student->delete();
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Student successfully deleted.'
|
||||
], 200);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Error deleting student.',
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,4 +25,12 @@ class Internship extends Model
|
||||
'position_description',
|
||||
'agreement',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the statuses for the internship.
|
||||
*/
|
||||
public function statuses()
|
||||
{
|
||||
return $this->hasMany(InternshipStatus::class, 'internship_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,4 +57,12 @@ class User extends Authenticatable
|
||||
{
|
||||
return $this->hasOne(StudentData::class, 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the internships for the user.
|
||||
*/
|
||||
public function internships()
|
||||
{
|
||||
return $this->hasMany(Internship::class, 'user_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ Route::middleware(['auth:sanctum'])->prefix('/students')->group(function () {
|
||||
Route::get('/', [StudentDataController::class, 'all']);
|
||||
Route::get('/{id}', [StudentDataController::class, 'get']);
|
||||
Route::post('/{id}', [StudentDataController::class, 'update_all']);
|
||||
Route::delete('/{id}', [StudentDataController::class, 'delete']);
|
||||
});
|
||||
|
||||
Route::post('/password-reset', [RegisteredUserController::class, 'reset_password'])
|
||||
|
||||
Reference in New Issue
Block a user