diff --git a/University.Web/app/Http/Controllers/AuthController.php b/University.Web/app/Http/Controllers/AuthController.php index c968b0f..b8803be 100644 --- a/University.Web/app/Http/Controllers/AuthController.php +++ b/University.Web/app/Http/Controllers/AuthController.php @@ -2,8 +2,10 @@ namespace App\Http\Controllers; +use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Session; class AuthController extends Controller { @@ -63,9 +65,13 @@ class AuthController extends Controller ]); if ($response->successful()) { - $token = $response->json()['token']; - session(['api_token' => $token]); - session()->forget('user_id'); + $data = $response->json(); + Session::put([ + 'api_token' => $data['token'], + 'user_id' => $data['user']['id'] ?? null, + 'user_name' => $data['user']['name'] ?? null, + 'user_role' => $data['user']['roles_id'] ?? null, + ]); return redirect()->intended('/dashboard'); } @@ -74,12 +80,15 @@ class AuthController extends Controller ]); } + /** + * @throws ConnectionException + */ public function logout(Request $request) { - Http::withToken(session('api_token')) + $response = Http::withToken(Session::get('api_token')) ->post("{$this->apiBaseUrl}/employee/logout"); $request->session()->invalidate(); - return redirect('/'); + return redirect('/login'); } } diff --git a/University.Web/app/Http/Controllers/DashboardController.php b/University.Web/app/Http/Controllers/DashboardController.php index 1c85bfa..303bbe5 100644 --- a/University.Web/app/Http/Controllers/DashboardController.php +++ b/University.Web/app/Http/Controllers/DashboardController.php @@ -2,12 +2,37 @@ namespace App\Http\Controllers; -use Illuminate\Http\Request; +use App\Services\ApiService; +use Illuminate\Contracts\View\Factory; +use Illuminate\Contracts\View\View; +use Illuminate\Foundation\Application; +use Illuminate\Http\Client\ConnectionException; class DashboardController extends Controller { - public function index() + protected ApiService $api; + + public function __construct(ApiService $api) { - return view('dashboard'); + $this->api = $api; + } + + /** + * @param ApiService $api + * @return Factory|View|Application|\Illuminate\View\View|object + * @throws ConnectionException + */ + public function index(ApiService $api) + { + $response = $api->withAuth()->get('/employee/me'); + $user = $response->json(); + + $statsResponse = $api->withAuth()->get('/employee/statistics'); + $stats = $statsResponse->json(); + + return view('dashboard', [ + 'user' => $user, + 'stats' => $stats + ]); } } diff --git a/University.Web/app/Http/Controllers/StatisticController.php b/University.Web/app/Http/Controllers/StatisticController.php new file mode 100644 index 0000000..b5805cb --- /dev/null +++ b/University.Web/app/Http/Controllers/StatisticController.php @@ -0,0 +1,10 @@ +api = $api; + } + + public function index() + { + $responseStudents = $this->api->get('/employee/students'); + $responseGroups = $this->api->get('/employee/groups'); + + if ($responseStudents->successful() && $responseGroups->successful()) { + $students = $responseStudents->json(); + $groups = $responseGroups->json(); + $groupsList = collect($groups)->pluck('name', 'id')->toArray(); + return view('students.index', compact('students', 'groupsList')); + } + + abort($responseStudents->status()); + } + + public function create() + { + $groups = $this->api->get('/employee/groups')->json(); + return view('students.form', ['groups' => $groups]); + } + + public function store(Request $request) + { + $response = $this->api->post('/employee/students', $request->all()); + + if ($response->successful()) { + return redirect()->route('students.index') + ->with('success', 'Студент успешно создан'); + } + + return back()->withErrors($response->json()['errors'] ?? []); + } + + public function edit($id) + { + $response = $this->api->get("/employee/students/{$id}"); + + if ($response->successful()) { + $student = $response->json(); + $groups = $this->api->get('/employee/groups')->json(); + return view('students.form', [ + 'student' => $student, + 'groups' => $groups, + 'isEdit' => true + ]); + } + + abort($response->status()); + } + + /** + * @throws ConnectionException + */ + public function update(Request $request, $id) + { + $response = $this->api->patch("/employee/students/{$id}", $request->all()); + + if ($response->successful()) { + return redirect()->route('students.index') + ->with('success', 'Данные студента обновлены'); + } + + return back()->withErrors($response->json()['errors'] ?? []); + } + + public function destroy($id) + { + $response = $this->api->delete("/employee/students/{$id}"); + + if ($response->successful()) { + return redirect()->route('students.index') + ->with('success', 'Студент удален'); + } + + return back()->withErrors($response->json()['error'] ?? 'Ошибка при удалении'); + } } diff --git a/University.Web/app/Http/Middleware/CheckJWTToken.php b/University.Web/app/Http/Middleware/CheckJWTToken.php new file mode 100644 index 0000000..1e43af4 --- /dev/null +++ b/University.Web/app/Http/Middleware/CheckJWTToken.php @@ -0,0 +1,31 @@ +route('login'); + } + + $response = Http::withToken(Session::get('api_token')) + ->get(env('API_BASE_URL') . '/employee/me'); + + if ($response->failed()) { + Session::forget('api_token'); + return redirect()->route('login'); + } + + return $next($request); + } +} diff --git a/University.Web/app/Services/ApiService.php b/University.Web/app/Services/ApiService.php new file mode 100644 index 0000000..fa5444e --- /dev/null +++ b/University.Web/app/Services/ApiService.php @@ -0,0 +1,66 @@ +baseUrl = env('API_BASE_URL', 'http://127.0.0.1:8000/api'); + } + + public function withAuth(): PendingRequest + { + return Http::withToken(Session::get('api_token')) + ->baseUrl($this->baseUrl); + } + + /** + * @throws ConnectionException + */ + public function get(string $url, array $params = []): PromiseInterface|Response + { + return $this->withAuth()->get($url, $params); + } + + /** + * @throws ConnectionException + */ + public function post(string $url, array $data = []): PromiseInterface|Response + { + return $this->withAuth()->post($url, $data); + } + + /** + * @throws ConnectionException + */ + public function patch(string $url, array $data = []): PromiseInterface|Response + { + return $this->withAuth()->patch($url, $data); + } + + /** + * @throws ConnectionException + */ + public function put(string $url, array $data = []): PromiseInterface|Response + { + return $this->withAuth()->put($url, $data); + } + + /** + * @throws ConnectionException + */ + public function delete(string $url, array $params = []): PromiseInterface|Response + { + return $this->withAuth()->delete($url, $params); + } +} diff --git a/University.Web/bootstrap/app.php b/University.Web/bootstrap/app.php index 7b162da..289e175 100644 --- a/University.Web/bootstrap/app.php +++ b/University.Web/bootstrap/app.php @@ -1,5 +1,6 @@ withMiddleware(function (Middleware $middleware) { - // + $middleware->alias([ + 'jwt.auth' => CheckJWTToken::class, + ]); }) ->withExceptions(function (Exceptions $exceptions) { // diff --git a/University.Web/resources/views/dashboard.blade.php b/University.Web/resources/views/dashboard.blade.php index b5410b0..43c9a6a 100644 --- a/University.Web/resources/views/dashboard.blade.php +++ b/University.Web/resources/views/dashboard.blade.php @@ -6,112 +6,283 @@
Выберите раздел для работы
+Добро пожаловать, {{ $user['name'] }}!