Program api
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Core;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use App\Models\Core\Program;
|
||||
use App\Http\Resources\Core\ProgramResource;
|
||||
|
||||
class ProgramController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Request $request): AnonymousResourceCollection
|
||||
{
|
||||
$perPage = $request->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)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Core;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
use App\Http\Resources\Pertanian\KomoditasResource;
|
||||
|
||||
class ProgramKomoditasResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Core;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
use App\Http\Resources\Wilayah\ProvinsiResource;
|
||||
use App\Http\Resources\Wilayah\KabupatenKotaResource;
|
||||
use App\Http\Resources\Wilayah\KecamatanResource;
|
||||
use App\Http\Resources\Wilayah\DesaKelurahanResource;
|
||||
|
||||
class ProgramLokasiResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Core;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use App\Http\Resources\Core\ProgramKomoditasResource;
|
||||
use App\Http\Resources\Core\ProgramLokasiResource;
|
||||
|
||||
class ProgramResource 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,
|
||||
'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);
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -77,3 +77,12 @@ Route::middleware('auth:sanctum')
|
||||
Route::resource('tabel.data', App\Http\Controllers\Api\Ekosistem\ValueBasisDataController::class);
|
||||
|
||||
});
|
||||
|
||||
Route::middleware('auth:sanctum')
|
||||
->name('core.')
|
||||
->prefix('core')
|
||||
->group(function () {
|
||||
|
||||
Route::resource('program', App\Http\Controllers\Api\Core\ProgramController::class)->only(['index', 'show']);
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user