From 4c77ade45fd4e1251e6cdeef026fb1b796c166f5 Mon Sep 17 00:00:00 2001 From: Rizky Date: Fri, 6 Jun 2025 23:54:14 +0700 Subject: [PATCH] update : API siswa, devices, absensi siswa --- .../AbsensiSiswaApiController.php} | 38 ++++++++----------- .../Controllers/API/DevicesApiController.php | 19 ++++++++++ .../Controllers/API/SiswaApiController.php | 19 ++++++++++ .../Controllers/AbsensiGuruController.php | 15 ++++++++ .../Controllers/AbsensiSiswaController.php | 15 ++++++++ app/Models/AbsensiGuru.php | 32 ++++++++++++++++ app/Models/AbsensiSiswa.php | 5 ++- app/Models/Device.php | 18 --------- app/Models/Devices.php | 18 +++++++++ ..._02_7_120027_create_absensi_guru_table.php | 2 +- ...2025_05_5_051500_create_devices_table.php} | 7 ++-- ...5_6_115801_create_absensi_siswa_table.php} | 4 +- database/seeders/DatabaseSeeder.php | 2 + database/seeders/DevicesSeeder.php | 29 ++++++++++++++ database/seeders/RuanganSeeder.php | 31 +++++++++++++++ routes/api.php | 12 ++++-- routes/web.php | 16 ++++++-- 17 files changed, 225 insertions(+), 57 deletions(-) rename app/Http/Controllers/{PresensiController.php => API/AbsensiSiswaApiController.php} (50%) create mode 100644 app/Http/Controllers/API/DevicesApiController.php create mode 100644 app/Http/Controllers/API/SiswaApiController.php create mode 100644 app/Http/Controllers/AbsensiGuruController.php create mode 100644 app/Http/Controllers/AbsensiSiswaController.php create mode 100644 app/Models/AbsensiGuru.php delete mode 100644 app/Models/Device.php create mode 100644 app/Models/Devices.php rename database/migrations/{2025_02_5_051500_create_devices_table.php => 2025_05_5_051500_create_devices_table.php} (69%) rename database/migrations/{2025_02_6_115801_create_absensi_siswa_table.php => 2025_05_6_115801_create_absensi_siswa_table.php} (81%) create mode 100644 database/seeders/DevicesSeeder.php create mode 100644 database/seeders/RuanganSeeder.php diff --git a/app/Http/Controllers/PresensiController.php b/app/Http/Controllers/API/AbsensiSiswaApiController.php similarity index 50% rename from app/Http/Controllers/PresensiController.php rename to app/Http/Controllers/API/AbsensiSiswaApiController.php index b615780..323622d 100644 --- a/app/Http/Controllers/PresensiController.php +++ b/app/Http/Controllers/API/AbsensiSiswaApiController.php @@ -1,49 +1,43 @@ validate([ 'id_siswa' => 'required|exists:siswa,id', - 'waktu_absen' => 'required|date_format:Y-m-d H:i:s', + 'devices_id' => 'required|exists:devices,id', + 'waktu' => 'required|date_format:Y-m-d H:i:s', 'status' => 'required|in:hadir,tidak_hadir', - 'device_id' => 'required|exists:devices,id' ]); - $tanggal = date('Y-m-d', strtotime($validated['waktu_absen'])); + $tanggal = date('Y-m-d', strtotime($validated['waktu'])); $sudahAbsen = AbsensiSiswa::where('id_siswa', $validated['id_siswa']) - ->whereDate('waktu_absen', $tanggal) + ->whereDate('waktu', $tanggal) ->first(); - if($sudahAbsen){ + if ($sudahAbsen) { return response()->json([ 'message' => 'Siswa sudah absen pada tanggal ini' - ]); + ], 409); } $absensi = AbsensiSiswa::create([ 'id_siswa' => $validated['id_siswa'], - 'waktu_absen' => $validated['waktu_absen'], + 'waktu' => $validated['waktu'], 'status' => $validated['status'], - 'devices_id' => $validated['device_id'], + 'id_devices' => $validated['devices_id'], ]); return response()->json([ 'message' => 'Absensi berhasil dicatat', 'data' => $absensi - ], 200); + ], 201); } -} \ No newline at end of file +} diff --git a/app/Http/Controllers/API/DevicesApiController.php b/app/Http/Controllers/API/DevicesApiController.php new file mode 100644 index 0000000..3996b7c --- /dev/null +++ b/app/Http/Controllers/API/DevicesApiController.php @@ -0,0 +1,19 @@ +get(); + + return response()->json([ + 'success' => true, + 'data' => $devices + ]); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/API/SiswaApiController.php b/app/Http/Controllers/API/SiswaApiController.php new file mode 100644 index 0000000..311e06c --- /dev/null +++ b/app/Http/Controllers/API/SiswaApiController.php @@ -0,0 +1,19 @@ +get(); + + return response()->json([ + 'success' => true, + 'data' => $siswa + ]); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/AbsensiGuruController.php b/app/Http/Controllers/AbsensiGuruController.php new file mode 100644 index 0000000..474cea1 --- /dev/null +++ b/app/Http/Controllers/AbsensiGuruController.php @@ -0,0 +1,15 @@ +latest()->get(); + return view('admin.presensi.guru', compact('absensi')); + } +} diff --git a/app/Http/Controllers/AbsensiSiswaController.php b/app/Http/Controllers/AbsensiSiswaController.php new file mode 100644 index 0000000..f247fab --- /dev/null +++ b/app/Http/Controllers/AbsensiSiswaController.php @@ -0,0 +1,15 @@ +latest()->get(); + return view('admin.presensi.siswa', compact('absensi')); + } +} diff --git a/app/Models/AbsensiGuru.php b/app/Models/AbsensiGuru.php new file mode 100644 index 0000000..4c6e76e --- /dev/null +++ b/app/Models/AbsensiGuru.php @@ -0,0 +1,32 @@ +belongsTo(Device::class, 'id_devices'); + } + + // Relasi ke tabel siswa + public function siswa() + { + return $this->belongsTo(Siswa::class, 'id_guru'); + } +} diff --git a/app/Models/AbsensiSiswa.php b/app/Models/AbsensiSiswa.php index e5cfb4c..a04fa18 100644 --- a/app/Models/AbsensiSiswa.php +++ b/app/Models/AbsensiSiswa.php @@ -13,14 +13,15 @@ class AbsensiSiswa extends Model protected $fillable = [ 'id_siswa', - 'waktu_absen', + 'id_devices', + 'waktu', 'status', ]; //Relasi ke tabel devices public function devices() { - return $this->belongsTo(Device::class, 'devices_id'); + return $this->belongsTo(Device::class, 'id_devices'); } // Relasi ke tabel siswa diff --git a/app/Models/Device.php b/app/Models/Device.php deleted file mode 100644 index 832133d..0000000 --- a/app/Models/Device.php +++ /dev/null @@ -1,18 +0,0 @@ -belongsTo(Kelas::class); - } - - public function absensi(){ - return $this->hasMany(AbsensiSiswa::class, 'device_id'); - } -} diff --git a/app/Models/Devices.php b/app/Models/Devices.php new file mode 100644 index 0000000..bdbc616 --- /dev/null +++ b/app/Models/Devices.php @@ -0,0 +1,18 @@ +belongsTo(Kelas::class); + } + + public function ruangan(){ + return $this->belongsTo(Ruangan::class); + } +} diff --git a/database/migrations/2025_02_7_120027_create_absensi_guru_table.php b/database/migrations/2025_02_7_120027_create_absensi_guru_table.php index 99d07c2..ae23896 100644 --- a/database/migrations/2025_02_7_120027_create_absensi_guru_table.php +++ b/database/migrations/2025_02_7_120027_create_absensi_guru_table.php @@ -14,7 +14,7 @@ public function up(): void Schema::create('absensi_guru', function (Blueprint $table) { $table->id(); $table->foreignId('id_guru')->constrained('guru')->onDelete('cascade'); - $table->date('tanggal'); + $table->date('waktu'); $table->string('status'); $table->timestamps(); }); diff --git a/database/migrations/2025_02_5_051500_create_devices_table.php b/database/migrations/2025_05_5_051500_create_devices_table.php similarity index 69% rename from database/migrations/2025_02_5_051500_create_devices_table.php rename to database/migrations/2025_05_5_051500_create_devices_table.php index 886fe1f..df0371b 100644 --- a/database/migrations/2025_02_5_051500_create_devices_table.php +++ b/database/migrations/2025_05_5_051500_create_devices_table.php @@ -13,11 +13,10 @@ public function up(): void { Schema::create('devices', function (Blueprint $table) { $table->id(); - $table->string('devices_id')->unique(); - $table->unsignedBigInteger('kelas_id'); + $table->string('nama_device')->unique(); + $table->foreignId('id_kelas')->constrained('kelas')->onDelete('cascade'); + $table->foreignId('id_ruangan')->constrained('ruangan')->onDelete('cascade'); $table->timestamps(); - - $table->foreign('kelas_id')->references('id')->on('kelas')->onDelete('cascade'); }); } diff --git a/database/migrations/2025_02_6_115801_create_absensi_siswa_table.php b/database/migrations/2025_05_6_115801_create_absensi_siswa_table.php similarity index 81% rename from database/migrations/2025_02_6_115801_create_absensi_siswa_table.php rename to database/migrations/2025_05_6_115801_create_absensi_siswa_table.php index 129752f..44dd2b8 100644 --- a/database/migrations/2025_02_6_115801_create_absensi_siswa_table.php +++ b/database/migrations/2025_05_6_115801_create_absensi_siswa_table.php @@ -14,11 +14,11 @@ public function up(): void Schema::create('absensi_siswa', function (Blueprint $table) { $table->id(); $table->foreignId('id_siswa')->constrained('siswa')->onDelete('cascade'); - $table->timestamp('waktu_absen'); + $table->foreignId('id_devices')->nullable()->constrained('devices')->onDelete('set null'); + $table->timestamp('waktu'); $table->string('status'); $table->timestamps(); - $table->foreignId('device_id')->nullable()->constrained('devices')->onDelete('set null'); }); } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 92bc9d6..bfd84f4 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -17,6 +17,8 @@ public function run(): void UserTableSeeder::class, JurusanSeeder::class, KelasSeeder::class, + RuanganSeeder::class, + DevicesSeeder::class, SiswaTableSeeder::class, StatusSeeder::class, GuruSeeder::class diff --git a/database/seeders/DevicesSeeder.php b/database/seeders/DevicesSeeder.php new file mode 100644 index 0000000..6d8a0e7 --- /dev/null +++ b/database/seeders/DevicesSeeder.php @@ -0,0 +1,29 @@ + 'R_Lab_TKK', + 'id_kelas' => 1, // Referencing Kelas ID + 'id_ruangan' => 1, // Referencing Ruangan ID + ], + [ + 'nama_device' => 'R_Lab_TIF', + 'id_kelas' => 2, // Referencing Kelas ID + 'id_ruangan' => 2, // Referencing Ruangan ID + ], + ]); + } +} diff --git a/database/seeders/RuanganSeeder.php b/database/seeders/RuanganSeeder.php new file mode 100644 index 0000000..8e3b8b4 --- /dev/null +++ b/database/seeders/RuanganSeeder.php @@ -0,0 +1,31 @@ + 'Lab_TKK', + 'id_kelas' => 1, // Referencing Kelas ID + 'id_jurusan' => 1, // Referencing Jurusan ID + 'relay_state' => 'off', + ], + [ + 'nama_ruangan' => 'Lab_TIF', + 'id_kelas' => 2, // Referencing Kelas ID + 'id_jurusan' => 2, // Referencing Jurusan ID + 'relay_state' => 'off', + ], + ]); + } +} diff --git a/routes/api.php b/routes/api.php index 29686df..cefb39f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -6,6 +6,10 @@ use App\Http\Controllers\SiswaController; use App\Http\Controllers\API\BellController; use App\Http\Controllers\AnnouncementController; +use App\Http\Controllers\Api\DevicesApiController; +use App\Http\Controllers\Api\SiswaApiController; +use App\Http\Controllers\Api\AbsensiSiswaApiController; + Route::prefix('bel')->group(function () { Route::post('/ring', [BelController::class, 'ring'])->name('api.bel.ring'); @@ -42,8 +46,8 @@ }); #Presensi -Route::post('/presensi', [PresensiController::class, 'store']); -Route::prefix('admin')->group(function () { - Route::apiResource('/siswa', SiswaController::class)->only(['index', 'store']); -}); \ No newline at end of file +Route::get('/siswa', [SiswaApiController::class, 'index']); +Route::get('/devices', [DevicesApiController::class, 'index']); + +Route::post('/absensi-siswa', [AbsensiSiswaApiController::class, 'store']); \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 1702654..c893a96 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,7 @@ name('index'); @@ -87,10 +91,14 @@ 'update' => 'admin.ruangan.update', 'destroy' => 'admin.ruangan.destroy', ]); - // Presensi - Route::controller(PresensiController::class)->group(function () { - Route::get('/presensi/siswa', 'indexSiswa')->name('admin.presensi.siswa'); - Route::get('/presensi/guru', 'indexGuru')->name('admin.presensi.guru'); + // Presensi Siswa + Route::controller(AbsensiSiswaController::class)->group(function () { + Route::get('/presensi/siswa', 'index')->name('admin.presensi.siswa'); + }); + + // Presensi Guru + Route::controller(AbsensiGuruController::class)->group(function () { + Route::get('/presensi/guru', 'index')->name('admin.presensi.guru'); }); // Laporan