feat: implement student management features including listing, editing, and updating student data

This commit is contained in:
dkecskes
2025-11-02 21:18:09 +01:00
parent 1057a8250c
commit 7954cdd093
8 changed files with 568 additions and 3 deletions

View File

@@ -8,7 +8,8 @@ use Illuminate\Http\Request;
class CompanyController extends Controller
{
public function all_simple() {
public function all_simple()
{
$companies = Company::all()->makeHidden(['created_at', 'updated_at']);
$companies->each(function ($company) {
@@ -18,6 +19,84 @@ class CompanyController extends Controller
return response()->json($companies);
}
/**
* Get a specific company with contact details.
*/
public function get(int $id)
{
$user = auth()->user();
if ($user->role !== 'ADMIN') {
abort(403, 'Unauthorized');
}
$company = Company::find($id);
if (!$company) {
return response()->json([
'message' => 'No such company exists.'
], 400);
}
$company->contact = User::find($company->contact)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
return response()->json($company);
}
/**
* Update company information and contact person.
*/
public function update_all(int $id, Request $request)
{
$user = auth()->user();
if ($user->role !== 'ADMIN') {
abort(403, 'Unauthorized');
}
$company = Company::find($id);
if (!$company) {
return response()->json([
'message' => 'No such company exists.'
], 400);
}
// Validácia dát
$request->validate([
'name' => ['required', 'string', 'max:255'],
'address' => ['required', 'string', 'max:500'],
'ico' => ['required', 'integer'],
'hiring' => ['required', 'boolean'],
'contact.name' => ['required', 'string', 'max:255'],
'contact.email' => ['required', 'email', 'max:255', 'unique:users,email,' . $company->contact],
'contact.phone' => ['nullable', 'string', 'max:20'],
]);
// Aktualizácia Company údajov
$company->update([
'name' => $request->name,
'address' => $request->address,
'ico' => $request->ico,
'hiring' => $request->hiring,
]);
// Aktualizácia kontaktnej osoby
if ($request->has('contact')) {
$contactPerson = User::find($company->contact);
if ($contactPerson) {
$contactPerson->update([
'name' => $request->contact['name'],
'email' => $request->contact['email'],
'phone' => $request->contact['phone'] ?? null,
]);
}
}
return response()->noContent();
}
/**
* Display a listing of the resource.
*/

View File

@@ -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.
*/

View File

@@ -49,4 +49,12 @@ class User extends Authenticatable
'password' => 'hashed',
];
}
/**
* Get the student data associated with the user.
*/
public function studentData()
{
return $this->hasOne(StudentData::class, 'user_id');
}
}

View File

@@ -3,6 +3,7 @@
use App\Http\Controllers\Auth\RegisteredUserController;
use App\Http\Controllers\CompanyController;
use App\Http\Controllers\InternshipController;
use App\Http\Controllers\StudentDataController;
use App\Models\Company;
use App\Models\StudentData;
use Illuminate\Http\Request;
@@ -20,6 +21,12 @@ Route::middleware(['auth:sanctum'])->get('/user', function (Request $request) {
return $user;
});
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::post('/password-reset', [RegisteredUserController::class, 'reset_password'])
->middleware(['guest', 'throttle:6,1'])
->name('password.reset');
@@ -34,4 +41,6 @@ Route::prefix('/internships')->group(function () {
Route::prefix('/companies')->middleware("auth:sanctum")->group(function () {
Route::get("/simple", [CompanyController::class, 'all_simple']);
Route::get("/{id}", [CompanyController::class, 'get']);
Route::post("/{id}", [CompanyController::class, 'update_all']);
});

View File

@@ -1,3 +1,135 @@
<script setup lang="ts">
import type { CompanyData } from '~/types/company_data';
import { FetchError } from 'ofetch';
definePageMeta({
middleware: ['sanctum:auth', 'admin-only']
})
const route = useRoute();
const client = useSanctumClient();
const companyId = route.params.id;
const loading = ref(true);
const saving = ref(false);
const form = ref({
name: '',
address: '',
ico: 0,
hiring: false,
contact: {
name: '',
email: '',
phone: ''
}
});
// Načítanie dát firmy
const { data } = await useSanctumFetch<CompanyData>(`/api/companies/${companyId}`);
watch(data, (newData) => {
if (newData) {
form.value.name = newData.name;
form.value.address = newData.address;
form.value.ico = newData.ico;
form.value.hiring = !!newData.hiring;
form.value.contact.name = newData.contact?.name;
form.value.contact.email = newData.contact?.email;
form.value.contact.phone = newData.contact?.phone;
loading.value = false;
}
}, { immediate: true });
// Uloženie zmien
async function saveChanges() {
saving.value = true;
try {
await client(`/api/companies/${companyId}`, {
method: 'POST',
body: form.value
});
alert('Údaje firmy boli úspešne aktualizované');
navigateTo("/dashboard/admin/companies");
} catch (e) {
if (e instanceof FetchError) {
console.error('Error saving company:', e.response?._data.message);
alert('Chyba:\n' + e.response?._data.message);
}
} finally {
saving.value = false;
}
}
function cancel() {
navigateTo('/dashboard/admin/companies');
}
</script>
<template>
<p>...</p>
</template>
<v-container class="h-100">
<div v-if="loading" class="text-center">
<v-progress-circular indeterminate color="primary"></v-progress-circular>
</div>
<div v-else>
<v-row class="mb-4">
<v-col>
<h1>Editovať firmu</h1>
</v-col>
</v-row>
<v-row>
<v-col cols="12" md="8">
<v-card>
<v-card-title>Údaje firmy</v-card-title>
<v-card-text>
<v-form>
<v-text-field v-model="form.name" label="Názov firmy" required variant="outlined"
class="mb-3"></v-text-field>
<v-textarea v-model="form.address" label="Adresa" required variant="outlined" rows="3"
class="mb-3"></v-textarea>
<v-text-field v-model.number="form.ico" label="IČO" type="number" required
variant="outlined" class="mb-3"></v-text-field>
<v-checkbox v-model="form.hiring" label="Prijíma študentov na prax"
class="mb-3"></v-checkbox>
<v-divider class="my-4"></v-divider>
<h3 class="mb-3">Kontaktná osoba</h3>
<v-text-field v-model="form.contact.name" label="Meno a priezvisko" required
variant="outlined" class="mb-3"></v-text-field>
<v-text-field v-model="form.contact.email" label="E-mail" type="email" required
variant="outlined" class="mb-3"></v-text-field>
<v-text-field v-model="form.contact.phone" label="Telefón"
variant="outlined"></v-text-field>
</v-form>
</v-card-text>
<v-card-actions class="px-6 pb-4">
<v-btn color="primary" @click="saveChanges" :loading="saving" :disabled="saving">
Uložiť zmeny
</v-btn>
<v-btn @click="cancel" :disabled="saving">
Zrušiť
</v-btn>
</v-card-actions>
</v-card>
</v-col>
</v-row>
</div>
</v-container>
</template>
<style scoped>
.h-100 {
min-height: 100vh;
}
</style>

View File

@@ -23,6 +23,9 @@ const user = useSanctumUser<User>();
<!-- spacer -->
<div style="height: 40px;"></div>
<v-btn prepend-icon="mdi-account-school" color="blue" class="mr-2" to="/dashboard/admin/students">
Študenti
</v-btn>
<v-btn prepend-icon="mdi-domain" color="blue" class="mr-2" to="/dashboard/admin/companies">
Firmy
</v-btn>

View File

@@ -0,0 +1,137 @@
<script setup lang="ts">
import type { User } from '~/types/user';
import { FetchError } from 'ofetch';
definePageMeta({
middleware: ['sanctum:auth', 'admin-only']
})
const route = useRoute();
const client = useSanctumClient();
const studentId = route.params.id;
const student = ref<User | null>(null);
const loading = ref(true);
const saving = ref(false);
const form = ref({
name: '',
email: '',
phone: '',
student_data: {
study_field: '',
personal_email: '',
address: ''
}
});
const { data } = await useSanctumFetch<User>(`/api/students/${studentId}`);
// Načítanie dát študenta
watch(data, (newData) => {
if (newData) {
student.value = newData;
form.value = {
name: newData.name || '',
email: newData.email || '',
phone: newData.phone || '',
student_data: {
study_field: newData.student_data?.study_field || '',
personal_email: newData.student_data?.personal_email || '',
address: newData.student_data?.address || ''
}
};
}
navigateTo('/dashboard/admin/students');
}
);
// Uloženie zmien
async function saveChanges() {
saving.value = true;
try {
await client(`/api/students/${studentId}`, {
method: 'POST',
body: form.value
});
alert('Údaje študenta boli úspešne aktualizované');
navigateTo("/dashboard/admin/students");
} catch (e) {
if (e instanceof FetchError) {
console.error('Error saving student:', e.response?._data.message);
alert('Chyba:\n' + e.response?._data.message);
}
} finally {
saving.value = false;
}
}
function cancel() {
navigateTo('/dashboard/admin/students');
}
</script>
<template>
<v-container class="h-100">
<div v-if="loading" class="text-center">
<v-progress-circular indeterminate color="primary"></v-progress-circular>
</div>
<div v-else>
<v-row class="mb-4">
<v-col>
<h1>Editovať študenta</h1>
</v-col>
</v-row>
<v-row>
<v-col cols="12" md="8">
<v-card>
<v-card-title>Základné údaje</v-card-title>
<v-card-text>
<v-form>
<v-text-field v-model="form.name" label="Meno a priezvisko" required variant="outlined"
class="mb-3"></v-text-field>
<v-text-field v-model="form.email" label="E-mail (prihlasovací)" type="email" required
variant="outlined" class="mb-3"></v-text-field>
<v-text-field v-model="form.phone" label="Telefón" variant="outlined"
class="mb-3"></v-text-field>
<v-divider class="my-4"></v-divider>
<h3 class="mb-3">Študijné údaje</h3>
<v-text-field v-model="form.student_data.study_field" label="Študijný program"
variant="outlined" class="mb-3"></v-text-field>
<v-text-field v-model="form.student_data.personal_email" label="Osobný e-mail"
type="email" variant="outlined" class="mb-3"></v-text-field>
<v-textarea v-model="form.student_data.address" label="Adresa" variant="outlined"
rows="3"></v-textarea>
</v-form>
</v-card-text>
<v-card-actions class="px-6 pb-4">
<v-btn color="primary" @click="saveChanges" :loading="saving" :disabled="saving">
Uložiť zmeny
</v-btn>
<v-btn @click="cancel" :disabled="saving">
Zrušiť
</v-btn>
</v-card-actions>
</v-card>
</v-col>
</v-row>
</div>
</v-container>
</template>
<style scoped>
.h-100 {
min-height: 100vh;
}
</style>

View File

@@ -0,0 +1,91 @@
<script setup lang="ts">
import type { User } from '~/types/user';
definePageMeta({
middleware: ['sanctum:auth', 'admin-only'],
});
useSeoMeta({
title: "Študenti | ISOP",
ogTitle: "Študenti",
description: "Študenti ISOP",
ogDescription: "Študenti",
});
const headers = [
{ title: 'Meno', key: 'name', align: 'left' },
{ title: 'E-mail', key: 'email', align: 'left' },
{ title: 'Telefón', key: 'phone', align: 'left' },
{ title: 'Študijný program', key: 'study_field', align: 'left' },
{ title: 'Osobný e-mail', key: 'personal_email', align: 'middle' },
{ title: 'Adresa', key: 'address', align: 'middle' },
{ title: 'Operácie', key: 'ops', align: 'middle' },
];
// Načítame všetkých študentov
const { data: students, error } = await useSanctumFetch<User[]>('/api/students');
</script>
<template>
<v-container fluid>
<v-card id="page-container-card">
<h1>Študenti</h1>
<hr />
<!-- spacer -->
<div style="height: 40px;"></div>
<!-- Chybová hláška -->
<v-alert v-if="error" density="compact" :text="error?.message" title="Chyba" type="error"
id="login-error-alert" class="mx-auto alert"></v-alert>
<div v-else>
<p>Aktuálne evidujeme {{ students?.length || 0 }} študentov.</p>
<br />
<v-table v-if="students && students.length > 0">
<thead>
<tr>
<th v-for="header in headers" :class="'text-' + header.align">
<strong>{{ header.title }}</strong>
</th>
</tr>
</thead>
<tbody>
<tr v-for="item in students" :key="item.id">
<td>{{ item.name }}</td>
<td>{{ item.email }}</td>
<td>{{ item.phone || '-' }}</td>
<td>{{ item.student_data?.study_field || '-' }}</td>
<td>{{ item.student_data?.personal_email || '-' }}</td>
<td>{{ item.student_data?.address || '-' }}</td>
<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>
</td>
</tr>
</tbody>
</v-table>
<v-alert v-else density="compact" text="Zatiaľ nie sú zaregistrovaní žiadni študenti." type="info"
class="mt-4"></v-alert>
</div>
</v-card>
</v-container>
</template>
<style scoped>
#page-container-card {
padding-left: 10px;
padding-right: 10px;
}
.op-btn {
margin: 10px;
}
.alert {
margin-top: 10px;
}
</style>