diff --git a/app/AccessLog.php b/app/AccessLog.php index 84b87cd..3dc7aa9 100644 --- a/app/AccessLog.php +++ b/app/AccessLog.php @@ -9,8 +9,16 @@ class AccessLog extends Model { use SoftDeletes; + protected $fillable = [ + 'siswa_id', + ]; + protected $with = [ 'siswa' ]; + protected $date = [ + 'created_at', 'updated_at', 'deleted_at', + ]; + public function siswa() { return $this->belongsTo('App\Siswa'); diff --git a/app/Siswa.php b/app/Siswa.php index 45442f2..ebc4fe6 100644 --- a/app/Siswa.php +++ b/app/Siswa.php @@ -8,4 +8,17 @@ use Illuminate\Database\Eloquent\SoftDeletes; class Siswa extends Model { use SoftDeletes; + + protected $fillable = [ + 'nama', 'nisn', 'kelas', 'tempat_lahir', 'tanggal_lahir', 'lulus', + ]; + + protected $dates = [ + 'created_at', 'updated_at', 'deleted_at', + ]; + + protected $casts = [ + 'tanggal_lahir' => 'datetime:Y-m-d', + 'lulus' => 'boolean', + ]; } diff --git a/database/migrations/2019_05_09_062832_create_siswas_table.php b/database/migrations/2019_05_09_062832_create_siswas_table.php index f4021c7..4ef1e89 100644 --- a/database/migrations/2019_05_09_062832_create_siswas_table.php +++ b/database/migrations/2019_05_09_062832_create_siswas_table.php @@ -19,7 +19,7 @@ class CreateSiswasTable extends Migration $table->string('nisn'); $table->string('kelas'); $table->string('tempat_lahir'); - $table->string('tanggal_lahir'); + $table->date('tanggal_lahir'); $table->boolean('lulus'); $table->timestamps(); $table->softDeletes(); 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 index e4174b7..109ee87 100644 --- a/database/migrations/2019_05_09_064211_create_access_logs_table.php +++ b/database/migrations/2019_05_09_064211_create_access_logs_table.php @@ -15,11 +15,12 @@ class CreateAccessLogsTable extends Migration { Schema::create('access_logs', function (Blueprint $table) { $table->bigIncrements('id'); - $table->unsignedInteger('siswa_id'); + $table->unsignedBigInteger('siswa_id'); $table->timestamps(); $table->softDeletes(); $table->foreign('siswa_id') + ->references('id') ->on('siswas') ->onUpdate('cascade') ->onDelete('restrict'); diff --git a/routes/api.php b/routes/api.php index 256c316..c0f7681 100644 --- a/routes/api.php +++ b/routes/api.php @@ -30,9 +30,7 @@ Route::post('/siswa', function (Request $request) { // Redirect with error if not found if (!$siswa || $siswa == null) { - return redirect()->back() - ->withErrors(['siswa' => 'Siswa tidak ditemukan.']) - ->withInput(); + return response()->json(['siswa' => 'Siswa tidak ditemukan.'], 404); } // Write to log diff --git a/tests/Feature/ApplicationTest.php b/tests/Feature/ApplicationTest.php new file mode 100644 index 0000000..2501783 --- /dev/null +++ b/tests/Feature/ApplicationTest.php @@ -0,0 +1,81 @@ +json('POST', '/api/siswa', ['nama' => 'Sally', 'nisn' => '1234567890']); + + $response + ->assertStatus(404) + ->assertJson([ + 'siswa' => 'Siswa tidak ditemukan.' + ]); + } + + public function testCariDataSiswaTidakDiisiGagal() + { + $response = $this->json('POST', '/api/siswa', ['nama' => '', 'nisn' => '']); + + $response + ->assertStatus(422) + ->assertJson([ + 'message' => 'The given data was invalid.', + 'errors' => [ + 'nama' => ['Kolom nama harus diisi.'], + 'nisn' => ['Kolom nisn harus diisi.'], + ], + ]); + } + + public function testCariDataSiswaBenarBerhasil() + { + $siswa = \App\Siswa::create([ + 'nama' => 'Sally', + 'nisn' => '1234567890', + 'kelas' => 'xii mm 1', + 'tempat_lahir' => $this->faker->city(), + 'tanggal_lahir' => $this->faker->dateTime('now', 'Asia/Jakarta'), + 'lulus' => true, + ]); + + $response = $this->json('POST', '/api/siswa', ['nama' => 'Sally', 'nisn' => '1234567890']); + + $response + ->assertStatus(200) + ->assertJson([ + 'nama' => $siswa->nama, + 'nisn' => $siswa->nisn, + 'kelas' => $siswa->kelas, + 'tempat_lahir' => $siswa->tempat_lahir, + 'tanggal_lahir' => $siswa->tanggal_lahir->toDateString(), + 'lulus' => true, + ]); + } + + public function testCariDataSiswaBenarMasukLog() + { + $siswa = \App\Siswa::create([ + 'nama' => 'Sally', + 'nisn' => '1234567890', + 'kelas' => 'xii mm 1', + 'tempat_lahir' => $this->faker->city(), + 'tanggal_lahir' => $this->faker->dateTime('now', 'Asia/Jakarta'), + 'lulus' => true, + ]); + + $response = $this->json('POST', '/api/siswa', ['nama' => 'Sally', 'nisn' => '1234567890']); + + $this->assertDatabaseHas('access_logs', [ + 'siswa_id' => $siswa->id, + ]); + } +} diff --git a/tests/Feature/SiswaTest.php b/tests/Feature/SiswaTest.php new file mode 100644 index 0000000..6f126d3 --- /dev/null +++ b/tests/Feature/SiswaTest.php @@ -0,0 +1,48 @@ +json('POST', '/api/siswa', ['nama' => 'Sally', 'nisn' => '1234567890']); + + $response + ->assertStatus(404) + ->assertJson([ + 'siswa' => 'Siswa tidak ditemukan.' + ]); + } + + public function testCariDataSiswaBerhasil() + { + $siswa = \App\Siswa::create([ + 'nama' => 'Sally', + 'nisn' => '1234567890', + 'kelas' => 'xii mm 1', + 'tempat_lahir' => $this->faker->city(), + 'tanggal_lahir' => $this->faker->dateTime('now', 'Asia/Jakarta'), + 'lulus' => true, + ]); + + $response = $this->json('POST', '/api/siswa', ['nama' => 'Sally', 'nisn' => '1234567890']); + + $response + ->assertStatus(200) + ->assertJson([ + 'nama' => $siswa->nama, + 'nisn' => $siswa->nisn, + 'kelas' => $siswa->kelas, + 'tempat_lahir' => $siswa->tempat_lahir, + 'tanggal_lahir' => $siswa->tanggal_lahir->toDateString(), + 'lulus' => true, + ]); + } +}