diff --git a/app/Http/Controllers/Api/FacilitatorController.php b/app/Http/Controllers/Api/FacilitatorController.php new file mode 100644 index 0000000..27309e7 --- /dev/null +++ b/app/Http/Controllers/Api/FacilitatorController.php @@ -0,0 +1,131 @@ +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 + { + Gate::authorize('kepala_desa'); + + $validated = $request->validate([ + 'user_email' => ['required', 'email', 'unique:user_details,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 + { + Gate::authorize('kepala_desa'); + + $user = Facilitator::findOrFail($id); + return response()->json($user); + } + + /** + * Memperbarui data user detail. + */ + public function update(Request $request, string $id): JsonResponse + { + Gate::authorize('kepala_desa'); + + $user = Facilitator::findOrFail($id); + + $validated = $request->validate([ + 'user_email' => ['required', 'email', 'unique:user_details,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 + { + Gate::authorize('kepala_desa'); + + $user = Facilitator::findOrFail($id); + $user->delete(); + + return response()->json(['message' => 'User deleted successfully']); + } +} \ No newline at end of file diff --git a/app/Models/Facilitator/Facilitator.php b/app/Models/Facilitator/Facilitator.php new file mode 100644 index 0000000..3ae8ff5 --- /dev/null +++ b/app/Models/Facilitator/Facilitator.php @@ -0,0 +1,54 @@ + 'string', + 'is_active' => 'boolean', + 'created_date' => 'datetime', + ]; + + // Boot method untuk otomatis mengisi UUID saat create + protected static function boot() + { + parent::boot(); + static::creating(function ($model) { + if (empty($model->id)) { + $model->id = (string) Str::uuid(); + } + }); + } + + // Jika ingin menggunakan created_date dari JSON sebagai pengganti created_at Laravel + const CREATED_AT = 'created_date'; + const UPDATED_AT = null; // Matikan jika tidak ada kolom updated_at +} \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index b33212b..05f8ce1 100644 --- a/routes/api.php +++ b/routes/api.php @@ -56,3 +56,10 @@ Route::apiResource('/kecamatan', \App\Http\Controllers\Api\KecamatanController::class)->only(['index', 'show']); Route::apiResource('/desa-kelurahan', \App\Http\Controllers\Api\DesaKelurahanController::class)->only(['index', 'show']); }); +Route::middleware('auth:sanctum') + ->name('manajemen.') + ->prefix('manajemen') + ->group(function () { + + Route::apiResource('fasilitator', \App\Http\Controllers\Api\KomoditasController::class); +}); \ No newline at end of file