From 3f780814e2a1f5c895b4d73f42c121098fd9b080 Mon Sep 17 00:00:00 2001 From: Novy San87 Date: Thu, 6 Aug 2026 20:44:31 +0700 Subject: [PATCH] feat: endpoint kelas (wali kelas dari kesiswaan + teman sekelas) + koneksi DB kesiswaan --- .env.example | 7 +++ .../Api/ParentStudentController.php | 45 +++++++++++++++++++ config/database.php | 14 ++++++ routes/api.php | 1 + 4 files changed, 67 insertions(+) diff --git a/.env.example b/.env.example index edca79d..d685e80 100644 --- a/.env.example +++ b/.env.example @@ -42,3 +42,10 @@ DB_EJURNAL_PORT=3306 DB_EJURNAL_DATABASE=e_jurnal DB_EJURNAL_USERNAME=ejurnal DB_EJURNAL_PASSWORD=P4ssw0rd + +# ===== Koneksi database kesiswaan (wali kelas & data siswa) ===== +DB_KESISWAAN_HOST=127.0.0.1 +DB_KESISWAAN_PORT=3306 +DB_KESISWAAN_DATABASE=database_kesiswaan +DB_KESISWAAN_USERNAME=kesiswaan +DB_KESISWAAN_PASSWORD=P4ssw0rd diff --git a/app/Http/Controllers/Api/ParentStudentController.php b/app/Http/Controllers/Api/ParentStudentController.php index e9a31d7..ffb27be 100644 --- a/app/Http/Controllers/Api/ParentStudentController.php +++ b/app/Http/Controllers/Api/ParentStudentController.php @@ -405,6 +405,51 @@ class ParentStudentController extends Controller } } + /** + * Daftar teman sekelas + wali kelas putra/putri. + */ + public function classmates(Request $request, Student $student): JsonResponse + { + $hasLink = ParentStudent::where('user_id', $request->user()->id) + ->where('student_id', $student->id) + ->where('status', 'active') + ->exists(); + + abort_unless($hasLink, 403, 'Putra/putri belum tertaut aktif.'); + + // Wali kelas dari database kesiswaan + $homeroom = null; + try { + $homeroom = DB::connection('kesiswaan') + ->table('classes') + ->where('name', $student->class_name) + ->where('is_active', 1) + ->orderByDesc('id') + ->value('homeroom_teacher'); + } catch (\Throwable $e) { + Log::error('Wali kelas tidak tersedia: '.$e->getMessage()); + } + + // Teman sekelas dari tabel students (aktif) + $students = Student::where('class_name', $student->class_name) + ->where('is_active', true) + ->orderBy('full_name') + ->get(['id', 'full_name', 'gender']) + ->map(fn (Student $s) => [ + 'id' => $s->id, + 'name' => $s->full_name, + 'gender' => $s->gender, // L | P + ]) + ->values(); + + return response()->json([ + 'class_name' => $student->class_name, + 'homeroom_teacher' => $homeroom, + 'students' => $students, + 'total' => $students->count(), + ]); + } + /** * Tautkan anak tambahan (kakak/adik) ke akun wali. */ diff --git a/config/database.php b/config/database.php index 21f7949..5c75d4a 100644 --- a/config/database.php +++ b/config/database.php @@ -78,6 +78,20 @@ return [ 'engine' => null, ], + 'kesiswaan' => [ + 'driver' => 'mysql', + 'host' => env('DB_KESISWAAN_HOST', '127.0.0.1'), + 'port' => env('DB_KESISWAAN_PORT', '3306'), + 'database' => env('DB_KESISWAAN_DATABASE', 'database_kesiswaan'), + 'username' => env('DB_KESISWAAN_USERNAME', 'kesiswaan'), + 'password' => env('DB_KESISWAAN_PASSWORD', ''), + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'prefix' => '', + 'strict' => false, + 'engine' => null, + ], + 'mariadb' => [ 'driver' => 'mariadb', 'url' => env('DB_URL'), diff --git a/routes/api.php b/routes/api.php index a490da8..6005c6f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -40,6 +40,7 @@ Route::middleware('auth:sanctum')->prefix('parent')->group(function () { Route::get('/students/{student}/points-history', [ParentStudentController::class, 'pointsHistory'])->name('api.parent.students.points-history'); Route::get('/students/{student}/violations-stats', [ParentStudentController::class, 'violationsStats'])->name('api.parent.students.violations-stats'); Route::get('/students/{student}/grades', [ParentStudentController::class, 'grades'])->name('api.parent.students.grades'); + Route::get('/students/{student}/classmates', [ParentStudentController::class, 'classmates'])->name('api.parent.students.classmates'); // Notifikasi & perangkat Route::get('/notifications', [ParentNotificationController::class, 'index'])->name('api.parent.notifications');