Improving view

This commit is contained in:
Gregorio Chiko Putra
2019-05-15 16:16:21 +07:00
parent dbe517c1fc
commit 5839165531
18 changed files with 2895 additions and 120 deletions

View File

@@ -13,55 +13,16 @@ use Illuminate\Http\Request;
|
*/
Route::post('/siswa', function (Request $request) {
// Validate user inputs
// Auto redirect on fail
Validator::make($request->all(), [
'nisn' => 'required|regex:/^[0-9]+$/',
'tanggalLahir' => 'required|regex:/^[0-9]+$/',
], [
'required' => 'Kolom :attribute harus diisi.',
'regex' => 'Kolom :attribute tidak sesuai.',
])->validate();
Route::post('/siswa', 'SiswaController')->name('siswa.retrieve');
// Look for the given inputs in the resource
try {
$tanggalLahir = Carbon\Carbon::parse($request->tanggalLahir)->format('Y-m-d');
} catch(Exception $e) {
return response()->json([
'message' => 'The given data was invalid.',
'errors' => [
'tanggalLahir' => ['Kolom tanggal lahir tidak sesuai.'],
]
], 422);
}
$siswa = App\Siswa::where('nisn', $request->nisn)
->where('tanggal_lahir', $tanggalLahir)
->first();
Route::post('/hasil_belajar', 'HasilBelajarController')->name('hasilBelajar.retrieve');
// Redirect with error if not found
if (!$siswa || $siswa == null) {
return response()->json(['errors' => ['siswa' => ['Siswa tidak ditemukan.']]], 404);
}
// Write to log
App\AccessLog::create(['siswa_id' => $siswa->id, 'src' => $request->src]);
return $siswa;
});
Route::get('/access_log', function (Request $request) {
// Get the number of unique access
$logs = DB::table('access_logs')->select(DB::raw('count(*) as num where src = ?', $request->src))
->groupBy('siswa_id')
->get()
->count();
// Get the total number of available resource
$resources = App\Siswa::count();
return response()->json([
'accessed' => $logs,
'total' => $resources,
]);
});
Route::get('/access_log', 'AccessLogController')->name('accessLog.fetch');
Route::post('/access_log', function(Request $request) {
return response()->json([], 404);
/*
* Write a new access log
*
*/
App\AccessLog::create(['siswa_id' => $request->siswaId, 'src' => $request->src]);
})->name('accessLog.create');