facilitator
This commit is contained in:
parent
298c591a20
commit
20a473c2b8
131
app/Http/Controllers/Api/FacilitatorController.php
Normal file
131
app/Http/Controllers/Api/FacilitatorController.php
Normal file
@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Models\Facilitator;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
class FacilitatorController extends Controller
|
||||
{
|
||||
/**
|
||||
* Menampilkan daftar user detail dengan fitur search, sort, dan filter.
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
// Otorisasi khusus kepala_desa sesuai role di JSON
|
||||
Gate::authorize('kepala_desa');
|
||||
|
||||
$size = +$request->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']);
|
||||
}
|
||||
}
|
||||
54
app/Models/Facilitator/Facilitator.php
Normal file
54
app/Models/Facilitator/Facilitator.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Facilitator extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
// Menentukan nama tabel jika tidak jamak (optional)
|
||||
protected $table = 'user_details';
|
||||
|
||||
// Karena ID menggunakan UUID (string), kita nonaktifkan auto-incrementing
|
||||
public $incrementing = false;
|
||||
protected $keyType = 'string';
|
||||
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'user_email',
|
||||
'full_name',
|
||||
'role',
|
||||
'village',
|
||||
'district',
|
||||
'regency',
|
||||
'province',
|
||||
'phone',
|
||||
'is_active',
|
||||
'created_date',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => '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
|
||||
}
|
||||
@ -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);
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user