testapi/app/Http/Controllers/ProjectController.php

172 lines
5.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Inertia\Inertia;
use App\Models\Project;
use App\Models\SmartdokProfile;
use App\Models\Department;
class ProjectController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request) {
$projects = Project::with(['profiles', 'departments'])
->orderBy('project_name')
->paginate(10)
->through(fn($project) => [
'id' => $project->id,
'project_name' => $project->project_name,
'project_number' => $project->project_number,
'user_ids' => $project->profiles->pluck('id'),
'department_ids' => $project->departments->pluck('id'),
]);
return Inertia::render('Project/Index', [
'projects' => $projects,
'filters' => $request->only(['page']),
]);
}
/**
* Show the form for creating a new resource.
*/
public function create() {
$profiles = SmartdokProfile::orderBy('name')
->get()
->map(fn($p) => ['id' => $p->id, 'name' => $p->name]);
$departments = Department::orderBy('name')
->get()
->map(fn($d) => ['id' => $d->id, 'name' => $d->name]);
return Inertia::render('Project/Create', [
'profiles' => $profiles,
'departments' => $departments,
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request) {
$data = $request->validate([
'project_name' => 'required|string|max:255',
'project_number' => 'required|string|max:255|unique:projects,project_number',
'user_ids' => 'sometimes|array',
'user_ids.*' => 'uuid|exists:smartdok_profiles,id',
'department_ids' => 'sometimes|array',
'department_ids.*' => 'integer|exists:departments,id',
]);
$project = Project::create([
'project_name' => $data['project_name'],
'project_number' => $data['project_number'],
]);
$project->profiles()->sync($data['user_ids'] ?? []);
$project->departments()->sync($data['department_ids'] ?? []);
return redirect()
->route('smartdok.projects.index')
->with('success', 'Project created successfully.');
}
/**
* Display the specified resource.
*/
public function show(string $id) {
$project = Project::with(['profiles', 'departments'])->findOrFail($id);
return Inertia::render('Project/Show', [
'project' => [
'id' => $project->id,
'project_name' => $project->project_name,
'project_number' => $project->project_number,
'profiles' => $project->profiles->map(fn ($p) => [
'id' => $p->id,
'username' => $p->username,
'name' => $p->name,
]),
'departments' => $project->departments->map(fn ($d) => [
'id' => $d->id,
'name' => $d->name,
]),
],
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id) {
$project = Project::with(['profiles', 'departments'])
->findOrFail($id);
$profiles = SmartdokProfile::orderBy('name')
->get()
->map(fn($p) => ['id' => $p->id, 'name' => $p->name]);
$departments = Department::orderBy('name')
->get()
->map(fn($d) => ['id' => $d->id, 'name' => $d->name]);
return Inertia::render('Project/Edit', [
'project' => [
'id' => $project->id,
'project_name' => $project->project_name,
'project_number' => $project->project_number,
// ensure these are plain arrays, not Collections
'user_ids' => $project->profiles->pluck('id')->all(),
'department_ids' => $project->departments->pluck('id')->all(),
],
'profiles' => $profiles,
'departments' => $departments,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id) {
$project = Project::findOrFail($id);
$data = $request->validate([
'project_name' => 'required|string|max:255',
'project_number' => 'required|string|max:255|unique:projects,project_number,' . $project->id,
'user_ids' => 'sometimes|array',
'user_ids.*' => 'uuid|exists:smartdok_profiles,id',
'department_ids' => 'sometimes|array',
'department_ids.*' => 'integer|exists:departments,id',
]);
$project->update([
'project_name' => $data['project_name'],
'project_number' => $data['project_number'],
]);
$project->profiles()->sync($data['user_ids'] ?? []);
$project->departments()->sync($data['department_ids'] ?? []);
return redirect()
->route('smartdok.projects.index')
->with('success', 'Project updated successfully.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id) {
$project = Project::findOrFail($id);
$project->delete();
return redirect()
->route('smartdok.projects.index')
->with('success', 'Project deleted successfully.');
}
}