update api

This commit is contained in:
2026-02-20 23:52:38 +07:00
parent 1dfcf9b2f5
commit c19e39fe5e
13 changed files with 628 additions and 16 deletions
@@ -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)
{
//
}
}
@@ -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)
{
//
}
}
@@ -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)
{
//
}
}
@@ -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)
{
//
}
}
@@ -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)
{
//
}
}
@@ -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)
{
//
}
}
+14
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');
}
}
+2 -2
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',
];
+2 -2
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',
];
+2 -2
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',
];
+2 -2
View File
@@ -12,8 +12,8 @@ class Provinsi extends Model
protected $table = 'master_provinsi';
protected $fillable = [
'kode_provinsi',
'nama_provinsi',
'kode',
'nama',
'path',
'iso',
];