Files
econom/app/Http/Controllers/References/DepartmentController.php
brusnitsyn fb2e6c58e3
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.3) (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled
first commit
2026-04-06 00:06:00 +09:00

71 lines
2.3 KiB
PHP

<?php
namespace App\Http\Controllers\References;
use App\Http\Controllers\Controller;
use App\Http\Requests\References\StoreDepartmentProfileRequest;
use App\Http\Requests\References\StoreDepartmentRequest;
use App\Models\Department;
use App\Models\DepartmentProfile;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
class DepartmentController extends Controller
{
/**
* Display the departments reference page.
*/
public function index(Request $request): Response
{
return Inertia::render('references/departments/Index', [
'currentTeam' => $request->user()?->currentTeam
? $request->user()?->toUserTeam($request->user()->currentTeam)
: null,
'departmentProfiles' => DepartmentProfile::query()
->withCount('departments')
->orderBy('name')
->get()
->map(fn (DepartmentProfile $departmentProfile) => [
'id' => $departmentProfile->id,
'name' => $departmentProfile->name,
'departmentsCount' => $departmentProfile->departments_count,
]),
'departments' => Department::query()
->with('departmentProfile:id,name')
->orderBy('name')
->get()
->map(fn (Department $department) => [
'id' => $department->id,
'name' => $department->name,
'isActive' => $department->is_active,
'departmentProfile' => [
'id' => $department->departmentProfile->id,
'name' => $department->departmentProfile->name,
],
]),
]);
}
/**
* Store a newly created department profile.
*/
public function storeProfile(StoreDepartmentProfileRequest $request): RedirectResponse
{
DepartmentProfile::create($request->validated());
return to_route('references.departments.index');
}
/**
* Store a newly created department.
*/
public function store(StoreDepartmentRequest $request): RedirectResponse
{
Department::create($request->validated());
return to_route('references.departments.index');
}
}