diff --git a/app/Http/Controllers/Api/Master/DesaKelurahanController.php b/app/Http/Controllers/Api/Master/DesaKelurahanController.php new file mode 100644 index 0000000..04062fb --- /dev/null +++ b/app/Http/Controllers/Api/Master/DesaKelurahanController.php @@ -0,0 +1,107 @@ +integer('size') ?: 10; + + $master = DesaKelurahan::with('kecamatan') + ->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->kecamatan_id, fn($q,$v) => $q->where('kecamatan_id',$v)) + ->when($request->kabupaten_kota_id, fn ($q, $v) => + $q->whereHas('kecamatan', fn ($k) => + $k->where('kabupaten_kota_id', $v) + ) + ) + ->when($request->provinsi_id, fn ($q, $v) => + $q->whereHas('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', 'kode', 'nama']; + + if (in_array($column, $allowed)) { + $master->orderBy($column, $dir); + } + } else { + $master->orderBy('kode', 'asc'); + } + + $masterList = $master->paginate($size); + + return DesaKelurahanResource::collection($masterList); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id): JsonResponse + { + return response()->json(DesaKelurahanResource::make(DesaKelurahan::with('kecamatan')->findOrFail($id))); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(string $id) + { + // + } + + /** + * 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/Controllers/Api/Master/FacilitatorController.php b/app/Http/Controllers/Api/Master/FacilitatorController.php new file mode 100644 index 0000000..c287fc6 --- /dev/null +++ b/app/Http/Controllers/Api/Master/FacilitatorController.php @@ -0,0 +1,123 @@ +get('size') ?: 10; + $query = Facilitator::query(); + + // Fitur Pencarian (berdasarkan nama atau email) + if ($request->has('search')) { + $s = strtolower($request->get('search')); + $query->where(function($q) use ($s) { + $q->whereRaw('lower(full_name) like (?)', ["%{$s}%"]) + ->orWhereRaw('lower(user_email) like (?)', ["%{$s}%"]); + }); + } + + // Fitur Sorting (contoh: ?sort=-full_name) + if ($request->has('sort')) { + $order = $request->get('sort'); + $firstChar = substr($order, 0, 1); + $dir = $firstChar === '-' ? 'desc' : 'asc'; + $field = $firstChar === '-' ? substr($order, 1) : $order; + $query->orderBy($field, $dir); + } + + // Fitur Filter Wilayah + if ($request->has('province')) { + $query->where('province', $request->get('province')); + } + if ($request->has('regency')) { + $query->where('regency', $request->get('regency')); + } + if ($request->has('is_active')) { + $query->where('is_active', $request->boolean('is_active')); + } + + $users = $query->paginate($size); + + return response()->json($users); + } + + /** + * Menyimpan data user detail baru. + */ + public function store(Request $request): JsonResponse + { + + $validated = $request->validate([ + 'user_email' => ['required', 'email', 'unique:facilitator,user_email'], + 'full_name' => ['required', 'string', 'max:255'], + 'role' => ['required', 'string'], + 'village' => ['nullable', 'string'], + 'district' => ['nullable', 'string'], + 'regency' => ['nullable', 'string'], + 'province' => ['nullable', 'string'], + 'phone' => ['nullable', 'string'], + 'is_active' => ['boolean'], + ]); + + $user = Facilitator::create($validated); + + return response()->json($user, 201); + } + + /** + * Menampilkan detail satu user. + */ + public function show(string $id): JsonResponse + { + + $user = Facilitator::findOrFail($id); + return response()->json($user); + } + + /** + * Memperbarui data user detail. + */ + public function update(Request $request, string $id): JsonResponse + { + + $user = Facilitator::findOrFail($id); + + $validated = $request->validate([ + 'user_email' => ['required', 'email', 'unique:facilitator,user_email,' . $id], + 'full_name' => ['required', 'string', 'max:255'], + 'role' => ['required', 'string'], + 'village' => ['nullable', 'string'], + 'district' => ['nullable', 'string'], + 'regency' => ['nullable', 'string'], + 'province' => ['nullable', 'string'], + 'phone' => ['nullable', 'string'], + 'is_active' => ['boolean'], + ]); + + $user->update($validated); + + return response()->json($user); + } + + /** + * Menghapus data user detail. + */ + public function destroy(string $id): JsonResponse + { + + $user = Facilitator::findOrFail($id); + $user->delete(); + + return response()->json(['message' => 'User deleted successfully']); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Api/Master/KabupatenKotaController.php b/app/Http/Controllers/Api/Master/KabupatenKotaController.php new file mode 100644 index 0000000..ec42e86 --- /dev/null +++ b/app/Http/Controllers/Api/Master/KabupatenKotaController.php @@ -0,0 +1,97 @@ +integer('size') ?: 10; + + $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->filled('sort')) { + $dir = str_starts_with($request->sort, '-') ? 'desc' : 'asc'; + $column = ltrim($request->sort, '-'); + + $allowed = ['id', 'kode', 'nama']; + + if (in_array($column, $allowed)) { + $master->orderBy($column, $dir); + } + } else { + $master->orderBy('kode', 'asc'); + } + + $masterList = $master->paginate($size); + + return KabupatenKotaResource::collection($masterList); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id): JsonResponse + { + return response()->json(KabupatenKotaResource::make(KabupatenKota::with('provinsi')->findOrFail($id))); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(string $id) + { + // + } + + /** + * 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/Controllers/Api/Master/KecamatanController.php b/app/Http/Controllers/Api/Master/KecamatanController.php new file mode 100644 index 0000000..2c45798 --- /dev/null +++ b/app/Http/Controllers/Api/Master/KecamatanController.php @@ -0,0 +1,101 @@ +integer('size') ?: 10; + + $master = Kecamatan::with('kabupatenKota') + ->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->kabupaten_kota_id, fn($q,$v) => $q->where('kabupaten_kota_id',$v)) + ->when($request->provinsi_id, fn ($q, $v) => + $q->whereHas('kabupatenKota.provinsi', 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', 'kode', 'nama']; + + if (in_array($column, $allowed)) { + $master->orderBy($column, $dir); + } + } else { + $master->orderBy('kode', 'asc'); + } + + $masterList = $master->paginate($size); + + return KecamatanResource::collection($masterList); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id): JsonResponse + { + return response()->json(KecamatanResource::make(Kecamatan::with('kabupatenKota')->findOrFail($id))); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(string $id) + { + // + } + + /** + * 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/Controllers/Api/Master/KomoditasController.php b/app/Http/Controllers/Api/Master/KomoditasController.php new file mode 100644 index 0000000..36cd2df --- /dev/null +++ b/app/Http/Controllers/Api/Master/KomoditasController.php @@ -0,0 +1,69 @@ +json(KomoditasResource::make(Komoditas::findOrFail($id))); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(string $id) + { + // + } + + /** + * 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/Controllers/Api/Master/ProvinsiController.php b/app/Http/Controllers/Api/Master/ProvinsiController.php new file mode 100644 index 0000000..17a9e24 --- /dev/null +++ b/app/Http/Controllers/Api/Master/ProvinsiController.php @@ -0,0 +1,94 @@ +integer('size') ?: 10; + + $master = 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}%"]); + }); + }); + + if ($request->filled('sort')) { + $dir = str_starts_with($request->sort, '-') ? 'desc' : 'asc'; + $column = ltrim($request->sort, '-'); + + $allowed = ['id', 'kode', 'nama']; + + if (in_array($column, $allowed)) { + $master->orderBy($column, $dir); + } + } else { + $master->orderBy('kode', 'asc'); + } + + $masterList = $master->paginate($size); + + return ProvinsiResource::collection($masterList); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id): JsonResponse + { + return response()->json(ProvinsiResource::make(Provinsi::findOrFail($id))); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +}