update controllers logic
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Staff;
|
||||
|
||||
use App\Http\Controllers\Api\BaseApiController;
|
||||
use App\Http\Controllers\Api\Core\BaseApiController;
|
||||
use App\Http\Requests\Teachers\TeacherAbsenceSubmitRequest;
|
||||
use App\Http\Requests\Teachers\TeacherClassViewRequest;
|
||||
use App\Http\Requests\Teachers\TeacherSwitchClassRequest;
|
||||
@@ -25,21 +25,21 @@ class TeacherController extends BaseApiController
|
||||
|
||||
public function classes(TeacherClassViewRequest $request): JsonResponse
|
||||
{
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->authenticatedUserIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
try {
|
||||
$data = $this->dashboardService->classView(
|
||||
$userId,
|
||||
$guard,
|
||||
$request->validated()['class_section_id'] ?? null
|
||||
);
|
||||
} catch (\RuntimeException $e) {
|
||||
return response()->json(['ok' => false, 'message' => $e->getMessage()], 403);
|
||||
}
|
||||
|
||||
$teacher = $this->teacherPayload($data['teacher'] ?? null, $userId);
|
||||
$teacher = $this->teacherPayload($data['teacher'] ?? null, $guard);
|
||||
$studentsBySection = [];
|
||||
|
||||
foreach (($data['studentsBySection'] ?? []) as $sectionId => $students) {
|
||||
@@ -59,15 +59,15 @@ class TeacherController extends BaseApiController
|
||||
|
||||
public function switchClass(TeacherSwitchClassRequest $request): JsonResponse
|
||||
{
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->authenticatedUserIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$requested = (int) $request->validated()['class_section_id'];
|
||||
|
||||
try {
|
||||
$data = $this->dashboardService->classView($userId, $requested);
|
||||
$data = $this->dashboardService->classView($guard, $requested);
|
||||
} catch (\RuntimeException $e) {
|
||||
return response()->json(['ok' => false, 'message' => $e->getMessage()], 403);
|
||||
}
|
||||
@@ -87,12 +87,12 @@ class TeacherController extends BaseApiController
|
||||
|
||||
public function absenceForm(): JsonResponse
|
||||
{
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->authenticatedUserIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$data = $this->absenceService->formData($userId);
|
||||
$data = $this->absenceService->formData($guard);
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
@@ -106,12 +106,12 @@ class TeacherController extends BaseApiController
|
||||
|
||||
public function submitAbsence(TeacherAbsenceSubmitRequest $request): JsonResponse
|
||||
{
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->authenticatedUserIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$result = $this->absenceService->submit($userId, $request->validated());
|
||||
$result = $this->absenceService->submit($guard, $request->validated());
|
||||
|
||||
return response()->json([
|
||||
'ok' => (bool) ($result['ok'] ?? false),
|
||||
@@ -140,4 +140,17 @@ class TeacherController extends BaseApiController
|
||||
'role' => User::getUserRoleName($userId),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|JsonResponse
|
||||
*/
|
||||
private function authenticatedUserIdOrUnauthorized(): int|JsonResponse
|
||||
{
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
}
|
||||
|
||||
return $userId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user