60 lines
1.9 KiB
PHP
60 lines
1.9 KiB
PHP
<?php
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| API Routes
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| Here is where you can register API routes for your application. These
|
|
| routes are loaded by the RouteServiceProvider within a group which
|
|
| is assigned the "api" middleware group. Enjoy building your API!
|
|
|
|
|
*/
|
|
|
|
Route::post('/siswa', function (Request $request) {
|
|
// Validate user inputs
|
|
// Auto redirect on fail
|
|
Validator::make($request->all(), [
|
|
'nisn' => 'required|regex:/^[0-9]+$/',
|
|
'tglLahir' => 'required|regex:/^[0-9]+$/',
|
|
], [
|
|
'required' => 'Kolom :attribute harus diisi.',
|
|
'string' => 'Kolom :attribute tidak sesuai.',
|
|
'regex' => 'Kolom :attribute tidak sesuai.',
|
|
])->validate();
|
|
|
|
// Look for the given inputs in the resource
|
|
$tglLahir = Carbon\Carbon::parse($request->tglLahir);
|
|
$siswa = App\Siswa::where('nisn', $request->nisn)
|
|
->where('tanggal_lahir', $tglLahir)
|
|
->first();
|
|
|
|
// Redirect with error if not found
|
|
if (!$siswa || $siswa == null) {
|
|
return response()->json(['siswa' => 'Siswa tidak ditemukan.'], 404);
|
|
}
|
|
|
|
// Write to log
|
|
App\AccessLog::create(['siswa_id' => $siswa->id]);
|
|
|
|
return $siswa;
|
|
});
|
|
|
|
Route::get('/access_log', function () {
|
|
// Get the number of unique access
|
|
$logs = DB::table('access_logs')->select(DB::raw('count(*) as num'))
|
|
->groupBy('siswa_id')
|
|
->get()
|
|
->count();
|
|
|
|
// Get the total number of available resource
|
|
$resources = App\Siswa::count();
|
|
|
|
return response()->json([
|
|
'accessed' => $logs,
|
|
'total' => $resources,
|
|
]);
|
|
});
|