add Group and StudentProfile, fix controller and service
This commit is contained in:
@@ -53,7 +53,7 @@ class AuthController extends Controller
|
||||
|
||||
$this->authService->generateTwoFactorCode($user);
|
||||
Log::info('2FA code sent to user', ['user_id' => $user->id]);
|
||||
return response()->json(['message' => '2FA code sent to your email', 'user_id' => $user->id], 200);
|
||||
return response()->json(['message' => '2FA code sent to your email', 'user_id' => $user->id]);
|
||||
} catch (JWTException $e) {
|
||||
Log::error('JWT error: ' . $e->getMessage(), ['email' => $data['email']]);
|
||||
return response()->json(['error' => 'Could not create token'], 500);
|
||||
@@ -107,13 +107,56 @@ class AuthController extends Controller
|
||||
|
||||
Log::info('User information fetched successfully', ['user_id' => $user->id]);
|
||||
|
||||
return response()->json($user, 200);
|
||||
return response()->json($user);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to fetch user information: ' . $e->getMessage(), ['user_id' => auth()->id()]);
|
||||
return response()->json(['error' => 'Failed to fetch user information'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function getStudentProfile() {
|
||||
try {
|
||||
$user = auth()->user();
|
||||
|
||||
if (!$user) {
|
||||
Log::warning('User not authenticated');
|
||||
return response()->json(['error' => 'User not authenticated'], 401);
|
||||
}
|
||||
|
||||
$studentProfile = $user->studentProfile;
|
||||
|
||||
if (!$studentProfile) {
|
||||
Log::warning('Student profile not found', ['user_id' => $user->id]);
|
||||
return response()->json(['error' => 'Student profile not found'], 404);
|
||||
}
|
||||
|
||||
$studentProfile->load('group');
|
||||
|
||||
$response = [
|
||||
'student' => [
|
||||
'id' => $user->id,
|
||||
'surname' => $user->surname,
|
||||
'name' => $user->name,
|
||||
'patronymic' => $user->patronymic,
|
||||
'email' => $user->email,
|
||||
'telephone' => $user->telephone,
|
||||
'gender' => $user->gender,
|
||||
'birth_date' => $user->birth_date,
|
||||
'citizenship' => $user->citizenship,
|
||||
'group_name' => $studentProfile->group->name,
|
||||
'matriculation_number' => $studentProfile->matriculation_number,
|
||||
]
|
||||
];
|
||||
|
||||
Log::info('Student profile information fetched successfully', ['user_id' => $user->id]);
|
||||
|
||||
return response()->json($response);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to fetch student profile information: ' . $e->getMessage(), ['user_id' => auth()->id()]);
|
||||
return response()->json(['error' => 'Failed to fetch student profile information'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function logout() {
|
||||
try {
|
||||
JWTAuth::invalidate(JWTAuth::getToken());
|
||||
|
||||
15
University.Server/app/Models/Group.php
Normal file
15
University.Server/app/Models/Group.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Group extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $guarded = false;
|
||||
}
|
||||
20
University.Server/app/Models/StudentProfile.php
Normal file
20
University.Server/app/Models/StudentProfile.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class StudentProfile extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $guarded = false;
|
||||
|
||||
public function group()
|
||||
{
|
||||
return $this->belongsTo(Group::class, 'group_id');
|
||||
}
|
||||
}
|
||||
@@ -61,4 +61,9 @@ class User extends Authenticatable implements JWTSubject
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function studentProfile()
|
||||
{
|
||||
return $this->hasOne(StudentProfile::class, 'user_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,15 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\StudentProfile;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class StudentService
|
||||
{
|
||||
/**
|
||||
* Создание студента.
|
||||
* Создание студента и связанного профиля.
|
||||
*
|
||||
* @param array $data
|
||||
* @return User
|
||||
@@ -16,6 +18,8 @@ class StudentService
|
||||
*/
|
||||
public function createStudent(array $data): User
|
||||
{
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
Log::info('Creating student', ['email' => $data['email']]);
|
||||
|
||||
@@ -34,8 +38,19 @@ class StudentService
|
||||
|
||||
Log::info('Student created successfully', ['user_id' => $user->id, 'email' => $user->email]);
|
||||
|
||||
$studentProfile = StudentProfile::create([
|
||||
'user_id' => $user->id,
|
||||
'group_id' => $data['group_id'],
|
||||
'matriculation_number' => $data['matriculation_number'],
|
||||
]);
|
||||
|
||||
Log::info('Student profile created successfully', ['student_profile_id' => $studentProfile->id]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $user;
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
Log::error('Failed to create student: ' . $e->getMessage(), ['email' => $data['email']]);
|
||||
throw $e;
|
||||
}
|
||||
|
||||
@@ -1,29 +1,44 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\AuthController;
|
||||
use App\Http\Middleware\IsUserAuth;
|
||||
use App\Http\Middleware\LogRequests;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::group(['prefix' => 'students'], function () {
|
||||
Route::post('/register', [AuthController::class, 'registerStudent']);
|
||||
Route::post('/login', [AuthController::class, 'loginStudent']);
|
||||
Route::post('/verify-2fa', [AuthController::class, 'verifyTwoFactorCode']);
|
||||
Route::post('register', [AuthController::class, 'registerStudent']);
|
||||
Route::post('login', [AuthController::class, 'loginStudent']);
|
||||
Route::post('verify-2fa', [AuthController::class, 'verifyTwoFactorCode']);
|
||||
|
||||
Route::middleware('is.user.auth')->group(function () {
|
||||
Route::controller(AuthController::class)->group(function () {
|
||||
Route::post('logout', 'logout');
|
||||
Route::get('me', 'getStudentProfile');
|
||||
});
|
||||
});
|
||||
|
||||
})->middleware('log.requests');
|
||||
|
||||
Route::group(['prefix' => 'teacher'], function () {
|
||||
Route::post('/login', [AuthController::class, 'loginTeacher']);
|
||||
Route::post('/verify-2fa', [AuthController::class, 'verifyTwoFactorCode']);
|
||||
});
|
||||
|
||||
Route::middleware('is.user.auth')->group(function () {
|
||||
Route::controller(AuthController::class)->group(function () {
|
||||
Route::post('/logout', 'logout');
|
||||
Route::get('/me', 'getUser');
|
||||
});
|
||||
});
|
||||
|
||||
})->middleware('log.requests');
|
||||
|
||||
Route::group(['prefix' => 'employee'], function () {
|
||||
Route::post('/login', [AuthController::class, 'loginEmployee']);
|
||||
Route::post('/verify-2fa', [AuthController::class, 'verifyTwoFactorCode']);
|
||||
});
|
||||
|
||||
Route::middleware('is.user.auth')->group(function () {
|
||||
Route::controller(AuthController::class)->group(function () {
|
||||
Route::post('logout', 'logout');
|
||||
Route::get('me', 'getUser');
|
||||
Route::middleware('is.user.auth')->group(function () {
|
||||
Route::controller(AuthController::class)->group(function () {
|
||||
Route::post('/logout', 'logout');
|
||||
Route::get('/me', 'getUser');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
})->middleware('log.requests');
|
||||
|
||||
Reference in New Issue
Block a user