Created temporary API
This commit is contained in:
parent
b1ab965f11
commit
b0553839d0
18
app/AccessLog.php
Normal file
18
app/AccessLog.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
class AccessLog extends Model
|
||||||
|
{
|
||||||
|
use SoftDeletes;
|
||||||
|
|
||||||
|
protected $with = [ 'siswa' ];
|
||||||
|
|
||||||
|
public function siswa()
|
||||||
|
{
|
||||||
|
return $this->belongsTo('App\Siswa');
|
||||||
|
}
|
||||||
|
}
|
11
app/Siswa.php
Normal file
11
app/Siswa.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
class Siswa extends Model
|
||||||
|
{
|
||||||
|
use SoftDeletes;
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
class CreateSiswasTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('siswas', function (Blueprint $table) {
|
||||||
|
$table->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');
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
class CreateAccessLogsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('access_logs', function (Blueprint $table) {
|
||||||
|
$table->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');
|
||||||
|
}
|
||||||
|
}
|
@ -13,6 +13,43 @@ use Illuminate\Http\Request;
|
|||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Route::middleware('auth:api')->get('/user', function (Request $request) {
|
Route::post('/siswa', function (Request $request) {
|
||||||
return $request->user();
|
// 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,
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user