feat(webstatement): tambah migration dan model ProvinceCore
Perubahan yang dilakukan: - Membuat migration untuk tabel province_core dengan field code dan name. - Menambahkan model ProvinceCore dengan beberapa scope dan method helper. - Mengimplementasikan logging untuk semua operasi database yang berkaitan. - Menambahkan dukungan transaction rollback untuk menjaga integritas data. - Membuat seeder untuk data provinsi seluruh Indonesia. - Menambahkan validasi dan method utility untuk keperluan dropdown. - Menggunakan PostgreSQL ILIKE untuk pencarian yang bersifat case-insensitive. - Menambahkan index pada kolom tertentu untuk optimasi performa query. - Mengimplementasikan event model untuk memantau operasi CRUD. - Menyesuaikan struktur file agar sesuai dengan arsitektur Laravel modules. Tujuan perubahan: - Menyediakan data master provinsi yang dapat digunakan secara global. - Memastikan efisiensi dan keamanan data pada proses insert/update. - Mendukung pengembangan fitur yang membutuhkan referensi data provinsi.
This commit is contained in:
161
app/Models/ProvinceCore.php
Normal file
161
app/Models/ProvinceCore.php
Normal file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Webstatement\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class ProvinceCore extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* Nama tabel yang digunakan oleh model
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'province_core';
|
||||
|
||||
/**
|
||||
* Field yang dapat diisi secara mass assignment
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'code',
|
||||
'name',
|
||||
];
|
||||
|
||||
/**
|
||||
* Field yang di-cast ke tipe data tertentu
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
|
||||
/**
|
||||
* Scope untuk mencari berdasarkan kode provinsi
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $code
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeByCode($query, $code)
|
||||
{
|
||||
Log::info('ProvinceCore: Mencari provinsi dengan kode: ' . $code);
|
||||
return $query->where('code', $code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope untuk mencari berdasarkan nama provinsi
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $name
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeByName($query, $name)
|
||||
{
|
||||
Log::info('ProvinceCore: Mencari provinsi dengan nama: ' . $name);
|
||||
return $query->where('name', 'ILIKE', '%' . $name . '%');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope untuk mendapatkan semua provinsi yang aktif
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeActive($query)
|
||||
{
|
||||
Log::info('ProvinceCore: Mengambil semua provinsi aktif');
|
||||
return $query->orderBy('name', 'asc');
|
||||
}
|
||||
|
||||
/**
|
||||
* Mendapatkan provinsi berdasarkan kode
|
||||
*
|
||||
* @param string $code
|
||||
* @return ProvinceCore|null
|
||||
*/
|
||||
public static function getByCode($code)
|
||||
{
|
||||
try {
|
||||
Log::info('ProvinceCore: Mengambil provinsi dengan kode: ' . $code);
|
||||
return self::byCode($code)->first();
|
||||
} catch (\Exception $e) {
|
||||
Log::error('ProvinceCore: Error mengambil provinsi dengan kode ' . $code . ': ' . $e->getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mendapatkan semua provinsi untuk dropdown/select
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public static function getForDropdown()
|
||||
{
|
||||
try {
|
||||
Log::info('ProvinceCore: Mengambil data provinsi untuk dropdown');
|
||||
return self::active()->pluck('name', 'code');
|
||||
} catch (\Exception $e) {
|
||||
Log::error('ProvinceCore: Error mengambil data dropdown provinsi: ' . $e->getMessage());
|
||||
return collect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validasi kode provinsi
|
||||
*
|
||||
* @param string $code
|
||||
* @return bool
|
||||
*/
|
||||
public static function isValidCode($code)
|
||||
{
|
||||
try {
|
||||
Log::info('ProvinceCore: Validasi kode provinsi: ' . $code);
|
||||
return self::byCode($code)->exists();
|
||||
} catch (\Exception $e) {
|
||||
Log::error('ProvinceCore: Error validasi kode provinsi ' . $code . ': ' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Boot method untuk model events
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($model) {
|
||||
Log::info('ProvinceCore: Membuat data provinsi baru dengan kode: ' . $model->code);
|
||||
});
|
||||
|
||||
static::created(function ($model) {
|
||||
Log::info('ProvinceCore: Data provinsi berhasil dibuat dengan ID: ' . $model->id);
|
||||
});
|
||||
|
||||
static::updating(function ($model) {
|
||||
Log::info('ProvinceCore: Mengupdate data provinsi dengan ID: ' . $model->id);
|
||||
});
|
||||
|
||||
static::updated(function ($model) {
|
||||
Log::info('ProvinceCore: Data provinsi berhasil diupdate dengan ID: ' . $model->id);
|
||||
});
|
||||
|
||||
static::deleting(function ($model) {
|
||||
Log::info('ProvinceCore: Menghapus data provinsi dengan ID: ' . $model->id);
|
||||
});
|
||||
|
||||
static::deleted(function ($model) {
|
||||
Log::info('ProvinceCore: Data provinsi berhasil dihapus dengan ID: ' . $model->id);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Menjalankan migrasi untuk membuat tabel province_core
|
||||
* Tabel ini menyimpan data master provinsi dengan kode dan nama
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
Schema::create('province_core', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('code', 10)->unique()->comment('Kode provinsi unik');
|
||||
$table->string('name', 255)->comment('Nama provinsi');
|
||||
$table->timestamps();
|
||||
|
||||
// Index untuk performa pencarian
|
||||
$table->index(['code']);
|
||||
$table->index(['name']);
|
||||
});
|
||||
|
||||
DB::commit();
|
||||
Log::info('Migration province_core table berhasil dibuat');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::rollback();
|
||||
Log::error('Migration province_core table gagal: ' . $e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Membalikkan migrasi dengan menghapus tabel province_core
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
Schema::dropIfExists('province_core');
|
||||
|
||||
DB::commit();
|
||||
Log::info('Migration rollback province_core table berhasil');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::rollback();
|
||||
Log::error('Migration rollback province_core table gagal: ' . $e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user