Files
landing-api/app/Http/Controllers/Api/V1/ProjectController.php
Josep Piguillem 82ef2b38dd
Some checks failed
Tests / lint (push) Has been cancelled
Tests / test (push) Has been cancelled
Tests / test-postgres (push) Has been cancelled
Tests / mcp (push) Has been cancelled
Manage projects, members and services through the API
/api/v1/projects, /members and /services: create, list, show, update,
delete and restore, with the panel's own rules — so the MCP server can
load the collective's work, people and offer without anyone retyping
them into forms. The MCP server picks the tools up from openapi.yaml.

- Relations travel as ids and are replaced whole when sent, left alone
  when not: a project's technology_ids, service_ids and members with
  each one's role; a member's technology_ids; a service's
  technology_ids and project_ids. Record and relations are saved in
  one transaction.
- Publishing stays a permission of its own. Without projects.publish
  or services.publish, published_at, unpublish_at and is_published are
  refused — the fields the panel disables for the same people.
- MemberPolicy still lets someone edit their own profile.
- Uploads accept `for: projects` and `for: members`, landing where the
  panel's own upload fields put them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 16:07:20 +02:00

123 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api\V1;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\ProjectRequest;
use App\Http\Resources\ProjectResource;
use App\Models\Project;
use App\Support\TranslatablePayload;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Support\Facades\DB;
/**
* Projects, as the panel edits them — so the MCP server can load the
* collective's work without anyone copying it into a form by hand.
*/
class ProjectController extends Controller
{
/** Relations every answer carries, so a client can PATCH back what it read. */
protected const WITH = ['coverMedia', 'technologies', 'services', 'members'];
/**
* Filters: status, search (title in the active language), trashed.
*/
public function index(Request $request): AnonymousResourceCollection
{
$this->authorize('viewAny', Project::class);
$locale = app()->getLocale();
$projects = Project::query()
->with(self::WITH)
->when($request->filled('status'), fn ($q) => $q->where('status', $request->string('status')))
->when($request->filled('search'), fn ($q) => $q->where("title->{$locale}", 'like', '%'.$request->string('search').'%'))
->when($request->boolean('trashed'), fn ($q) => $q->onlyTrashed())
->orderBy('position')
->orderByDesc('created_at')
->paginate(perPage: min($request->integer('per_page', 25), 100))
->withQueryString();
return ProjectResource::collection($projects);
}
public function store(ProjectRequest $request): JsonResponse
{
$this->authorize('create', Project::class);
$project = $this->save(new Project, $request);
return ProjectResource::make($project)->response()->setStatusCode(201);
}
public function show(Project $project): ProjectResource
{
$this->authorize('view', $project);
return ProjectResource::make($project->load(self::WITH));
}
public function update(ProjectRequest $request, Project $project): ProjectResource
{
$this->authorize('update', $project);
return ProjectResource::make($this->save($project, $request));
}
public function destroy(Project $project): JsonResponse
{
$this->authorize('delete', $project);
$project->delete();
return response()->json(status: 204);
}
public function restore(Project $project): ProjectResource
{
$this->authorize('restore', $project);
$project->restore();
return ProjectResource::make($project->load(self::WITH));
}
/**
* The record and its relations in one transaction: a project that saved
* but lost its team halfway would be worse than one that did not save.
*/
protected function save(Project $project, ProjectRequest $request): Project
{
$data = $request->validated();
DB::transaction(function () use ($project, $data): void {
$project->fill(TranslatablePayload::merge(
$project,
collect($data)->except(['technology_ids', 'service_ids', 'members'])->all(),
$project->translatable,
));
$project->save();
if (array_key_exists('technology_ids', $data)) {
$project->technologies()->sync($data['technology_ids']);
}
if (array_key_exists('service_ids', $data)) {
$project->services()->sync($data['service_ids']);
}
if (array_key_exists('members', $data)) {
$project->members()->sync(collect($data['members'])->mapWithKeys(
fn (array $member) => [$member['id'] => ['role' => $member['role'] ?? null]],
)->all());
}
});
return $project->load(self::WITH);
}
}