update controllers logic
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Parents;
|
||||
|
||||
use App\Http\Controllers\Api\BaseApiController;
|
||||
use App\Http\Controllers\Api\Core\BaseApiController;
|
||||
use App\Http\Requests\Parents\ParentAttendanceRequest;
|
||||
use App\Http\Requests\Parents\ParentEnrollmentActionRequest;
|
||||
use App\Http\Requests\Parents\ParentEnrollmentRequest;
|
||||
@@ -41,12 +41,12 @@ class ParentController extends BaseApiController
|
||||
|
||||
public function attendance(ParentAttendanceRequest $request): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$data = $this->attendanceService->listAttendance($parentId, $request->validated()['school_year'] ?? null);
|
||||
$data = $this->attendanceService->listAttendance($guard, $request->validated()['school_year'] ?? null);
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
@@ -58,13 +58,13 @@ class ParentController extends BaseApiController
|
||||
|
||||
public function invoices(ParentInvoiceRequest $request): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$schoolYear = $request->validated()['school_year'] ?? null;
|
||||
$rows = $this->invoiceService->listInvoices($parentId, $schoolYear);
|
||||
$rows = $this->invoiceService->listInvoices($guard, $schoolYear);
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
@@ -74,12 +74,12 @@ class ParentController extends BaseApiController
|
||||
|
||||
public function enrollments(ParentEnrollmentRequest $request): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$data = $this->enrollmentService->overview($parentId, $request->validated()['school_year'] ?? null);
|
||||
$data = $this->enrollmentService->overview($guard, $request->validated()['school_year'] ?? null);
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
@@ -94,14 +94,14 @@ class ParentController extends BaseApiController
|
||||
|
||||
public function updateEnrollments(ParentEnrollmentActionRequest $request): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$payload = $request->validated();
|
||||
$result = $this->enrollmentService->updateEnrollment(
|
||||
$parentId,
|
||||
$guard,
|
||||
$payload['enroll'] ?? [],
|
||||
$payload['withdraw'] ?? []
|
||||
);
|
||||
@@ -115,12 +115,12 @@ class ParentController extends BaseApiController
|
||||
|
||||
public function registration(): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$data = $this->registrationService->overview($parentId);
|
||||
$data = $this->registrationService->overview($guard);
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
@@ -136,14 +136,14 @@ class ParentController extends BaseApiController
|
||||
|
||||
public function storeRegistration(ParentRegistrationRequest $request): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$payload = $request->validated();
|
||||
$result = $this->registrationService->register(
|
||||
$parentId,
|
||||
$guard,
|
||||
$payload['students'] ?? [],
|
||||
$payload['emergency_contacts'] ?? []
|
||||
);
|
||||
@@ -156,36 +156,36 @@ class ParentController extends BaseApiController
|
||||
|
||||
public function updateStudent(ParentStudentUpdateRequest $request, int $studentId): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$this->registrationService->updateStudent($parentId, $studentId, $request->validated());
|
||||
$this->registrationService->updateStudent($guard, $studentId, $request->validated());
|
||||
|
||||
return response()->json(['ok' => true]);
|
||||
}
|
||||
|
||||
public function deleteStudent(int $studentId): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$this->registrationService->deleteStudent($parentId, $studentId);
|
||||
$this->registrationService->deleteStudent($guard, $studentId);
|
||||
|
||||
return response()->json(['ok' => true]);
|
||||
}
|
||||
|
||||
public function emergencyContacts(): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$rows = $this->emergencyContactService->list($parentId);
|
||||
$rows = $this->emergencyContactService->list($guard);
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
@@ -195,12 +195,12 @@ class ParentController extends BaseApiController
|
||||
|
||||
public function storeEmergencyContact(ParentEmergencyContactRequest $request): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$contact = $this->emergencyContactService->store($parentId, $request->validated());
|
||||
$contact = $this->emergencyContactService->store($guard, $request->validated());
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
@@ -210,12 +210,12 @@ class ParentController extends BaseApiController
|
||||
|
||||
public function updateEmergencyContact(ParentEmergencyContactRequest $request, int $contactId): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$contact = $this->emergencyContactService->update($parentId, $contactId, $request->validated());
|
||||
$contact = $this->emergencyContactService->update($guard, $contactId, $request->validated());
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
@@ -225,12 +225,12 @@ class ParentController extends BaseApiController
|
||||
|
||||
public function profile(): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$profile = $this->profileService->getProfile($parentId);
|
||||
$profile = $this->profileService->getProfile($guard);
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
@@ -240,12 +240,12 @@ class ParentController extends BaseApiController
|
||||
|
||||
public function updateProfile(ParentProfileUpdateRequest $request): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$profile = $this->profileService->updateProfile($parentId, $request->validated());
|
||||
$profile = $this->profileService->updateProfile($guard, $request->validated());
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
@@ -255,12 +255,12 @@ class ParentController extends BaseApiController
|
||||
|
||||
public function events(): JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$data = $this->eventService->overview($parentId);
|
||||
$data = $this->eventService->overview($guard);
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
@@ -271,14 +271,27 @@ class ParentController extends BaseApiController
|
||||
}
|
||||
|
||||
public function updateParticipation(ParentEventParticipationRequest $request): JsonResponse
|
||||
{
|
||||
$guard = $this->parentIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$this->eventService->updateParticipation($guard, $request->validated()['participation'] ?? []);
|
||||
|
||||
return response()->json(['ok' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|JsonResponse Authenticated parent user id, or 401 JSON for this controller's legacy `{ ok }` shape.
|
||||
*/
|
||||
private function parentIdOrUnauthorized(): int|JsonResponse
|
||||
{
|
||||
$parentId = (int) (auth()->id() ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
}
|
||||
|
||||
$this->eventService->updateParticipation($parentId, $request->validated()['participation'] ?? []);
|
||||
|
||||
return response()->json(['ok' => true]);
|
||||
return $parentId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user