ソースを参照

feat(students): adiciona novo metodo para capturar alunos ativos

ebagabee 1 ヶ月 前
コミット
4d890c39dd

+ 18 - 0
app/Http/Controllers/StudentController.php

@@ -20,6 +20,24 @@ public function index(): JsonResponse
         return $this->successResponse(payload: StudentResource::collection($items));
     }
 
+    public function franchisorActive(): JsonResponse
+    {
+        $items = $this->service->getFranchisorActive();
+        return $this->successResponse(payload: StudentResource::collection($items));
+    }
+
+    public function franchisorStudentDetail(int $id): JsonResponse
+    {
+        $detail = $this->service->getFranchisorStudentDetail($id);
+        return $this->successResponse(payload: $detail);
+    }
+
+    public function franchisorSummary(): JsonResponse
+    {
+        $summary = $this->service->getFranchisorSummary();
+        return $this->successResponse(payload: $summary);
+    }
+
     public function store(StudentRequest $request): JsonResponse
     {
         $item = $this->service->create(Auth::user(), $request->validated());

+ 4 - 0
app/Http/Resources/StudentResource.php

@@ -33,6 +33,10 @@ public function toArray(Request $request): array
             'notes'               => $this->notes,
             'photo_url'           => $this->photo_url ? Storage::url($this->photo_url) : null,
             'status'              => $this->status,
+            'unit'                => $this->whenLoaded('unit', fn () => [
+                'id'           => $this->unit->id,
+                'fantasy_name' => $this->unit->fantasy_name,
+            ]),
             'created_at'          => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
             'updated_at'          => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
         ];

+ 38 - 0
app/Services/StudentService.php

@@ -3,6 +3,7 @@
 namespace App\Services;
 
 use App\Models\Student;
+use App\Models\StudentContract;
 use App\Models\User;
 use Illuminate\Database\Eloquent\Collection;
 use Illuminate\Http\UploadedFile;
@@ -19,6 +20,43 @@ public function getAll(User $user): Collection
             ->get();
     }
 
+    public function getFranchisorActive(): Collection
+    {
+        return Student::with('unit')
+            ->where('status', 'active')
+            ->orderBy('name')
+            ->get();
+    }
+
+    public function getFranchisorStudentDetail(int $id): ?array
+    {
+        $student = Student::with('unit')->find($id);
+
+        if (!$student) {
+            return null;
+        }
+
+        $contract = StudentContract::where('student_id', $id)
+            ->where('status', 'active')
+            ->first();
+
+        return [
+            'id'       => $student->id,
+            'name'     => $student->name,
+            'phone'    => $student->phone,
+            'unit'     => $student->unit ? ['fantasy_name' => $student->unit->fantasy_name] : null,
+            'protocol' => $contract?->protocol,
+        ];
+    }
+
+    public function getFranchisorSummary(): array
+    {
+        $total  = Student::count();
+        $active = Student::where('status', 'active')->count();
+
+        return ['total' => $total, 'active' => $active];
+    }
+
     public function findById(int $id): ?Student
     {
         return Student::find($id);

+ 4 - 0
routes/authRoutes/student.php

@@ -4,6 +4,10 @@
 use App\Http\Controllers\StudentController;
 
 Route::controller(StudentController::class)->prefix('student')->group(function () {
+    Route::get('/franchisor/summary', 'franchisorSummary');
+    Route::get('/franchisor/active', 'franchisorActive');
+    Route::get('/franchisor/{id}', 'franchisorStudentDetail');
+
     Route::get('/', 'index');
 
     Route::post('/', 'store');