diff --git a/app/AccessLog.php b/app/AccessLog.php new file mode 100644 index 0000000..84b87cd --- /dev/null +++ b/app/AccessLog.php @@ -0,0 +1,18 @@ +belongsTo('App\Siswa'); + } +} diff --git a/app/Siswa.php b/app/Siswa.php new file mode 100644 index 0000000..45442f2 --- /dev/null +++ b/app/Siswa.php @@ -0,0 +1,11 @@ +bigIncrements('id'); + $table->string('nama'); + $table->string('nisn'); + $table->string('kelas'); + $table->string('tempat_lahir'); + $table->string('tanggal_lahir'); + $table->boolean('lulus'); + $table->timestamps(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('siswas'); + } +} diff --git a/database/migrations/2019_05_09_064211_create_access_logs_table.php b/database/migrations/2019_05_09_064211_create_access_logs_table.php new file mode 100644 index 0000000..e4174b7 --- /dev/null +++ b/database/migrations/2019_05_09_064211_create_access_logs_table.php @@ -0,0 +1,38 @@ +bigIncrements('id'); + $table->unsignedInteger('siswa_id'); + $table->timestamps(); + $table->softDeletes(); + + $table->foreign('siswa_id') + ->on('siswas') + ->onUpdate('cascade') + ->onDelete('restrict'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('access_logs'); + } +} diff --git a/routes/api.php b/routes/api.php index c641ca5..256c316 100644 --- a/routes/api.php +++ b/routes/api.php @@ -13,6 +13,43 @@ use Illuminate\Http\Request; | */ -Route::middleware('auth:api')->get('/user', function (Request $request) { - return $request->user(); +Route::post('/siswa', function (Request $request) { + // Validate user inputs + // Auto redirect on fail + Validator::make($request->all(), [ + 'nama' => 'required|string', + 'nisn' => 'required|integer', + ], [ + 'required' => 'Kolom :attribute harus diisi.', + ])->validate(); + + // Look for the given inputs in the resource + $siswa = App\Siswa::where('nama', $request->nama) + ->where('nisn', $request->nisn) + ->first(); + + // Redirect with error if not found + if (!$siswa || $siswa == null) { + return redirect()->back() + ->withErrors(['siswa' => 'Siswa tidak ditemukan.']) + ->withInput(); + } + + // Write to log + App\AccessLog::create(['siswa_id' => $siswa->id]); + + return $siswa; +}); + +Route::get('/access_log', function () { + // Get the number of unique access + $logs = App\AccessLog()->unique()->count(); + + // Get the total number of available resource + $resources = App\Siswa::count(); + + return response()->json([ + 'accessed' => $logs, + 'total' => $resources, + ]); });