diff --git a/app/Http/Controllers/Api/DesaKelurahanController.php b/app/Http/Controllers/Api/DesaKelurahanController.php new file mode 100644 index 0000000..aa33925 --- /dev/null +++ b/app/Http/Controllers/Api/DesaKelurahanController.php @@ -0,0 +1,92 @@ +get('size') ?: 10; + + $master = DesaKelurahan::query(); + + if ($request->has('search')) { + $s = $request->get('search'); + $s = strtolower($s); + $master->where(function($query) use ($s) { + $query->whereRaw('lower(kode) like (?)',["%{$s}%"]) + ->orWhereRaw('lower(nama) like (?)',["%{$s}%"]); + }); + } + if ($request->has('sort')) { + $order = $request->get('sort'); + $d = substr($order, 0, 1); + $dir = $d === '-' ? 'desc' : 'asc'; + $order = $d === '-' ? substr($order, 1) : $order; + $master->orderBy($order, $dir); + } + if ($request->has('kecamatan_id')) { + $master->where('kecamatan_id', $request->get('kecamatan_id')); + } + + $masterList = $master->paginate($size); + + return response()->json($masterList); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id): JsonResponse + { + return response()->json(DesaKelurahan::findOrFail($id)); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/app/Http/Controllers/Api/KabupatenKotaController.php b/app/Http/Controllers/Api/KabupatenKotaController.php new file mode 100644 index 0000000..8f0b62e --- /dev/null +++ b/app/Http/Controllers/Api/KabupatenKotaController.php @@ -0,0 +1,93 @@ +get('size') ?: 10; + + $master = KabupatenKota::query(); + + if ($request->has('search')) { + $s = $request->get('search'); + $s = strtolower($s); + $master->where(function($query) use ($s) { + $query->whereRaw('lower(kode) like (?)',["%{$s}%"]) + ->orWhereRaw('lower(nama) like (?)',["%{$s}%"]); + }); + } + if ($request->has('sort')) { + $order = $request->get('sort'); + $d = substr($order, 0, 1); + $dir = $d === '-' ? 'desc' : 'asc'; + $order = $d === '-' ? substr($order, 1) : $order; + $master->orderBy($order, $dir); + } + if ($request->has('provinsi_id')) { + $master->where('provinsi_id', $request->get('provinsi_id')); + } + + $masterList = $master->paginate($size); + + return response()->json($masterList); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id) + { + return response()->json(KabupatenKota::findOrFail($id)); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/app/Http/Controllers/Api/KecamatanController.php b/app/Http/Controllers/Api/KecamatanController.php new file mode 100644 index 0000000..3210701 --- /dev/null +++ b/app/Http/Controllers/Api/KecamatanController.php @@ -0,0 +1,92 @@ +get('size') ?: 10; + + $master = Kecamatan::query(); + + if ($request->has('search')) { + $s = $request->get('search'); + $s = strtolower($s); + $master->where(function($query) use ($s) { + $query->whereRaw('lower(kode) like (?)',["%{$s}%"]) + ->orWhereRaw('lower(nama) like (?)',["%{$s}%"]); + }); + } + if ($request->has('sort')) { + $order = $request->get('sort'); + $d = substr($order, 0, 1); + $dir = $d === '-' ? 'desc' : 'asc'; + $order = $d === '-' ? substr($order, 1) : $order; + $master->orderBy($order, $dir); + } + if ($request->has('kabupaten_kota_id')) { + $master->where('kabupaten_kota_id', $request->get('kabupaten_kota_id')); + } + + $masterList = $master->paginate($size); + + return response()->json($masterList); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id): JsonResponse + { + return response()->json(Kecamatan::findOrFail($id)); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/app/Http/Controllers/Api/KomoditasController.php b/app/Http/Controllers/Api/KomoditasController.php new file mode 100644 index 0000000..62359ac --- /dev/null +++ b/app/Http/Controllers/Api/KomoditasController.php @@ -0,0 +1,67 @@ +json(Komoditas::all()); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id): JsonResponse + { + return response()->json(Komoditas::findOrFail($id)); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/app/Http/Controllers/Api/ProvinsiController.php b/app/Http/Controllers/Api/ProvinsiController.php new file mode 100644 index 0000000..aca9ec6 --- /dev/null +++ b/app/Http/Controllers/Api/ProvinsiController.php @@ -0,0 +1,89 @@ +get('size') ?: 10; + + $master = Provinsi::query(); + + if ($request->has('search')) { + $s = $request->get('search'); + $s = strtolower($s); + $master->where(function($query) use ($s) { + $query->whereRaw('lower(kode) like (?)',["%{$s}%"]) + ->orWhereRaw('lower(nama) like (?)',["%{$s}%"]); + }); + } + if ($request->has('sort')) { + $order = $request->get('sort'); + $d = substr($order, 0, 1); + $dir = $d === '-' ? 'desc' : 'asc'; + $order = $d === '-' ? substr($order, 1) : $order; + $master->orderBy($order, $dir); + } + + $masterList = $master->paginate($size); + + return response()->json($masterList); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id): JsonResponse + { + return response()->json(Provinsi::findOrFail($id)); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/app/Http/Controllers/Api/TanamanController.php b/app/Http/Controllers/Api/TanamanController.php new file mode 100644 index 0000000..9aba598 --- /dev/null +++ b/app/Http/Controllers/Api/TanamanController.php @@ -0,0 +1,151 @@ +profile; + + $size = +$request->get('size') ?: 10; + + $tanamanQuery = Tanaman::query(); + + $tanamanQuery = $tanamanQuery->where('profile_id', $profile->id); + + if ($request->has('search')) { + $s = $request->get('search'); + $s = strtolower($s); + $tanamanQuery->where(function($query) use ($s) { + $query->whereRaw('lower(nama) like (?)',["%{$s}%"]); + }); + } + if ($request->has('sort')) { + $order = $request->get('sort'); + $d = substr($order, 0, 1); + $dir = $d === '-' ? 'desc' : 'asc'; + $order = $d === '-' ? substr($order, 1) : $order; + $tanamanQuery->orderBy($order, $dir); + } + if ($request->has('lahan_id')) { + $tanamanQuery->where('lahan_id', $request->get('lahan_id')); + } + if ($request->has('komoditas_id')) { + $tanamanQuery->where('komoditas_id', $request->get('komoditas_id')); + } + + $tanamanList = $tanamanQuery->paginate($size); + + return response()->json($tanamanList); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request): JsonResponse + { + Gate::authorize('petani'); + $user = Auth::user(); + $profile = $user->profile; + + $validated = $request->validate([ + 'nama' => ['required', 'string', 'max:255'], + 'komoditas_id' => ['required', 'exists:master_komoditas,id'], + 'lahan_id' => ['required', 'exists:map_lahan,id'], + 'latitude' => ['nullable', 'numeric'], + 'longitude' => ['nullable', 'numeric'], + ]); + + $tanaman = Tanaman::create([ + 'nama' => $validated['nama'], + 'komoditas_id' => $validated['komoditas_id'], + 'lahan_id' => $validated['lahan_id'], + 'latitude' => $validated['latitude'] ?? null, + 'longitude' => $validated['longitude'] ?? null, + 'profile_id' => $profile->id, + ]); + + return response()->json($tanaman); + } + + /** + * Display the specified resource. + */ + public function show(string $id) + { + return response()->json(Tanaman::findOrFail($id)); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id): JsonResponse + { + Gate::authorize('petani'); + $tanaman = Tanaman::findOrFail($id); + $user = Auth::user(); + $profile = $user->profile; + + if ($tanaman->profile_id !== $profile->id) { + return response()->json(['message' => 'Unauthorized'], 403); + } + + $validated = $request->validate([ + 'nama' => ['required', 'string', 'max:255'], + 'komoditas_id' => ['required', 'exists:master_komoditas,id'], + 'lahan_id' => ['required', 'exists:map_lahan,id'], + 'latitude' => ['nullable', 'numeric'], + 'longitude' => ['nullable', 'numeric'], + 'kondisi_tanaman' => ['nullable', 'string', 'in:sehat,sakit,mati'], + ]); + + $tanaman->update([ + 'nama' => $validated['nama'], + 'komoditas_id' => $validated['komoditas_id'] ?? $tanaman->komoditas_id, + 'lahan_id' => $validated['lahan_id'] ?? $tanaman->lahan_id, + 'latitude' => $validated['latitude'] ?? $tanaman->latitude, + 'longitude' => $validated['longitude'] ?? $tanaman->longitude, + 'kondisi_tanaman' => $validated['kondisi_tanaman'] ?? $tanaman->kondisi_tanaman, + ]); + + return response()->json($tanaman); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/app/Models/Map/Tanaman.php b/app/Models/Map/Tanaman.php index 9bdaa67..cd03e73 100644 --- a/app/Models/Map/Tanaman.php +++ b/app/Models/Map/Tanaman.php @@ -3,6 +3,7 @@ namespace App\Models\Map; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletes; class Tanaman extends Model { @@ -22,4 +23,17 @@ class Tanaman extends Model 'updated_by', 'deleted_by', ]; + + protected function casts(): array + { + return [ + 'kondisi_tanaman' => \App\Enums\KondisiTanaman::class, + ]; + } + + public function komoditas() + { + return $this->belongsTo(Komoditas::class, 'komoditas_id'); + } + } diff --git a/app/Models/Wilayah/DesaKelurahan.php b/app/Models/Wilayah/DesaKelurahan.php index a2861d9..f7af1f3 100644 --- a/app/Models/Wilayah/DesaKelurahan.php +++ b/app/Models/Wilayah/DesaKelurahan.php @@ -12,8 +12,8 @@ class DesaKelurahan extends Model protected $table = 'master_desa_kelurahan'; protected $fillable = [ - 'kode_desa_kelurahan', - 'nama_desa_kelurahan', + 'kode', + 'nama', 'path', 'kecamatan_id', ]; diff --git a/app/Models/Wilayah/KabupatenKota.php b/app/Models/Wilayah/KabupatenKota.php index f71adee..bb0a2cc 100644 --- a/app/Models/Wilayah/KabupatenKota.php +++ b/app/Models/Wilayah/KabupatenKota.php @@ -12,8 +12,8 @@ class KabupatenKota extends Model protected $table = 'master_kabupaten_kota'; protected $fillable = [ - 'kode_kabupaten_kota', - 'nama_kabupaten_kota', + 'kode', + 'nama', 'path', 'provinsi_id', ]; diff --git a/app/Models/Wilayah/Kecamatan.php b/app/Models/Wilayah/Kecamatan.php index 55b8ca7..aee32a6 100644 --- a/app/Models/Wilayah/Kecamatan.php +++ b/app/Models/Wilayah/Kecamatan.php @@ -12,8 +12,8 @@ class Kecamatan extends Model protected $table = 'master_kecamatan'; protected $fillable = [ - 'kode_kecamatan', - 'nama_kecamatan', + 'kode', + 'nama', 'path', 'kabupaten_kota_id', ]; diff --git a/app/Models/Wilayah/Provinsi.php b/app/Models/Wilayah/Provinsi.php index 9f245da..5f6ea91 100644 --- a/app/Models/Wilayah/Provinsi.php +++ b/app/Models/Wilayah/Provinsi.php @@ -12,8 +12,8 @@ class Provinsi extends Model protected $table = 'master_provinsi'; protected $fillable = [ - 'kode_provinsi', - 'nama_provinsi', + 'kode', + 'nama', 'path', 'iso', ]; diff --git a/database/migrations/2026_02_12_110653_create_wilayah_table.php b/database/migrations/2026_02_12_110653_create_wilayah_table.php index db1fd7a..3d2b528 100644 --- a/database/migrations/2026_02_12_110653_create_wilayah_table.php +++ b/database/migrations/2026_02_12_110653_create_wilayah_table.php @@ -13,8 +13,8 @@ public function up(): void { Schema::create('master_provinsi', function (Blueprint $table) { $table->id(); - $table->string('kode_provinsi', 10)->unique(); - $table->string('nama_provinsi', 100); + $table->string('kode', 2)->unique(); + $table->string('nama', 100); $table->string('path', 10000); $table->string('iso', 100); $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null'); @@ -26,8 +26,8 @@ public function up(): void Schema::create('master_kabupaten_kota', function (Blueprint $table) { $table->id(); - $table->string('kode_kabupaten_kota', 10)->unique(); - $table->string('nama_kabupaten_kota', 100); + $table->string('kode', 4)->unique(); + $table->string('nama', 100); $table->string('path', 10000); $table->foreignId('provinsi_id')->constrained('master_provinsi')->onDelete('set null'); $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null'); @@ -39,8 +39,8 @@ public function up(): void Schema::create('master_kecamatan', function (Blueprint $table) { $table->id(); - $table->string('kode_kecamatan', 10)->unique(); - $table->string('nama_kecamatan', 100); + $table->string('kode', 6)->unique(); + $table->string('nama', 100); $table->string('path', 10000); $table->foreignId('kabupaten_kota_id')->constrained('master_kabupaten_kota')->onDelete('set null'); $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null'); @@ -52,8 +52,8 @@ public function up(): void Schema::create('master_desa_kelurahan', function (Blueprint $table) { $table->id(); - $table->string('kode_desa_kelurahan', 10)->unique(); - $table->string('nama_desa_kelurahan', 100); + $table->string('kode', 10)->unique(); + $table->string('nama', 100); $table->string('path', 10000); $table->foreignId('kecamatan_id')->constrained('master_kecamatan')->onDelete('set null'); $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null'); diff --git a/routes/api.php b/routes/api.php index 57c5593..c91bc39 100644 --- a/routes/api.php +++ b/routes/api.php @@ -28,4 +28,18 @@ ->group(function () { Route::apiResource('/lahan', \App\Http\Controllers\Api\LahanController::class)->except(['create', 'edit']); + Route::apiResource('/tanaman', \App\Http\Controllers\Api\TanamanController::class)->except(['create', 'edit']); +}); + +Route::middleware('auth:sanctum') + ->name('master.') + ->prefix('master') + ->group(function () { + + Route::apiResource('/komoditas', \App\Http\Controllers\Api\KomoditasController::class)->only(['index', 'show']); + + Route::apiResource('/provinsi', \App\Http\Controllers\Api\ProvinsiController::class)->only(['index', 'show']); + Route::apiResource('/kabupaten-kota', \App\Http\Controllers\Api\KabupatenKotaController::class)->only(['index', 'show']); + Route::apiResource('/kecamatan', \App\Http\Controllers\Api\KecamatanController::class)->only(['index', 'show']); + Route::apiResource('/desa-kelurahan', \App\Http\Controllers\Api\DesaKelurahanController::class)->only(['index', 'show']); });