188 lines
6.2 KiB
PHP
188 lines
6.2 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Exception\ApiException;
|
||
|
|
use App\Repository\CardRepository;
|
||
|
|
use App\Repository\ProjectRepository;
|
||
|
|
use App\Support\Validator;
|
||
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
||
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* CRUD for the cards within one project. Every route first checks that the
|
||
|
|
* parent project is owned by the authenticated user; otherwise it responds 404.
|
||
|
|
*/
|
||
|
|
final class CardController extends Controller
|
||
|
|
{
|
||
|
|
private const TEXT_MAX = 1000;
|
||
|
|
|
||
|
|
public function __construct(
|
||
|
|
private readonly ProjectRepository $projects,
|
||
|
|
private readonly CardRepository $cards,
|
||
|
|
) {
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 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
|
||
|
|
*/
|
||
|
|
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();
|
||
|
|
|
||
|
|
$card = $this->cards->create($projectId, $text, $complete, $position);
|
||
|
|
$this->projects->touch($projectId);
|
||
|
|
|
||
|
|
return $this->json($response, ['card' => $this->present($card)], 201);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* GET /api/projects/{projectId}/cards/{cardId}
|
||
|
|
*/
|
||
|
|
public function show(Request $request, Response $response, array $args): Response
|
||
|
|
{
|
||
|
|
$projectId = $this->requireOwnedProjectId($request, $args);
|
||
|
|
|
||
|
|
return $this->json($response, ['card' => $this->present($this->requireCard($projectId, $args))]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* PATCH /api/projects/{projectId}/cards/{cardId}
|
||
|
|
*/
|
||
|
|
public function update(Request $request, Response $response, array $args): Response
|
||
|
|
{
|
||
|
|
$projectId = $this->requireOwnedProjectId($request, $args);
|
||
|
|
$card = $this->requireCard($projectId, $args);
|
||
|
|
|
||
|
|
$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 ($validator->has('position')) {
|
||
|
|
$fields['position'] = $validator->optionalInt('position', 0);
|
||
|
|
}
|
||
|
|
if ($fields === [] && !$validator->failed()) {
|
||
|
|
$validator->add('text', 'Provide at least one of: text, complete, position.');
|
||
|
|
}
|
||
|
|
$validator->assert();
|
||
|
|
|
||
|
|
$updated = $this->cards->update($card['id'], $projectId, $fields);
|
||
|
|
$this->projects->touch($projectId);
|
||
|
|
|
||
|
|
return $this->json($response, ['card' => $this->present($updated)]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* DELETE /api/projects/{projectId}/cards/{cardId}
|
||
|
|
*/
|
||
|
|
public function destroy(Request $request, Response $response, array $args): Response
|
||
|
|
{
|
||
|
|
$projectId = $this->requireOwnedProjectId($request, $args);
|
||
|
|
$this->cards->delete($this->requireCard($projectId, $args)['id']);
|
||
|
|
$this->projects->touch($projectId);
|
||
|
|
|
||
|
|
return $response->withStatus(204);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* PUT /api/projects/{projectId}/cards/order
|
||
|
|
*
|
||
|
|
* Body: { "card_ids": [3, 1, 2] } — every card in the project, exactly once,
|
||
|
|
* in the desired order. Positions are rewritten to 0..n-1.
|
||
|
|
*/
|
||
|
|
public function reorder(Request $request, Response $response, array $args): Response
|
||
|
|
{
|
||
|
|
$projectId = $this->requireOwnedProjectId($request, $args);
|
||
|
|
|
||
|
|
$order = $this->body($request)['card_ids'] ?? null;
|
||
|
|
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 */
|
||
|
|
$expected = $this->cards->idsForProject($projectId);
|
||
|
|
$given = $order;
|
||
|
|
sort($given);
|
||
|
|
sort($expected);
|
||
|
|
|
||
|
|
if ($given !== $expected) {
|
||
|
|
throw new ApiException('card_ids must contain every card in the project exactly once.', 422);
|
||
|
|
}
|
||
|
|
|
||
|
|
$cards = $this->cards->reorder($projectId, $order);
|
||
|
|
$this->projects->touch($projectId);
|
||
|
|
|
||
|
|
return $this->json($response, ['cards' => array_map($this->present(...), $cards)]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @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
|
||
|
|
* @return array{id: int, project_id: int, text: string, complete: bool, position: int, created_at: string, updated_at: string}
|
||
|
|
*/
|
||
|
|
private function requireCard(int $projectId, array $args): array
|
||
|
|
{
|
||
|
|
$card = $this->cards->findInProject((int) $args['cardId'], $projectId);
|
||
|
|
|
||
|
|
if ($card === null) {
|
||
|
|
throw new ApiException('Card not found.', 404);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $card;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array{id: int, project_id: int, text: string, complete: bool, position: int, created_at: string, updated_at: string} $card
|
||
|
|
* @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'],
|
||
|
|
'created_at' => $card['created_at'],
|
||
|
|
'updated_at' => $card['updated_at'],
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|