Files
prn2026/app/Http/Controllers/Admin/Management/RepresentativeController.php
2026-06-03 08:51:22 +08:00

59 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers\Admin\Management;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Management\DeleteRepresentativeRequest;
use App\Http\Requests\Admin\Management\StoreRepresentativeRequest;
use App\Models\Election;
use App\Models\JkmRepresentative;
use App\Models\KkmRepresentative;
use App\Models\PoliceEscort;
use App\Models\PusatMengundi;
use App\Models\SaluranMengundi;
use App\Services\Admin\Management\AdminRepresentativeService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class RepresentativeController extends Controller
{
public function index(): View
{
return view('admin.management.representatives.index', [
'policeEscorts' => PoliceEscort::query()->with(['election', 'pusatMengundi', 'saluranMengundi'])->orderBy('name')->paginate(10, ['*'], 'police_page'),
'kkmRepresentatives' => KkmRepresentative::query()->with(['election', 'pusatMengundi'])->orderBy('name')->paginate(10, ['*'], 'kkm_page'),
'jkmRepresentatives' => JkmRepresentative::query()->with(['election', 'pusatMengundi'])->orderBy('name')->paginate(10, ['*'], 'jkm_page'),
'elections' => Election::query()->orderByDesc('id')->get(),
'pusats' => PusatMengundi::query()->orderBy('name')->get(),
'salurans' => SaluranMengundi::query()->with('pusatMengundi')->orderBy('number')->get(),
]);
}
public function store(StoreRepresentativeRequest $request, AdminRepresentativeService $service): RedirectResponse
{
$service->store($request->user(), $request->validated());
return redirect()->route('admin.management.representatives.index')
->with('status', 'Wakil agensi telah disimpan.');
}
public function destroy(DeleteRepresentativeRequest $request, string $type, int $id, AdminRepresentativeService $service): RedirectResponse
{
$service->delete($this->resolveRepresentative($type, $id), $request->user(), $request->validated('note'));
return redirect()->route('admin.management.representatives.index')
->with('status', 'Wakil agensi telah dipadam.');
}
private function resolveRepresentative(string $type, int $id): Model
{
return match ($type) {
'police' => PoliceEscort::query()->findOrFail($id),
'kkm' => KkmRepresentative::query()->findOrFail($id),
'jkm' => JkmRepresentative::query()->findOrFail($id),
default => abort(404),
};
}
}