From 9231f25e5d08bfaa19ab45348869dd2dc7d63af7 Mon Sep 17 00:00:00 2001 From: Irwan Cahyono Date: Tue, 26 May 2026 00:55:51 +0700 Subject: [PATCH] Program api --- .../Api/Core/ProgramController.php | 59 +++++++++++++++++++ .../Core/ProgramKomoditasResource.php | 27 +++++++++ .../Resources/Core/ProgramLokasiResource.php | 36 +++++++++++ app/Http/Resources/Core/ProgramResource.php | 33 +++++++++++ routes/api.php | 11 +++- 5 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/Api/Core/ProgramController.php create mode 100644 app/Http/Resources/Core/ProgramKomoditasResource.php create mode 100644 app/Http/Resources/Core/ProgramLokasiResource.php create mode 100644 app/Http/Resources/Core/ProgramResource.php diff --git a/app/Http/Controllers/Api/Core/ProgramController.php b/app/Http/Controllers/Api/Core/ProgramController.php new file mode 100644 index 0000000..34f1cec --- /dev/null +++ b/app/Http/Controllers/Api/Core/ProgramController.php @@ -0,0 +1,59 @@ +query('per_page', 10); + $search = $request->query('search', ''); + + $program = Program::where('nama', 'like', "%$search%") + ->paginate($perPage); + + return ProgramResource::collection($program); + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id): ProgramResource + { + $program = Program::findOrFail($id); + return new ProgramResource($program); + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/app/Http/Resources/Core/ProgramKomoditasResource.php b/app/Http/Resources/Core/ProgramKomoditasResource.php new file mode 100644 index 0000000..313e256 --- /dev/null +++ b/app/Http/Resources/Core/ProgramKomoditasResource.php @@ -0,0 +1,27 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + 'program_id' => $this->program_id, + 'komoditas_id' => $this->komoditas_id, + 'default' => $this->default, + 'komoditas' => $this->komoditas ? new KomoditasResource($this->komoditas) : null, + ]; + } +} diff --git a/app/Http/Resources/Core/ProgramLokasiResource.php b/app/Http/Resources/Core/ProgramLokasiResource.php new file mode 100644 index 0000000..934c296 --- /dev/null +++ b/app/Http/Resources/Core/ProgramLokasiResource.php @@ -0,0 +1,36 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + 'program_id' => $this->program_id, + 'provinsi_id' => $this->provinsi_id, + 'kabupaten_kota_id' => $this->kabupaten_kota_id, + 'kecamatan_id' => $this->kecamatan_id, + 'desa_kelurahan_id' => $this->desa_kelurahan_id, + 'default' => $this->default, + 'provinsi' => $this->provinsi ? new ProvinsiResource($this->provinsi) : null, + 'kabupatenKota' => $this->kabupatenKota ? new KabupatenKotaResource($this->kabupatenKota) : null, + 'kecamatan' => $this->kecamatan ? new KecamatanResource($this->kecamatan) : null, + 'desaKelurahan' => $this->desaKelurahan ? new DesaKelurahanResource($this->desaKelurahan) : null, + ]; + } +} diff --git a/app/Http/Resources/Core/ProgramResource.php b/app/Http/Resources/Core/ProgramResource.php new file mode 100644 index 0000000..3b6503f --- /dev/null +++ b/app/Http/Resources/Core/ProgramResource.php @@ -0,0 +1,33 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + 'nama' => $this->nama, + 'sub_domain' => $this->sub_domain, + 'tahun' => $this->tahun, + 'instansi' => $this->instansi, + 'komoditas' => $this->komoditas()->with('komoditas')->get()->map(function ($item) { + return new ProgramKomoditasResource($item); + }), + 'lokasi' => $this->lokasi()->with(['provinsi', 'kabupatenKota', 'kecamatan', 'desaKelurahan'])->get()->map(function ($item) { + return new ProgramLokasiResource($item); + }), + ]; + } +} diff --git a/routes/api.php b/routes/api.php index 679fa03..98562db 100644 --- a/routes/api.php +++ b/routes/api.php @@ -76,4 +76,13 @@ Route::middleware('auth:sanctum') Route::resource('tabel.formulir', App\Http\Controllers\Api\Ekosistem\FormBasisDataController::class)->only(['index']); Route::resource('tabel.data', App\Http\Controllers\Api\Ekosistem\ValueBasisDataController::class); -}); \ No newline at end of file +}); + +Route::middleware('auth:sanctum') + ->name('core.') + ->prefix('core') + ->group(function () { + + Route::resource('program', App\Http\Controllers\Api\Core\ProgramController::class)->only(['index', 'show']); + +}); \ No newline at end of file