Files
alrahma_sunday_school_api/app/Http/Controllers/Api/System/SchoolIdController.php
T
2026-03-10 00:48:32 -04:00

65 lines
1.8 KiB
PHP

<?php
namespace App\Http\Controllers\Api\System;
use App\Http\Controllers\Api\BaseApiController;
use App\Http\Requests\SchoolIds\SchoolIdAssignRequest;
use App\Http\Resources\SchoolIds\SchoolIdResource;
use App\Services\SchoolIds\SchoolIdAssignmentService;
use App\Services\SchoolIds\SchoolIdGenerationService;
use Illuminate\Http\JsonResponse;
class SchoolIdController extends BaseApiController
{
public function __construct(
private SchoolIdAssignmentService $assignmentService,
private SchoolIdGenerationService $generationService
) {
parent::__construct();
}
public function assign(SchoolIdAssignRequest $request): JsonResponse
{
$payload = $request->validated();
$schoolId = $this->assignmentService->assignToUser((int) $payload['user_id']);
if ($schoolId === null) {
return response()->json(['ok' => false, 'message' => 'User not found.'], 404);
}
return response()->json([
'ok' => true,
'result' => new SchoolIdResource([
'type' => 'user',
'school_id' => $schoolId,
]),
]);
}
public function generateStudent(): JsonResponse
{
$schoolId = $this->generationService->generateStudentId();
return response()->json([
'ok' => true,
'result' => new SchoolIdResource([
'type' => 'student',
'school_id' => $schoolId,
]),
]);
}
public function generateUser(): JsonResponse
{
$schoolId = $this->generationService->generateUserId();
return response()->json([
'ok' => true,
'result' => new SchoolIdResource([
'type' => 'user',
'school_id' => $schoolId,
]),
]);
}
}