- Implemented employee creation and editing forms with validation. - Added employee listing with actions for edit, delete, and reset password. - Integrated toggle for employee active status. - Created reusable UI components for forms, alerts, and data tables. - Updated routing to include employee management endpoints.
120 lines
4.3 KiB
PHP
120 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\SDM;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\SDM\EmployeeRequest;
|
|
use App\Models\User;
|
|
use App\Services\Admin\SDM\EmployeeService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class EmployeeController extends Controller
|
|
{
|
|
public function __construct(
|
|
private EmployeeService $service
|
|
) {}
|
|
|
|
public function index(): Response
|
|
{
|
|
return Inertia::render('admin/sdm/employee/index', [
|
|
'employees' => $this->service->getAll(),
|
|
]);
|
|
}
|
|
|
|
public function create(): Response
|
|
{
|
|
return Inertia::render('admin/sdm/employee/create');
|
|
}
|
|
|
|
public function store(EmployeeRequest $request): RedirectResponse
|
|
{
|
|
try {
|
|
$this->service->create($request->validated());
|
|
|
|
Inertia::flash('toast', ['type' => 'success', 'message' => 'Pegawai berhasil ditambahkan.']);
|
|
} catch (ValidationException $e) {
|
|
$firstError = collect($e->errors())->flatten()->first();
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']);
|
|
} catch (\Exception $e) {
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
|
|
return to_route('admin.sdm.employees.index');
|
|
}
|
|
|
|
public function edit(User $employee): Response
|
|
{
|
|
return Inertia::render('admin/sdm/employee/edit', [
|
|
'employee' => $this->service->getById($employee->id),
|
|
]);
|
|
}
|
|
|
|
public function update(EmployeeRequest $request, User $employee): RedirectResponse
|
|
{
|
|
try {
|
|
$this->service->update($employee, $request->validated());
|
|
|
|
Inertia::flash('toast', ['type' => 'success', 'message' => 'Pegawai berhasil diperbarui.']);
|
|
} catch (ValidationException $e) {
|
|
$firstError = collect($e->errors())->flatten()->first();
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']);
|
|
} catch (\Exception $e) {
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
|
|
return to_route('admin.sdm.employees.index');
|
|
}
|
|
|
|
public function destroy(User $employee): RedirectResponse
|
|
{
|
|
try {
|
|
$this->service->delete($employee);
|
|
|
|
Inertia::flash('toast', ['type' => 'success', 'message' => 'Pegawai berhasil dihapus.']);
|
|
} catch (ValidationException $e) {
|
|
$firstError = collect($e->errors())->flatten()->first();
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']);
|
|
} catch (\Exception $e) {
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
|
|
return to_route('admin.sdm.employees.index');
|
|
}
|
|
|
|
public function toggleActive(User $employee): RedirectResponse
|
|
{
|
|
try {
|
|
$this->service->toggleActive($employee);
|
|
|
|
$status = $employee->fresh()->is_active ? 'diaktifkan' : 'dinonaktifkan';
|
|
Inertia::flash('toast', ['type' => 'success', 'message' => "Pegawai berhasil {$status}."]);
|
|
} catch (ValidationException $e) {
|
|
$firstError = collect($e->errors())->flatten()->first();
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']);
|
|
} catch (\Exception $e) {
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
|
|
return to_route('admin.sdm.employees.index');
|
|
}
|
|
|
|
public function resetPassword(User $employee): RedirectResponse
|
|
{
|
|
try {
|
|
$this->service->resetPassword($employee);
|
|
|
|
Inertia::flash('toast', ['type' => 'success', 'message' => 'Kata sandi pegawai berhasil direset ke kata sandi default.']);
|
|
} catch (ValidationException $e) {
|
|
$firstError = collect($e->errors())->flatten()->first();
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']);
|
|
} catch (\Exception $e) {
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
|
|
return to_route('admin.sdm.employees.index');
|
|
}
|
|
}
|