update api

This commit is contained in:
Irwan Cahyono 2026-02-20 23:52:38 +07:00
parent 1dfcf9b2f5
commit c19e39fe5e
13 changed files with 628 additions and 16 deletions

View File

@ -0,0 +1,92 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Wilayah\DesaKelurahan;
use Illuminate\Http\JsonResponse;
class DesaKelurahanController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request): JsonResponse
{
$size = +$request->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)
{
//
}
}

View File

@ -0,0 +1,93 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Models\Wilayah\Provinsi;
use App\Models\Wilayah\KabupatenKota;
class KabupatenKotaController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request): JsonResponse
{
$size = +$request->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)
{
//
}
}

View File

@ -0,0 +1,92 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Wilayah\Kecamatan;
use Illuminate\Http\JsonResponse;
class KecamatanController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request): JsonResponse
{
$size = +$request->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)
{
//
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Models\Komoditas\Komoditas;
class KomoditasController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(): JsonResponse
{
return response()->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)
{
//
}
}

View File

@ -0,0 +1,89 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Models\Wilayah\Provinsi;
class ProvinsiController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request): JsonResponse
{
$size = +$request->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)
{
//
}
}

View File

@ -0,0 +1,151 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Models\Map\Tanaman;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
class TanamanController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request): JsonResponse
{
Gate::authorize('petani');
$user = Auth::user();
$profile = $user->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)
{
//
}
}

View File

@ -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');
}
}

View File

@ -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',
];

View File

@ -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',
];

View File

@ -12,8 +12,8 @@ class Kecamatan extends Model
protected $table = 'master_kecamatan';
protected $fillable = [
'kode_kecamatan',
'nama_kecamatan',
'kode',
'nama',
'path',
'kabupaten_kota_id',
];

View File

@ -12,8 +12,8 @@ class Provinsi extends Model
protected $table = 'master_provinsi';
protected $fillable = [
'kode_provinsi',
'nama_provinsi',
'kode',
'nama',
'path',
'iso',
];

View File

@ -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');

View File

@ -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']);
});