update wilayah dan pertanian
This commit is contained in:
@@ -5,52 +5,74 @@ 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;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Validation\Rules\Enum;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
use App\Http\Resources\Pertanian\TanamanResource;
|
||||
use App\Models\Pertanian\Tanaman;
|
||||
use App\Enums\KondisiTanaman;
|
||||
|
||||
class TanamanController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
public function index(Request $request): AnonymousResourceCollection
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
|
||||
$user = Auth::user();
|
||||
$profile = $user->profile;
|
||||
$profile = Auth::user()->profile;
|
||||
|
||||
$size = +$request->get('size') ?: 10;
|
||||
$size = $request->integer('size') ?: 10;
|
||||
|
||||
$tanamanQuery = Tanaman::query();
|
||||
$tanamanList = Tanaman::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->lahan_id, fn($q,$v) => $q->where('lahan_id',$v))
|
||||
->when($request->komoditas_id, fn($q,$v) => $q->where('komoditas_id',$v))
|
||||
->when($request->desa_kelurahan_id, fn($q,$v) =>
|
||||
$q->whereHas('lahan', fn($k)
|
||||
=> $k->where('desa_kelurahan_id',$v)
|
||||
)
|
||||
)
|
||||
->when($request->kecamatan_id, fn ($q, $v) =>
|
||||
$q->whereHas('lahan.desaKelurahan', fn ($k) =>
|
||||
$k->where('kecamatan_id', $v)
|
||||
)
|
||||
)
|
||||
->when($request->kabupaten_kota_id, fn ($q, $v) =>
|
||||
$q->whereHas('lahan.desaKelurahan.kecamatan', fn ($k) =>
|
||||
$k->where('kabupaten_kota_id', $v)
|
||||
)
|
||||
)
|
||||
->when($request->provinsi_id, fn ($q, $v) =>
|
||||
$q->whereHas('lahan.desaKelurahan.kecamatan.kabupatenkota', fn ($k) =>
|
||||
$k->where('provinsi_id', $v)
|
||||
)
|
||||
);
|
||||
|
||||
$tanamanQuery = $tanamanQuery->where('profile_id', $profile->id);
|
||||
if ($request->filled('sort')) {
|
||||
$dir = str_starts_with($request->sort, '-') ? 'desc' : 'asc';
|
||||
$column = ltrim($request->sort, '-');
|
||||
|
||||
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'));
|
||||
$allowed = ['id', 'nama'];
|
||||
|
||||
if (in_array($column, $allowed)) {
|
||||
$tanamanList->orderBy($column, $dir);
|
||||
}
|
||||
} else {
|
||||
$tanamanList->orderBy('nama', 'asc');
|
||||
}
|
||||
|
||||
$tanamanList = $tanamanQuery->paginate($size);
|
||||
$tanamanList = $tanamanList->paginate($size);
|
||||
|
||||
return response()->json($tanamanList);
|
||||
return TanamanResource::collection($tanamanList);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,35 +89,46 @@ class TanamanController extends Controller
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
$user = Auth::user();
|
||||
$profile = $user->profile;
|
||||
$profile = Auth::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'],
|
||||
'lahan_id' => [
|
||||
'required',
|
||||
Rule::exists('map_lahan', 'id')->where(function ($query) use ($profile) {
|
||||
$query->where('profile_id', $profile->id)
|
||||
->whereNull('deleted_at');
|
||||
}),
|
||||
],
|
||||
'latitude' => ['nullable', 'numeric', 'between:-90,90', 'required_with:longitude'],
|
||||
'longitude' => ['nullable', 'numeric', 'between:-180,180', 'required_with:latitude'],
|
||||
'kondisi_tanaman' => ['nullable', 'string', new Enum(KondisiTanaman::class)],
|
||||
]);
|
||||
|
||||
$tanaman = Tanaman::create([
|
||||
'nama' => $validated['nama'],
|
||||
'komoditas_id' => $validated['komoditas_id'],
|
||||
'lahan_id' => $validated['lahan_id'],
|
||||
'latitude' => $validated['latitude'] ?? null,
|
||||
'longitude' => $validated['longitude'] ?? null,
|
||||
...$validated,
|
||||
'profile_id' => $profile->id,
|
||||
]);
|
||||
|
||||
return response()->json($tanaman);
|
||||
return (new TanamanResource($tanaman))
|
||||
->response()
|
||||
->setStatusCode(201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
public function show(string $id): JsonResponse
|
||||
{
|
||||
return response()->json(Tanaman::findOrFail($id));
|
||||
Gate::authorize('petani');
|
||||
|
||||
$tanaman = Tanaman::findOrFail($id);
|
||||
if ($tanaman->profile_id !== Auth::user()->profile->id) {
|
||||
return response()->json(['message' => 'Unauthorized'], 403);
|
||||
}
|
||||
|
||||
return (new TanamanResource($tanaman))->response();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,39 +146,45 @@ class TanamanController extends Controller
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
$tanaman = Tanaman::findOrFail($id);
|
||||
$user = Auth::user();
|
||||
$profile = $user->profile;
|
||||
|
||||
if ($tanaman->profile_id !== $profile->id) {
|
||||
if ($tanaman->profile_id !== Auth::user()->profile->id) {
|
||||
return response()->json(['message' => 'Unauthorized'], 403);
|
||||
}
|
||||
}
|
||||
|
||||
$profile = Auth::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'],
|
||||
'kondisi_tanaman' => ['nullable', 'string', 'in:sehat,sakit,mati'],
|
||||
'lahan_id' => [
|
||||
'required',
|
||||
Rule::exists('map_lahan', 'id')->where(function ($query) use ($profile) {
|
||||
$query->where('profile_id', $profile->id)
|
||||
->whereNull('deleted_at');
|
||||
}),
|
||||
],
|
||||
'latitude' => ['nullable', 'numeric', 'between:-90,90', 'required_with:longitude'],
|
||||
'longitude' => ['nullable', 'numeric', 'between:-180,180', 'required_with:latitude'],
|
||||
'kondisi_tanaman' => ['nullable', 'string', new Enum(KondisiTanaman::class)],
|
||||
]);
|
||||
|
||||
$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,
|
||||
]);
|
||||
$tanaman->update($validated);
|
||||
|
||||
return response()->json($tanaman);
|
||||
return (new TanamanResource($tanaman))->response();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
public function destroy(string $id): JsonResponse
|
||||
{
|
||||
//
|
||||
Gate::authorize('petani');
|
||||
$tanaman = Tanaman::findOrFail($id);
|
||||
if ($tanaman->profile_id !== Auth::user()->profile->id) {
|
||||
return response()->json(['message' => 'Unauthorized'], 403);
|
||||
}
|
||||
|
||||
$tanaman->delete();
|
||||
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user