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'])
|
||||
|
||||
@@ -14,6 +14,12 @@ const student = ref<User | null>(null);
|
||||
const loading = ref(true);
|
||||
const saving = ref(false);
|
||||
|
||||
// Delete state
|
||||
const deleteDialog = ref(false);
|
||||
const deleteLoading = ref(false);
|
||||
const deleteError = ref<string | null>(null);
|
||||
const deleteSuccess = ref(false);
|
||||
|
||||
const form = ref({
|
||||
name: '',
|
||||
email: '',
|
||||
@@ -70,6 +76,49 @@ async function saveChanges() {
|
||||
function cancel() {
|
||||
navigateTo('/dashboard/admin/students');
|
||||
}
|
||||
|
||||
// Funkcia na otvorenie delete dialogu
|
||||
const openDeleteDialog = () => {
|
||||
deleteDialog.value = true;
|
||||
deleteError.value = null;
|
||||
};
|
||||
|
||||
// Funkcia na zatvorenie dialogu
|
||||
const closeDeleteDialog = () => {
|
||||
deleteDialog.value = false;
|
||||
deleteError.value = null;
|
||||
deleteSuccess.value = false;
|
||||
};
|
||||
|
||||
// Funkcia na vymazanie študenta
|
||||
const deleteStudent = async () => {
|
||||
if (!studentId) return;
|
||||
|
||||
deleteLoading.value = true;
|
||||
deleteError.value = null;
|
||||
|
||||
try {
|
||||
await client(`/api/students/${studentId}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
deleteSuccess.value = true;
|
||||
|
||||
// Presmeruj na zoznam po 1.5 sekundách
|
||||
setTimeout(() => {
|
||||
navigateTo('/dashboard/admin/students');
|
||||
}, 1500);
|
||||
|
||||
} catch (e) {
|
||||
if (e instanceof FetchError) {
|
||||
deleteError.value = e.response?._data?.message || 'Chyba pri mazaní študenta.';
|
||||
} else {
|
||||
deleteError.value = 'Neznáma chyba pri mazaní študenta.';
|
||||
}
|
||||
} finally {
|
||||
deleteLoading.value = false;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -122,11 +171,53 @@ function cancel() {
|
||||
<v-btn @click="cancel" :disabled="saving">
|
||||
Zrušiť
|
||||
</v-btn>
|
||||
<v-spacer></v-spacer>
|
||||
<v-btn color="red" variant="outlined" @click="openDeleteDialog" :disabled="saving">
|
||||
Vymazať študenta
|
||||
</v-btn>
|
||||
</v-card-actions>
|
||||
</v-card>
|
||||
</v-col>
|
||||
</v-row>
|
||||
</div>
|
||||
|
||||
<!-- Delete Confirmation Dialog -->
|
||||
<v-dialog v-model="deleteDialog" max-width="500px">
|
||||
<v-card>
|
||||
<v-card-title class="text-h5">
|
||||
Potvrdiť vymazanie
|
||||
</v-card-title>
|
||||
<v-card-text>
|
||||
<p v-if="!deleteSuccess">
|
||||
Naozaj chcete vymazať študenta <strong>{{ student?.name }}</strong>?
|
||||
</p>
|
||||
<p v-if="!deleteSuccess" class="text-error mt-2">
|
||||
Táto akcia vymaže aj všetky súvisiace dáta (praxe, statusy, atď.) a <strong>nie je možné ju
|
||||
vrátiť späť</strong>.
|
||||
</p>
|
||||
|
||||
<!-- Error message -->
|
||||
<v-alert v-if="deleteError" type="error" density="compact" class="mt-3">
|
||||
{{ deleteError }}
|
||||
</v-alert>
|
||||
|
||||
<!-- Success message -->
|
||||
<v-alert v-if="deleteSuccess" type="success" density="compact" class="mt-3">
|
||||
Študent bol úspešne vymazaný. Presmerovanie...
|
||||
</v-alert>
|
||||
</v-card-text>
|
||||
<v-card-actions>
|
||||
<v-spacer></v-spacer>
|
||||
<v-btn color="grey" variant="text" @click="closeDeleteDialog" :disabled="deleteLoading">
|
||||
Zrušiť
|
||||
</v-btn>
|
||||
<v-btn color="red" variant="text" @click="deleteStudent" :loading="deleteLoading"
|
||||
:disabled="deleteSuccess">
|
||||
Vymazať
|
||||
</v-btn>
|
||||
</v-card-actions>
|
||||
</v-card>
|
||||
</v-dialog>
|
||||
</v-container>
|
||||
</template>
|
||||
|
||||
|
||||
@@ -22,8 +22,66 @@ const headers = [
|
||||
{ title: 'Operácie', key: 'ops', align: 'middle' },
|
||||
];
|
||||
|
||||
const client = useSanctumClient();
|
||||
|
||||
// Načítame všetkých študentov
|
||||
const { data: students, error } = await useSanctumFetch<User[]>('/api/students');
|
||||
|
||||
// State pre delete dialog
|
||||
const deleteDialog = ref(false);
|
||||
const studentToDelete = ref<User | null>(null);
|
||||
const deleteLoading = ref(false);
|
||||
const deleteError = ref<string | null>(null);
|
||||
const deleteSuccess = ref(false);
|
||||
|
||||
// Funkcia na otvorenie delete dialogu
|
||||
const openDeleteDialog = (student: User) => {
|
||||
studentToDelete.value = student;
|
||||
deleteDialog.value = true;
|
||||
deleteError.value = null;
|
||||
};
|
||||
|
||||
// Funkcia na zatvorenie dialogu
|
||||
const closeDeleteDialog = () => {
|
||||
deleteDialog.value = false;
|
||||
studentToDelete.value = null;
|
||||
deleteError.value = null;
|
||||
deleteSuccess.value = false;
|
||||
};
|
||||
|
||||
// Funkcia na vymazanie študenta
|
||||
const deleteStudent = async () => {
|
||||
if (!studentToDelete.value) return;
|
||||
|
||||
deleteLoading.value = true;
|
||||
deleteError.value = null;
|
||||
|
||||
try {
|
||||
await client(`/api/students/${studentToDelete.value.id}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
// Odstránime študenta zo zoznamu
|
||||
if (students.value) {
|
||||
const index = students.value.findIndex(s => s.id === studentToDelete.value!.id);
|
||||
if (index > -1) {
|
||||
students.value.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
deleteSuccess.value = true;
|
||||
|
||||
// Zavri dialog po 1.5 sekundách
|
||||
setTimeout(() => {
|
||||
closeDeleteDialog();
|
||||
}, 1500);
|
||||
|
||||
} catch (err: any) {
|
||||
deleteError.value = err.data?.message || 'Chyba pri mazaní študenta.';
|
||||
} finally {
|
||||
deleteLoading.value = false;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -63,6 +121,8 @@ const { data: students, error } = await useSanctumFetch<User[]>('/api/students')
|
||||
<td class="text-left">
|
||||
<v-btn class="m-1 op-btn" density="compact" append-icon="mdi-pencil" base-color="orange"
|
||||
:to="'/dashboard/admin/students/edit/' + item.id">Editovať</v-btn>
|
||||
<v-btn class="m-1 op-btn" density="compact" append-icon="mdi-delete" base-color="red"
|
||||
@click="openDeleteDialog(item)">Vymazať</v-btn>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
@@ -72,6 +132,44 @@ const { data: students, error } = await useSanctumFetch<User[]>('/api/students')
|
||||
class="mt-4"></v-alert>
|
||||
</div>
|
||||
</v-card>
|
||||
|
||||
<!-- Delete Confirmation Dialog -->
|
||||
<v-dialog v-model="deleteDialog" max-width="500px">
|
||||
<v-card>
|
||||
<v-card-title class="text-h5">
|
||||
Potvrdiť vymazanie
|
||||
</v-card-title>
|
||||
<v-card-text>
|
||||
<p v-if="!deleteSuccess">
|
||||
Naozaj chcete vymazať študenta <strong>{{ studentToDelete?.name }}</strong>?
|
||||
</p>
|
||||
<p v-if="!deleteSuccess" class="text-error mt-2">
|
||||
Táto akcia vymaže aj všetky súvisiace dáta (praxe, statusy, atď.) a <strong>nie je možné ju
|
||||
vrátiť späť</strong>.
|
||||
</p>
|
||||
|
||||
<!-- Error message -->
|
||||
<v-alert v-if="deleteError" type="error" density="compact" class="mt-3">
|
||||
{{ deleteError }}
|
||||
</v-alert>
|
||||
|
||||
<!-- Success message -->
|
||||
<v-alert v-if="deleteSuccess" type="success" density="compact" class="mt-3">
|
||||
Študent bol úspešne vymazaný.
|
||||
</v-alert>
|
||||
</v-card-text>
|
||||
<v-card-actions>
|
||||
<v-spacer></v-spacer>
|
||||
<v-btn color="grey" variant="text" @click="closeDeleteDialog" :disabled="deleteLoading">
|
||||
Zrušiť
|
||||
</v-btn>
|
||||
<v-btn color="red" variant="text" @click="deleteStudent" :loading="deleteLoading"
|
||||
:disabled="deleteSuccess">
|
||||
Vymazať
|
||||
</v-btn>
|
||||
</v-card-actions>
|
||||
</v-card>
|
||||
</v-dialog>
|
||||
</v-container>
|
||||
</template>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user