2026-09-04 11:28:59 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Exception\ApiException;
|
|
|
|
|
use App\Repository\CardRepository;
|
2026-09-04 13:37:17 +01:00
|
|
|
use App\Repository\CardStatusRepository;
|
2026-09-04 11:28:59 +01:00
|
|
|
use App\Repository\ProjectRepository;
|
|
|
|
|
use App\Support\Validator;
|
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-04 15:22:33 +01:00
|
|
|
* CRUD for cards. A card either sits in the caller's inbox (no project) or
|
|
|
|
|
* belongs to one of their projects with a status in it; single-card and
|
|
|
|
|
* ordering routes are addressed globally (by card id, or by an explicit
|
|
|
|
|
* project_id/status_id column) since a card need not have a project.
|
2026-09-04 11:28:59 +01:00
|
|
|
*/
|
|
|
|
|
final class CardController extends Controller
|
|
|
|
|
{
|
|
|
|
|
private const TEXT_MAX = 1000;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly ProjectRepository $projects,
|
|
|
|
|
private readonly CardRepository $cards,
|
2026-09-04 13:37:17 +01:00
|
|
|
private readonly CardStatusRepository $statuses,
|
2026-09-04 11:28:59 +01:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GET /api/projects/{projectId}/cards
|
|
|
|
|
*/
|
|
|
|
|
public function index(Request $request, Response $response, array $args): Response
|
|
|
|
|
{
|
|
|
|
|
$projectId = $this->requireOwnedProjectId($request, $args);
|
|
|
|
|
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'cards' => array_map($this->present(...), $this->cards->allForProject($projectId)),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* POST /api/projects/{projectId}/cards
|
2026-09-04 15:22:33 +01:00
|
|
|
*
|
|
|
|
|
* Creates the card directly in the project, in its first status (a
|
|
|
|
|
* project card always has one -- see the invariant on the `cards` table).
|
2026-09-04 11:28:59 +01:00
|
|
|
*/
|
|
|
|
|
public function store(Request $request, Response $response, array $args): Response
|
|
|
|
|
{
|
|
|
|
|
$projectId = $this->requireOwnedProjectId($request, $args);
|
|
|
|
|
|
|
|
|
|
$validator = new Validator($this->body($request));
|
|
|
|
|
$text = $validator->requiredString('text', self::TEXT_MAX);
|
|
|
|
|
$complete = $validator->optionalBool('complete') ?? false;
|
|
|
|
|
$position = $validator->optionalInt('position', 0);
|
|
|
|
|
$validator->assert();
|
|
|
|
|
|
2026-09-04 15:22:33 +01:00
|
|
|
$card = $this->cards->createInProject(
|
|
|
|
|
$this->user($request)['id'],
|
|
|
|
|
$projectId,
|
|
|
|
|
$this->firstStatusId($projectId),
|
|
|
|
|
$text,
|
|
|
|
|
$complete,
|
|
|
|
|
$position,
|
|
|
|
|
);
|
2026-09-04 11:28:59 +01:00
|
|
|
$this->projects->touch($projectId);
|
|
|
|
|
|
|
|
|
|
return $this->json($response, ['card' => $this->present($card)], 201);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-04 15:22:33 +01:00
|
|
|
* GET /api/inbox/cards
|
2026-09-04 11:28:59 +01:00
|
|
|
*/
|
2026-09-04 15:22:33 +01:00
|
|
|
public function inboxIndex(Request $request, Response $response): Response
|
2026-09-04 11:28:59 +01:00
|
|
|
{
|
2026-09-04 15:22:33 +01:00
|
|
|
$cards = $this->cards->allInInbox($this->user($request)['id']);
|
2026-09-04 11:28:59 +01:00
|
|
|
|
2026-09-04 15:22:33 +01:00
|
|
|
return $this->json($response, ['cards' => array_map($this->present(...), $cards)]);
|
2026-09-04 11:28:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-04 15:22:33 +01:00
|
|
|
* POST /api/inbox/cards
|
|
|
|
|
*/
|
|
|
|
|
public function inboxStore(Request $request, Response $response): Response
|
|
|
|
|
{
|
|
|
|
|
$validator = new Validator($this->body($request));
|
|
|
|
|
$text = $validator->requiredString('text', self::TEXT_MAX);
|
|
|
|
|
$complete = $validator->optionalBool('complete') ?? false;
|
|
|
|
|
$position = $validator->optionalInt('position', 0);
|
|
|
|
|
$validator->assert();
|
|
|
|
|
|
|
|
|
|
$card = $this->cards->createInInbox($this->user($request)['id'], $text, $complete, $position);
|
|
|
|
|
|
|
|
|
|
return $this->json($response, ['card' => $this->present($card)], 201);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GET /api/cards/{cardId}
|
|
|
|
|
*/
|
|
|
|
|
public function show(Request $request, Response $response, array $args): Response
|
|
|
|
|
{
|
|
|
|
|
return $this->json($response, ['card' => $this->present($this->requireCard($request, $args))]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* PATCH /api/cards/{cardId}
|
|
|
|
|
*
|
|
|
|
|
* Text and completion only. Moving a card -- into/out of the inbox, or
|
|
|
|
|
* between projects -- goes through PUT /api/cards/order.
|
2026-09-04 11:28:59 +01:00
|
|
|
*/
|
|
|
|
|
public function update(Request $request, Response $response, array $args): Response
|
|
|
|
|
{
|
2026-09-04 15:22:33 +01:00
|
|
|
$card = $this->requireCard($request, $args);
|
2026-09-04 11:28:59 +01:00
|
|
|
|
|
|
|
|
$validator = new Validator($this->body($request));
|
|
|
|
|
$fields = [];
|
|
|
|
|
if ($validator->has('text')) {
|
|
|
|
|
$fields['text'] = $validator->requiredString('text', self::TEXT_MAX);
|
|
|
|
|
}
|
|
|
|
|
if ($validator->has('complete')) {
|
|
|
|
|
$fields['complete'] = $validator->optionalBool('complete');
|
|
|
|
|
}
|
|
|
|
|
if ($fields === [] && !$validator->failed()) {
|
2026-09-04 15:22:33 +01:00
|
|
|
$validator->add('text', 'Provide at least one of: text, complete.');
|
2026-09-04 11:28:59 +01:00
|
|
|
}
|
|
|
|
|
$validator->assert();
|
|
|
|
|
|
2026-09-04 15:22:33 +01:00
|
|
|
$updated = $this->cards->update($card['id'], $fields);
|
|
|
|
|
if ($card['project_id'] !== null) {
|
|
|
|
|
$this->projects->touch($card['project_id']);
|
|
|
|
|
}
|
2026-09-04 11:28:59 +01:00
|
|
|
|
|
|
|
|
return $this->json($response, ['card' => $this->present($updated)]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-04 15:22:33 +01:00
|
|
|
* DELETE /api/cards/{cardId}
|
2026-09-04 11:28:59 +01:00
|
|
|
*/
|
|
|
|
|
public function destroy(Request $request, Response $response, array $args): Response
|
|
|
|
|
{
|
2026-09-04 15:22:33 +01:00
|
|
|
$card = $this->requireCard($request, $args);
|
|
|
|
|
$this->cards->delete($card['id']);
|
|
|
|
|
if ($card['project_id'] !== null) {
|
|
|
|
|
$this->projects->touch($card['project_id']);
|
|
|
|
|
}
|
2026-09-04 11:28:59 +01:00
|
|
|
|
|
|
|
|
return $response->withStatus(204);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-04 15:22:33 +01:00
|
|
|
* PUT /api/cards/order
|
2026-09-04 11:28:59 +01:00
|
|
|
*
|
2026-09-04 15:22:33 +01:00
|
|
|
* Sets the contents and order of one column.
|
2026-09-04 13:37:17 +01:00
|
|
|
*
|
2026-09-04 15:22:33 +01:00
|
|
|
* Body: { "project_id": 5 | null, "status_id": 2 | null, "card_ids": [3, 1, 2] }
|
|
|
|
|
* -- both null for the inbox, or both set to a project owned by the
|
|
|
|
|
* caller and one of its statuses. `card_ids` are the cards that should
|
|
|
|
|
* make up that column, in order; any card moved in from elsewhere
|
|
|
|
|
* (another project, the inbox) is re-parented and its old column
|
|
|
|
|
* re-packed.
|
2026-09-04 11:28:59 +01:00
|
|
|
*/
|
2026-09-04 15:22:33 +01:00
|
|
|
public function reorder(Request $request, Response $response): Response
|
2026-09-04 11:28:59 +01:00
|
|
|
{
|
2026-09-04 15:22:33 +01:00
|
|
|
$ownerId = $this->user($request)['id'];
|
2026-09-04 13:37:17 +01:00
|
|
|
$body = $this->body($request);
|
2026-09-04 11:28:59 +01:00
|
|
|
|
2026-09-04 15:22:33 +01:00
|
|
|
[$projectId, $statusId] = $this->targetColumn($ownerId, $body);
|
2026-09-04 13:37:17 +01:00
|
|
|
|
|
|
|
|
$order = $body['card_ids'] ?? null;
|
2026-09-04 11:28:59 +01:00
|
|
|
if (!is_array($order) || array_filter($order, static fn ($id) => !is_int($id)) !== []) {
|
|
|
|
|
throw new ApiException('card_ids must be an array of card IDs.', 422);
|
|
|
|
|
}
|
|
|
|
|
/** @var int[] $order */
|
2026-09-04 13:37:17 +01:00
|
|
|
if (count($order) !== count(array_unique($order))) {
|
|
|
|
|
throw new ApiException('card_ids must not contain duplicates.', 422);
|
|
|
|
|
}
|
2026-09-04 15:22:33 +01:00
|
|
|
if (array_diff($order, $this->cards->idsOwnedBy($ownerId)) !== []) {
|
|
|
|
|
throw new ApiException('Every card_id must be a card you own.', 422);
|
2026-09-04 13:37:17 +01:00
|
|
|
}
|
2026-09-04 15:22:33 +01:00
|
|
|
if (array_diff($this->cards->idsInColumn($ownerId, $projectId, $statusId), $order) !== []) {
|
2026-09-04 13:37:17 +01:00
|
|
|
throw new ApiException('card_ids must include every card already in this column.', 422);
|
2026-09-04 11:28:59 +01:00
|
|
|
}
|
|
|
|
|
|
2026-09-04 15:22:33 +01:00
|
|
|
$cards = $this->cards->orderColumn($ownerId, $projectId, $statusId, $order);
|
|
|
|
|
|
|
|
|
|
if ($projectId !== null) {
|
|
|
|
|
$this->projects->touch($projectId);
|
|
|
|
|
}
|
2026-09-04 11:28:59 +01:00
|
|
|
|
|
|
|
|
return $this->json($response, ['cards' => array_map($this->present(...), $cards)]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 15:22:33 +01:00
|
|
|
/**
|
|
|
|
|
* Validate and resolve the { project_id, status_id } target column from
|
|
|
|
|
* the request body.
|
|
|
|
|
*
|
|
|
|
|
* @param array<string, mixed> $body
|
|
|
|
|
* @return array{0: int|null, 1: int|null}
|
|
|
|
|
*/
|
|
|
|
|
private function targetColumn(int $ownerId, array $body): array
|
|
|
|
|
{
|
|
|
|
|
$projectId = $body['project_id'] ?? null;
|
|
|
|
|
$statusId = $body['status_id'] ?? null;
|
|
|
|
|
|
|
|
|
|
if ($projectId === null) {
|
|
|
|
|
if ($statusId !== null) {
|
|
|
|
|
throw new ApiException('status_id must be null when project_id is null.', 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [null, null];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is_int($projectId)) {
|
|
|
|
|
throw new ApiException('project_id must be an integer or null.', 422);
|
|
|
|
|
}
|
|
|
|
|
if ($this->projects->findOwnedBy($projectId, $ownerId) === null) {
|
|
|
|
|
throw new ApiException('Project not found.', 404);
|
|
|
|
|
}
|
|
|
|
|
if (!is_int($statusId)) {
|
|
|
|
|
throw new ApiException('status_id is required when project_id is set.', 422);
|
|
|
|
|
}
|
|
|
|
|
if ($this->statuses->findInProject($statusId, $projectId) === null) {
|
|
|
|
|
throw new ApiException('That status does not belong to this project.', 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [$projectId, $statusId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Every project is seeded with statuses on creation; this is never empty. */
|
|
|
|
|
private function firstStatusId(int $projectId): int
|
|
|
|
|
{
|
|
|
|
|
return $this->statuses->allForProject($projectId)[0]['id'];
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 11:28:59 +01:00
|
|
|
/**
|
|
|
|
|
* @param array<string, string> $args
|
|
|
|
|
*/
|
|
|
|
|
private function requireOwnedProjectId(Request $request, array $args): int
|
|
|
|
|
{
|
|
|
|
|
$project = $this->projects->findOwnedBy((int) $args['projectId'], $this->user($request)['id']);
|
|
|
|
|
|
|
|
|
|
if ($project === null) {
|
|
|
|
|
throw new ApiException('Project not found.', 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $project['id'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array<string, string> $args
|
2026-09-04 15:22:33 +01:00
|
|
|
* @return array{id: int, owner_id: int, project_id: int|null, text: string, complete: bool, position: int, status_id: int|null, status: array{id: int, name: string}|null, created_at: string, updated_at: string}
|
2026-09-04 11:28:59 +01:00
|
|
|
*/
|
2026-09-04 15:22:33 +01:00
|
|
|
private function requireCard(Request $request, array $args): array
|
2026-09-04 11:28:59 +01:00
|
|
|
{
|
2026-09-04 15:22:33 +01:00
|
|
|
$card = $this->cards->findOwnedBy((int) $args['cardId'], $this->user($request)['id']);
|
2026-09-04 11:28:59 +01:00
|
|
|
|
|
|
|
|
if ($card === null) {
|
|
|
|
|
throw new ApiException('Card not found.', 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $card;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-04 15:22:33 +01:00
|
|
|
* @param array{id: int, owner_id: int, project_id: int|null, text: string, complete: bool, position: int, status_id: int|null, status: array{id: int, name: string}|null, created_at: string, updated_at: string} $card
|
2026-09-04 11:28:59 +01:00
|
|
|
* @return array<string, mixed>
|
|
|
|
|
*/
|
|
|
|
|
private function present(array $card): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'id' => $card['id'],
|
|
|
|
|
'project_id' => $card['project_id'],
|
|
|
|
|
'text' => $card['text'],
|
|
|
|
|
'complete' => $card['complete'],
|
|
|
|
|
'position' => $card['position'],
|
2026-09-04 13:37:17 +01:00
|
|
|
'status_id' => $card['status_id'],
|
|
|
|
|
'status' => $card['status'],
|
2026-09-04 11:28:59 +01:00
|
|
|
'created_at' => $card['created_at'],
|
|
|
|
|
'updated_at' => $card['updated_at'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|