update wilayah dan pertanian
This commit is contained in:
@@ -7,24 +7,66 @@ use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Validation\Rules\Enum;
|
||||
|
||||
use App\Models\Map\Lahan;
|
||||
use App\Models\Pertanian\Lahan;
|
||||
use App\Http\Resources\Pertanian\LahanResource;
|
||||
use App\Enums\StatusLahan;
|
||||
|
||||
class LahanController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(): JsonResponse
|
||||
public function index(Request $request): AnonymousResourceCollection
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
|
||||
$user = Auth::user();
|
||||
$profile = $user->profile;
|
||||
|
||||
$listLahan = Lahan::where('profile_id', $profile->id)->get();
|
||||
$size = $request->integer('size') ?: 10;
|
||||
|
||||
return response()->json($listLahan);
|
||||
$listLahan = Lahan::where('profile_id', $profile->id)
|
||||
->when($request->search, function ($q, $search) {
|
||||
$search = strtolower($search);
|
||||
$q->whereRaw('lower(nama) like ?', ["%{$search}%"]);
|
||||
})
|
||||
->when($request->status_kepemilikan, fn($q,$v) => $q->where('status_kepemilikan',$v))
|
||||
->when($request->desa_kelurahan_id, fn($q,$v) => $q->where('desa_kelurahan_id',$v))
|
||||
->when($request->kecamatan_id, fn ($q, $v) =>
|
||||
$q->whereHas('desaKelurahan', fn ($k) =>
|
||||
$k->where('kecamatan_id', $v)
|
||||
)
|
||||
)
|
||||
->when($request->kabupaten_kota_id, fn ($q, $v) =>
|
||||
$q->whereHas('desaKelurahan.kecamatan', fn ($k) =>
|
||||
$k->where('kabupaten_kota_id', $v)
|
||||
)
|
||||
)
|
||||
->when($request->provinsi_id, fn ($q, $v) =>
|
||||
$q->whereHas('desaKelurahan.kecamatan.kabupatenkota', fn ($k) =>
|
||||
$k->where('provinsi_id', $v)
|
||||
)
|
||||
);
|
||||
if ($request->filled('sort')) {
|
||||
$dir = str_starts_with($request->sort, '-') ? 'desc' : 'asc';
|
||||
$column = ltrim($request->sort, '-');
|
||||
|
||||
$allowed = ['id', 'nama'];
|
||||
|
||||
if (in_array($column, $allowed)) {
|
||||
$listLahan->orderBy($column, $dir);
|
||||
}
|
||||
} else {
|
||||
$listLahan->orderBy('nama', 'asc');
|
||||
}
|
||||
|
||||
$listLahan = $listLahan->paginate($size);
|
||||
|
||||
|
||||
return LahanResource::collection($listLahan);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,66 +81,76 @@ class LahanController extends Controller
|
||||
|
||||
$validated = $request->validate([
|
||||
'nama' => ['required', 'string', 'max:255'],
|
||||
'status_kepemilikan' => ['required', 'string'],
|
||||
'desa_kelurahan_id' => ['numeric'],
|
||||
'path' => ['string'],
|
||||
'luas_lahan' => ['numeric'],
|
||||
'status_kepemilikan' => ['required', new Enum(StatusLahan::class)],
|
||||
'desa_kelurahan_id' => ['nullable', 'numeric', 'exists:master_desa_kelurahan,id'],
|
||||
'path' => ['nullable', 'array'],
|
||||
'path.*' => ['array'],
|
||||
'luas_lahan' => ['nullable', 'numeric'],
|
||||
]);
|
||||
|
||||
$lahan = Lahan::create([
|
||||
'nama' => $validated['nama'],
|
||||
'status_kepemilikan' => $validated['status_kepemilikan'],
|
||||
...$validated,
|
||||
'profile_id' => $profile->id,
|
||||
'desa_kelurahan_id' => $validated['desa_kelurahan_id'] ?? null,
|
||||
'path' => $validated['path'] ?? null,
|
||||
'luas_lahan' => $validated['luas_lahan'] ?? null,
|
||||
]);
|
||||
|
||||
return response()->json($lahan, 201);
|
||||
return (new LahanResource($lahan))
|
||||
->response()
|
||||
->setStatusCode(201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
public function show(string $id): JsonResponse
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
|
||||
$lahan = Lahan::findOrFail($id);
|
||||
$lahan = Lahan::with('desaKelurahan')->findOrFail($id);
|
||||
if ($lahan->profile_id !== Auth::user()->profile->id) {
|
||||
return response()->json(['message' => 'Unauthorized'], 403);
|
||||
}
|
||||
|
||||
return response()->json($lahan);
|
||||
return (new LahanResource($lahan))->response();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
public function update(Request $request, string $id): JsonResponse
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
|
||||
$lahan = Lahan::findOrFail($id);
|
||||
if ($lahan->profile_id !== Auth::user()->profile->id) {
|
||||
return response()->json(['message' => 'Unauthorized'], 403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'nama' => ['required', 'string', 'max:255'],
|
||||
'status_kepemilikan' => ['required', 'string'],
|
||||
'desa_kelurahan_id' => ['numeric'],
|
||||
'path' => ['string'],
|
||||
'luas_lahan' => ['numeric'],
|
||||
'status_kepemilikan' => ['required', new Enum(StatusLahan::class)],
|
||||
'desa_kelurahan_id' => ['nullable', 'numeric', 'exists:master_desa_kelurahan,id'],
|
||||
'path' => ['nullable', 'array'],
|
||||
'path.*' => ['array'],
|
||||
'luas_lahan' => ['nullable', 'numeric'],
|
||||
]);
|
||||
|
||||
$lahan->update($validated);
|
||||
|
||||
return response()->json($lahan);
|
||||
return (new LahanResource($lahan))->response();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
public function destroy(string $id): JsonResponse
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
|
||||
$lahan = Lahan::findOrFail($id);
|
||||
$lahan = Lahan::findOrFail($id);
|
||||
if ($lahan->profile_id !== Auth::user()->profile->id) {
|
||||
return response()->json(['message' => 'Unauthorized'], 403);
|
||||
}
|
||||
|
||||
$lahan->delete();
|
||||
|
||||
return response()->json(null, 204);
|
||||
|
||||
Reference in New Issue
Block a user