update wilayah dan pertanian
This commit is contained in:
parent
167cf5c680
commit
502c3be5bf
@ -4,7 +4,9 @@
|
|||||||
|
|
||||||
enum KondisiTanaman: string
|
enum KondisiTanaman: string
|
||||||
{
|
{
|
||||||
case SEHAT = 'sehat';
|
case PRODUKTIF = 'productive';
|
||||||
case SAKIT = 'sakit';
|
case KURANG_PRODUKTIF = 'less_productive';
|
||||||
case MATI = 'mati';
|
case TIDAK_PRODUKTIF = 'not_productive';
|
||||||
|
case SAKIT = 'sick';
|
||||||
|
case MATI = 'dead';
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,40 +7,44 @@
|
|||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use App\Models\Wilayah\Provinsi;
|
use App\Models\Wilayah\Provinsi;
|
||||||
use App\Models\Wilayah\KabupatenKota;
|
use App\Models\Wilayah\KabupatenKota;
|
||||||
|
use App\Http\Resources\Wilayah\KabupatenKotaResource;
|
||||||
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||||
|
|
||||||
class KabupatenKotaController extends Controller
|
class KabupatenKotaController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*/
|
*/
|
||||||
public function index(Request $request): JsonResponse
|
public function index(Request $request): AnonymousResourceCollection
|
||||||
{
|
{
|
||||||
$size = +$request->get('size') ?: 10;
|
$size = $request->integer('size') ?: 10;
|
||||||
|
|
||||||
$master = KabupatenKota::query();
|
$master = KabupatenKota::with('provinsi')
|
||||||
|
->when($request->search, function ($q, $search) {
|
||||||
|
$search = strtolower($search);
|
||||||
|
$q->where(function ($query) use ($search) {
|
||||||
|
$query->whereRaw('lower(kode) like ?', ["%{$search}%"])
|
||||||
|
->orWhereRaw('lower(nama) like ?', ["%{$search}%"]);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
->when($request->provinsi_id, fn($q,$v) => $q->where('provinsi_id',$v));
|
||||||
|
|
||||||
if ($request->has('search')) {
|
if ($request->filled('sort')) {
|
||||||
$s = $request->get('search');
|
$dir = str_starts_with($request->sort, '-') ? 'desc' : 'asc';
|
||||||
$s = strtolower($s);
|
$column = ltrim($request->sort, '-');
|
||||||
$master->where(function($query) use ($s) {
|
|
||||||
$query->whereRaw('lower(kode) like (?)',["%{$s}%"])
|
$allowed = ['id', 'kode', 'nama'];
|
||||||
->orWhereRaw('lower(nama) like (?)',["%{$s}%"]);
|
|
||||||
});
|
if (in_array($column, $allowed)) {
|
||||||
}
|
$master->orderBy($column, $dir);
|
||||||
if ($request->has('sort')) {
|
}
|
||||||
$order = $request->get('sort');
|
} else {
|
||||||
$d = substr($order, 0, 1);
|
$master->orderBy('kode', 'asc');
|
||||||
$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);
|
$masterList = $master->paginate($size);
|
||||||
|
|
||||||
return response()->json($masterList);
|
return KabupatenKotaResource::collection($masterList);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -62,9 +66,9 @@ public function store(Request $request)
|
|||||||
/**
|
/**
|
||||||
* Display the specified resource.
|
* Display the specified resource.
|
||||||
*/
|
*/
|
||||||
public function show(string $id)
|
public function show(string $id): JsonResponse
|
||||||
{
|
{
|
||||||
return response()->json(KabupatenKota::findOrFail($id));
|
return response()->json(KabupatenKotaResource::make(KabupatenKota::with('provinsi')->findOrFail($id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -16,7 +16,7 @@ class KecamatanController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function index(Request $request): AnonymousResourceCollection
|
public function index(Request $request): AnonymousResourceCollection
|
||||||
{
|
{
|
||||||
$size = $request->get('size') ?: 10;
|
$size = $request->integer('size') ?: 10;
|
||||||
|
|
||||||
$master = Kecamatan::with('kabupatenKota')
|
$master = Kecamatan::with('kabupatenKota')
|
||||||
->when($request->search, function ($q, $search) {
|
->when($request->search, function ($q, $search) {
|
||||||
|
|||||||
@ -5,16 +5,18 @@
|
|||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use App\Models\Komoditas\Komoditas;
|
use App\Models\Pertanian\Komoditas;
|
||||||
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||||
|
use App\Http\Resources\Pertanian\KomoditasResource;
|
||||||
|
|
||||||
class KomoditasController extends Controller
|
class KomoditasController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*/
|
*/
|
||||||
public function index(): JsonResponse
|
public function index(): AnonymousResourceCollection
|
||||||
{
|
{
|
||||||
return response()->json(Komoditas::all());
|
return KomoditasResource::collection(Komoditas::all());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,7 +40,7 @@ public function store(Request $request)
|
|||||||
*/
|
*/
|
||||||
public function show(string $id): JsonResponse
|
public function show(string $id): JsonResponse
|
||||||
{
|
{
|
||||||
return response()->json(Komoditas::findOrFail($id));
|
return response()->json(KomoditasResource::make(Komoditas::findOrFail($id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -7,24 +7,66 @@
|
|||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\Gate;
|
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
|
class LahanController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*/
|
*/
|
||||||
public function index(): JsonResponse
|
public function index(Request $request): AnonymousResourceCollection
|
||||||
{
|
{
|
||||||
Gate::authorize('petani');
|
Gate::authorize('petani');
|
||||||
|
|
||||||
$user = Auth::user();
|
$user = Auth::user();
|
||||||
$profile = $user->profile;
|
$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 @@ public function store(Request $request): JsonResponse
|
|||||||
|
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'nama' => ['required', 'string', 'max:255'],
|
'nama' => ['required', 'string', 'max:255'],
|
||||||
'status_kepemilikan' => ['required', 'string'],
|
'status_kepemilikan' => ['required', new Enum(StatusLahan::class)],
|
||||||
'desa_kelurahan_id' => ['numeric'],
|
'desa_kelurahan_id' => ['nullable', 'numeric', 'exists:master_desa_kelurahan,id'],
|
||||||
'path' => ['string'],
|
'path' => ['nullable', 'array'],
|
||||||
'luas_lahan' => ['numeric'],
|
'path.*' => ['array'],
|
||||||
|
'luas_lahan' => ['nullable', 'numeric'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$lahan = Lahan::create([
|
$lahan = Lahan::create([
|
||||||
'nama' => $validated['nama'],
|
...$validated,
|
||||||
'status_kepemilikan' => $validated['status_kepemilikan'],
|
|
||||||
'profile_id' => $profile->id,
|
'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.
|
* Display the specified resource.
|
||||||
*/
|
*/
|
||||||
public function show(string $id)
|
public function show(string $id): JsonResponse
|
||||||
{
|
{
|
||||||
Gate::authorize('petani');
|
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.
|
* Update the specified resource in storage.
|
||||||
*/
|
*/
|
||||||
public function update(Request $request, string $id)
|
public function update(Request $request, string $id): JsonResponse
|
||||||
{
|
{
|
||||||
Gate::authorize('petani');
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'nama' => ['required', 'string', 'max:255'],
|
'nama' => ['required', 'string', 'max:255'],
|
||||||
'status_kepemilikan' => ['required', 'string'],
|
'status_kepemilikan' => ['required', new Enum(StatusLahan::class)],
|
||||||
'desa_kelurahan_id' => ['numeric'],
|
'desa_kelurahan_id' => ['nullable', 'numeric', 'exists:master_desa_kelurahan,id'],
|
||||||
'path' => ['string'],
|
'path' => ['nullable', 'array'],
|
||||||
'luas_lahan' => ['numeric'],
|
'path.*' => ['array'],
|
||||||
|
'luas_lahan' => ['nullable', 'numeric'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$lahan->update($validated);
|
$lahan->update($validated);
|
||||||
|
|
||||||
return response()->json($lahan);
|
return (new LahanResource($lahan))->response();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the specified resource from storage.
|
* Remove the specified resource from storage.
|
||||||
*/
|
*/
|
||||||
public function destroy(string $id)
|
public function destroy(string $id): JsonResponse
|
||||||
{
|
{
|
||||||
Gate::authorize('petani');
|
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();
|
$lahan->delete();
|
||||||
|
|
||||||
return response()->json(null, 204);
|
return response()->json(null, 204);
|
||||||
|
|||||||
@ -6,37 +6,42 @@
|
|||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use App\Models\Wilayah\Provinsi;
|
use App\Models\Wilayah\Provinsi;
|
||||||
|
use App\Http\Resources\Wilayah\ProvinsiResource;
|
||||||
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||||
|
|
||||||
class ProvinsiController extends Controller
|
class ProvinsiController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*/
|
*/
|
||||||
public function index(Request $request): JsonResponse
|
public function index(Request $request): AnonymousResourceCollection
|
||||||
{
|
{
|
||||||
$size = +$request->get('size') ?: 10;
|
$size = $request->integer('size') ?: 10;
|
||||||
|
|
||||||
$master = Provinsi::query();
|
$master = Provinsi::when($request->search, function ($q, $search) {
|
||||||
|
$search = strtolower($search);
|
||||||
if ($request->has('search')) {
|
$q->where(function ($query) use ($search) {
|
||||||
$s = $request->get('search');
|
$query->whereRaw('lower(kode) like ?', ["%{$search}%"])
|
||||||
$s = strtolower($s);
|
->orWhereRaw('lower(nama) like ?', ["%{$search}%"]);
|
||||||
$master->where(function($query) use ($s) {
|
});
|
||||||
$query->whereRaw('lower(kode) like (?)',["%{$s}%"])
|
|
||||||
->orWhereRaw('lower(nama) like (?)',["%{$s}%"]);
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
if ($request->has('sort')) {
|
if ($request->filled('sort')) {
|
||||||
$order = $request->get('sort');
|
$dir = str_starts_with($request->sort, '-') ? 'desc' : 'asc';
|
||||||
$d = substr($order, 0, 1);
|
$column = ltrim($request->sort, '-');
|
||||||
$dir = $d === '-' ? 'desc' : 'asc';
|
|
||||||
$order = $d === '-' ? substr($order, 1) : $order;
|
$allowed = ['id', 'kode', 'nama'];
|
||||||
$master->orderBy($order, $dir);
|
|
||||||
|
if (in_array($column, $allowed)) {
|
||||||
|
$master->orderBy($column, $dir);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$master->orderBy('kode', 'asc');
|
||||||
}
|
}
|
||||||
|
|
||||||
$masterList = $master->paginate($size);
|
$masterList = $master->paginate($size);
|
||||||
|
|
||||||
return response()->json($masterList);
|
return ProvinsiResource::collection($masterList);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -60,7 +65,7 @@ public function store(Request $request)
|
|||||||
*/
|
*/
|
||||||
public function show(string $id): JsonResponse
|
public function show(string $id): JsonResponse
|
||||||
{
|
{
|
||||||
return response()->json(Provinsi::findOrFail($id));
|
return response()->json(ProvinsiResource::make(Provinsi::findOrFail($id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -5,52 +5,74 @@
|
|||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use App\Models\Map\Tanaman;
|
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\Gate;
|
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
|
class TanamanController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*/
|
*/
|
||||||
public function index(Request $request): JsonResponse
|
public function index(Request $request): AnonymousResourceCollection
|
||||||
{
|
{
|
||||||
Gate::authorize('petani');
|
Gate::authorize('petani');
|
||||||
|
|
||||||
$user = Auth::user();
|
$profile = Auth::user()->profile;
|
||||||
$profile = $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')) {
|
$allowed = ['id', 'nama'];
|
||||||
$s = $request->get('search');
|
|
||||||
$s = strtolower($s);
|
if (in_array($column, $allowed)) {
|
||||||
$tanamanQuery->where(function($query) use ($s) {
|
$tanamanList->orderBy($column, $dir);
|
||||||
$query->whereRaw('lower(nama) like (?)',["%{$s}%"]);
|
}
|
||||||
});
|
} else {
|
||||||
}
|
$tanamanList->orderBy('nama', 'asc');
|
||||||
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);
|
$tanamanList = $tanamanList->paginate($size);
|
||||||
|
|
||||||
return response()->json($tanamanList);
|
return TanamanResource::collection($tanamanList);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -67,35 +89,46 @@ public function create()
|
|||||||
public function store(Request $request): JsonResponse
|
public function store(Request $request): JsonResponse
|
||||||
{
|
{
|
||||||
Gate::authorize('petani');
|
Gate::authorize('petani');
|
||||||
$user = Auth::user();
|
$profile = Auth::user()->profile;
|
||||||
$profile = $user->profile;
|
|
||||||
|
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'nama' => ['required', 'string', 'max:255'],
|
'nama' => ['required', 'string', 'max:255'],
|
||||||
'komoditas_id' => ['required', 'exists:master_komoditas,id'],
|
'komoditas_id' => ['required', 'exists:master_komoditas,id'],
|
||||||
'lahan_id' => ['required', 'exists:map_lahan,id'],
|
'lahan_id' => [
|
||||||
'latitude' => ['nullable', 'numeric'],
|
'required',
|
||||||
'longitude' => ['nullable', 'numeric'],
|
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([
|
$tanaman = Tanaman::create([
|
||||||
'nama' => $validated['nama'],
|
...$validated,
|
||||||
'komoditas_id' => $validated['komoditas_id'],
|
|
||||||
'lahan_id' => $validated['lahan_id'],
|
|
||||||
'latitude' => $validated['latitude'] ?? null,
|
|
||||||
'longitude' => $validated['longitude'] ?? null,
|
|
||||||
'profile_id' => $profile->id,
|
'profile_id' => $profile->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return response()->json($tanaman);
|
return (new TanamanResource($tanaman))
|
||||||
|
->response()
|
||||||
|
->setStatusCode(201);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display the specified resource.
|
* 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 @@ public function update(Request $request, string $id): JsonResponse
|
|||||||
{
|
{
|
||||||
Gate::authorize('petani');
|
Gate::authorize('petani');
|
||||||
$tanaman = Tanaman::findOrFail($id);
|
$tanaman = Tanaman::findOrFail($id);
|
||||||
$user = Auth::user();
|
if ($tanaman->profile_id !== Auth::user()->profile->id) {
|
||||||
$profile = $user->profile;
|
|
||||||
|
|
||||||
if ($tanaman->profile_id !== $profile->id) {
|
|
||||||
return response()->json(['message' => 'Unauthorized'], 403);
|
return response()->json(['message' => 'Unauthorized'], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$profile = Auth::user()->profile;
|
||||||
|
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'nama' => ['required', 'string', 'max:255'],
|
'nama' => ['required', 'string', 'max:255'],
|
||||||
'komoditas_id' => ['required', 'exists:master_komoditas,id'],
|
'komoditas_id' => ['required', 'exists:master_komoditas,id'],
|
||||||
'lahan_id' => ['required', 'exists:map_lahan,id'],
|
'lahan_id' => [
|
||||||
'latitude' => ['nullable', 'numeric'],
|
'required',
|
||||||
'longitude' => ['nullable', 'numeric'],
|
Rule::exists('map_lahan', 'id')->where(function ($query) use ($profile) {
|
||||||
'kondisi_tanaman' => ['nullable', 'string', 'in:sehat,sakit,mati'],
|
$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([
|
$tanaman->update($validated);
|
||||||
'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);
|
return (new TanamanResource($tanaman))->response();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the specified resource from storage.
|
* 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
22
app/Http/Resources/Pertanian/KomoditasResource.php
Normal file
22
app/Http/Resources/Pertanian/KomoditasResource.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources\Pertanian;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class KomoditasResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'nama' => $this->nama
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
30
app/Http/Resources/Pertanian/LahanResource.php
Normal file
30
app/Http/Resources/Pertanian/LahanResource.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources\Pertanian;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
use App\Http\Resources\Wilayah\DesaKelurahanResource;
|
||||||
|
|
||||||
|
class LahanResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'nama' => $this->nama,
|
||||||
|
'status_kepemilikan' => $this->status_kepemilikan,
|
||||||
|
'profile_id' => $this->profile_id,
|
||||||
|
'desa_kelurahan' => $this->desaKelurahan ? new DesaKelurahanResource($this->desaKelurahan) : null,
|
||||||
|
'path' => $this->path,
|
||||||
|
'luas_lahan' => $this->luas_lahan,
|
||||||
|
'keluarga_pengelola_id' => $this->keluarga_pengelola_id,
|
||||||
|
'adat_pemilik_id' => $this->adat_pemilik_id,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
29
app/Http/Resources/Pertanian/TanamanResource.php
Normal file
29
app/Http/Resources/Pertanian/TanamanResource.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources\Pertanian;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
use App\Http\Resources\Wilayah\DesaKelurahanResource;
|
||||||
|
|
||||||
|
class TanamanResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'nama' => $this->nama,
|
||||||
|
'komoditas' => $this->komoditas ? new KomoditasResource($this->komoditas) : null,
|
||||||
|
'profile_id' => $this->profile_id,
|
||||||
|
'lahan' => $this->lahan ? new LahanResource($this->lahan) : null,
|
||||||
|
'latitude' => $this->latitude,
|
||||||
|
'longitude' => $this->longitude,
|
||||||
|
'kondisi_tanaman' => $this->kondisi_tanaman,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,6 +7,9 @@
|
|||||||
use App\Models\Wilayah\DesaKelurahan;
|
use App\Models\Wilayah\DesaKelurahan;
|
||||||
use App\Models\Pertanian\Tanaman;
|
use App\Models\Pertanian\Tanaman;
|
||||||
use App\Models\Profile;
|
use App\Models\Profile;
|
||||||
|
use App\Enums\StatusLahan;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
|
||||||
class Lahan extends Model
|
class Lahan extends Model
|
||||||
{
|
{
|
||||||
@ -28,17 +31,22 @@ class Lahan extends Model
|
|||||||
'deleted_by',
|
'deleted_by',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function profile()
|
protected $casts = [
|
||||||
|
'status_kepemilikan' => StatusLahan::class,
|
||||||
|
'path' => 'array',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function profile(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Profile::class, 'profile_id');
|
return $this->belongsTo(Profile::class, 'profile_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function desakelurahan()
|
public function desaKelurahan(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(DesaKelurahan::class, 'desa_kelurahan_id');
|
return $this->belongsTo(DesaKelurahan::class, 'desa_kelurahan_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tanaman()
|
public function tanaman(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(Tanaman::class, 'lahan_id');
|
return $this->hasMany(Tanaman::class, 'lahan_id');
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user