dstpabuaran.com/app/Http/Controllers/Admin/HR/EmployeeController.php

84 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers\Admin\HR;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\HR\EmployeeRequest;
use App\Models\User;
use App\Services\Admin\HR\EmployeeService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;
class EmployeeController extends Controller
{
public function __construct(
private EmployeeService $service
) {}
public function index(): Response
{
return Inertia::render('admin/hr/employee/index', [
'employees' => $this->service->getAll(),
]);
}
public function create(): Response
{
return Inertia::render('admin/hr/employee/create');
}
public function store(EmployeeRequest $request): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->create($request->validated()),
'Pegawai berhasil ditambahkan.',
'admin.hr.employees.index'
);
}
public function edit(User $employee): Response
{
return Inertia::render('admin/hr/employee/edit', [
'employee' => $this->service->getById($employee->id),
]);
}
public function update(EmployeeRequest $request, User $employee): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->update($employee, $request->validated()),
'Pegawai berhasil diperbarui.',
'admin.hr.employees.index'
);
}
public function destroy(User $employee): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->delete($employee),
'Pegawai berhasil dihapus.',
'admin.hr.employees.index'
);
}
public function toggleActive(User $employee): RedirectResponse
{
$this->service->toggleActive($employee);
$status = $employee->fresh()->is_active ? 'diaktifkan' : 'dinonaktifkan';
Inertia::flash('toast', ['type' => 'success', 'message' => "Pegawai berhasil {$status}."]);
return to_route('admin.hr.employees.index');
}
public function resetPassword(User $employee): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->resetPassword($employee),
'Kata sandi pegawai berhasil direset ke kata sandi default.',
'admin.hr.employees.index'
);
}
}