Add per-project card statuses and a kanban board

Statuses
- Migration 006: card_statuses table (project-scoped) and cards.status_id, a
  nullable FK with ON DELETE SET NULL. Every new project is seeded with
  "To do" / "Doing" / "Done"; GET /api/projects/{id}/statuses lists them.
- New cards have no status -- they sit in an "inbox" until moved.

Project view
- Full-width and tabbed: "All tasks" (a flat list, sorted by name
  case-insensitively) and "Kanban" (Inbox plus one column per status).
- Drag a card within or between columns to reorder / restatus; the Inbox
  column has its own name + Add form.

Ordering
- Migration 007: `position` is now a dense 0..n-1 rank within a
  (project_id, status_id) column, not a project-wide order. New composite
  index idx_cards_project_status_position; existing rows re-ranked.
- PUT /api/projects/{id}/cards/order takes { status_id, card_ids } and sets one
  column's contents and order, re-parenting moved-in cards and re-packing their
  source column in a single transaction. PATCH status_id appends the card to the
  end of the destination column.

58 phpunit tests pass; the frontend type-checks and builds.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-04 13:37:17 +01:00
co-authored by Claude Sonnet 5
parent d9db4a3a30
commit c47c800d01
21 changed files with 1389 additions and 239 deletions
+169 -28
View File
@@ -9,24 +9,41 @@ use PDO;
/**
* Data access for the `cards` table.
*
* `position` is a dense 0..n-1 rank *within a column* — the cards sharing a
* (project_id, status_id). The inbox is the column where status_id IS NULL.
*
* @phpstan-type CardStatus array{id: int, name: string}
* @phpstan-type CardRow array{
* id: int, project_id: int, text: string, complete: bool, position: int,
* status_id: int|null, status: CardStatus|null,
* created_at: string, updated_at: string
* }
*/
final class CardRepository
{
private const SELECT = <<<'SQL'
SELECT c.id, c.project_id, c.text, c.complete, c.position, c.status_id,
c.created_at, c.updated_at,
s.name AS status_name
FROM cards c
LEFT JOIN card_statuses s ON s.id = c.status_id
SQL;
public function __construct(private readonly PDO $pdo)
{
}
/**
* Every card in the project, grouped by column (inbox first) and ordered by
* position within each. Consumers that want a different order (e.g. the
* alphabetical "all tasks" list) re-sort client-side.
*
* @return CardRow[]
*/
public function allForProject(int $projectId): array
{
$stmt = $this->pdo->prepare(
'SELECT * FROM cards WHERE project_id = :project ORDER BY position ASC, id ASC'
self::SELECT . ' WHERE c.project_id = :project ORDER BY c.status_id, c.position ASC, c.id ASC'
);
$stmt->execute(['project' => $projectId]);
@@ -38,7 +55,7 @@ final class CardRepository
*/
public function findInProject(int $id, int $projectId): ?array
{
$stmt = $this->pdo->prepare('SELECT * FROM cards WHERE id = :id AND project_id = :project');
$stmt = $this->pdo->prepare(self::SELECT . ' WHERE c.id = :id AND c.project_id = :project');
$stmt->execute(['id' => $id, 'project' => $projectId]);
$row = $stmt->fetch();
@@ -51,7 +68,8 @@ final class CardRepository
*/
public function create(int $projectId, string $text, bool $complete, ?int $position): array
{
$position ??= $this->nextPosition($projectId);
// A new card has no status — it goes to the end of the inbox column.
$position ??= $this->nextPositionInColumn($projectId, null);
$stmt = $this->pdo->prepare(
'INSERT INTO cards (project_id, text, complete, position)
@@ -71,12 +89,22 @@ final class CardRepository
}
/**
* @param array{text?: string, complete?: bool, position?: int} $fields
* Update simple fields, and/or move the card to another column. A status
* change drops the card at the end of the destination column and re-packs
* the one it left. Precise slotting within a column is done via orderColumn().
*
* @param array{text?: string, complete?: bool, status_id?: int|null} $fields
* @return CardRow
*/
public function update(int $id, int $projectId, array $fields): array
{
$sets = ['updated_at = ' . "strftime('%Y-%m-%dT%H:%M:%SZ', 'now')"];
/** @var CardRow $card */
$card = $this->findInProject($id, $projectId);
$movesColumn = array_key_exists('status_id', $fields) && $fields['status_id'] !== $card['status_id'];
$sourceColumn = $card['status_id'];
$sets = ['updated_at = ' . $this->nowExpr()];
$params = ['id' => $id];
if (array_key_exists('text', $fields)) {
@@ -87,18 +115,40 @@ final class CardRepository
$sets[] = 'complete = :complete';
$params['complete'] = $fields['complete'] ? 1 : 0;
}
if (array_key_exists('position', $fields)) {
if (array_key_exists('status_id', $fields)) {
$sets[] = 'status_id = :status_id';
$params['status_id'] = $fields['status_id'];
}
if ($movesColumn) {
$sets[] = 'position = :position';
$params['position'] = $fields['position'];
$params['position'] = $this->nextPositionInColumn($projectId, $fields['status_id']);
}
$stmt = $this->pdo->prepare('UPDATE cards SET ' . implode(', ', $sets) . ' WHERE id = :id');
$stmt->execute($params);
$sql = 'UPDATE cards SET ' . implode(', ', $sets) . ' WHERE id = :id';
/** @var CardRow $card */
$card = $this->findInProject($id, $projectId);
if (!$movesColumn) {
$this->pdo->prepare($sql)->execute($params);
return $card;
/** @var CardRow $updated */
$updated = $this->findInProject($id, $projectId);
return $updated;
}
$this->pdo->beginTransaction();
try {
$this->pdo->prepare($sql)->execute($params);
$this->repack($projectId, $sourceColumn);
$this->pdo->commit();
} catch (\Throwable $e) {
$this->pdo->rollBack();
throw $e;
}
/** @var CardRow $updated */
$updated = $this->findInProject($id, $projectId);
return $updated;
}
public function delete(int $id): void
@@ -107,39 +157,69 @@ final class CardRepository
}
/**
* IDs of every card in the project, in current position order.
* IDs of every card in the project.
*
* @return int[]
*/
public function idsForProject(int $projectId): array
{
$stmt = $this->pdo->prepare(
'SELECT id FROM cards WHERE project_id = :project ORDER BY position ASC, id ASC'
);
$stmt = $this->pdo->prepare('SELECT id FROM cards WHERE project_id = :project');
$stmt->execute(['project' => $projectId]);
return array_map(intval(...), $stmt->fetchAll(PDO::FETCH_COLUMN));
}
/**
* Assign positions 0..n-1 to the given cards in one transaction.
* IDs of the cards currently in one column, in position order.
*
* @param int[] $orderedIds every card id in the project, exactly once
* @return CardRow[] the project's cards in their new order
* @return int[]
*/
public function reorder(int $projectId, array $orderedIds): array
public function idsInColumn(int $projectId, ?int $statusId): array
{
[$match, $params] = $this->columnMatch($statusId);
$stmt = $this->pdo->prepare(
'UPDATE cards SET position = :position,
updated_at = ' . "strftime('%Y-%m-%dT%H:%M:%SZ', 'now')" . '
WHERE id = :id AND project_id = :project'
"SELECT id FROM cards WHERE project_id = :project AND {$match} ORDER BY position ASC, id ASC"
);
$stmt->execute(['project' => $projectId] + $params);
return array_map(intval(...), $stmt->fetchAll(PDO::FETCH_COLUMN));
}
/**
* Set the contents and order of one column. Every id in $orderedIds is moved
* into $statusId at positions 0..n-1; any other column those cards came from
* is re-packed. One transaction.
*
* @param int[] $orderedIds
* @return CardRow[] the project's cards, grouped by column
*/
public function orderColumn(int $projectId, ?int $statusId, array $orderedIds): array
{
$orderedIds = array_values($orderedIds);
$this->pdo->beginTransaction();
try {
foreach (array_values($orderedIds) as $position => $id) {
$stmt->execute(['position' => $position, 'id' => $id, 'project' => $projectId]);
$sourceColumns = $this->columnsOf($projectId, $orderedIds);
$place = $this->pdo->prepare(
'UPDATE cards SET status_id = :status, position = :position, updated_at = ' . $this->nowExpr() . '
WHERE id = :id AND project_id = :project'
);
foreach ($orderedIds as $position => $id) {
$place->execute([
'status' => $statusId,
'position' => $position,
'id' => $id,
'project' => $projectId,
]);
}
foreach ($sourceColumns as $source) {
if ($source !== $statusId) {
$this->repack($projectId, $source);
}
}
$this->pdo->commit();
} catch (\Throwable $e) {
$this->pdo->rollBack();
@@ -149,16 +229,70 @@ final class CardRepository
return $this->allForProject($projectId);
}
private function nextPosition(int $projectId): int
/**
* The distinct status_id values (columns) the given cards currently sit in.
*
* @param int[] $ids
* @return array<int, int|null>
*/
private function columnsOf(int $projectId, array $ids): array
{
if ($ids === []) {
return [];
}
$placeholders = implode(',', array_fill(0, count($ids), '?'));
$stmt = $this->pdo->prepare(
'SELECT COALESCE(MAX(position), -1) + 1 FROM cards WHERE project_id = :project'
"SELECT DISTINCT status_id FROM cards WHERE project_id = ? AND id IN ($placeholders)"
);
$stmt->execute(['project' => $projectId]);
$stmt->execute([$projectId, ...$ids]);
return array_map(
static fn ($value) => $value === null ? null : (int) $value,
$stmt->fetchAll(PDO::FETCH_COLUMN),
);
}
/** Rewrite one column's positions to a dense 0..n-1 in current order. */
private function repack(int $projectId, ?int $statusId): void
{
$ids = $this->idsInColumn($projectId, $statusId);
$update = $this->pdo->prepare('UPDATE cards SET position = :position WHERE id = :id');
foreach ($ids as $position => $id) {
$update->execute(['position' => $position, 'id' => $id]);
}
}
private function nextPositionInColumn(int $projectId, ?int $statusId): int
{
[$match, $params] = $this->columnMatch($statusId);
$stmt = $this->pdo->prepare(
"SELECT COALESCE(MAX(position), -1) + 1 FROM cards WHERE project_id = :project AND {$match}"
);
$stmt->execute(['project' => $projectId] + $params);
return (int) $stmt->fetchColumn();
}
/**
* A WHERE fragment matching one column, since SQLite needs `IS NULL` (not
* `= NULL`) for the inbox.
*
* @return array{0: string, 1: array<string, int>}
*/
private function columnMatch(?int $statusId): array
{
return $statusId === null
? ['status_id IS NULL', []]
: ['status_id = :status', ['status' => $statusId]];
}
private function nowExpr(): string
{
return "strftime('%Y-%m-%dT%H:%M:%SZ', 'now')";
}
/**
* @param array<string, mixed> $row
* @return CardRow
@@ -170,6 +304,13 @@ final class CardRepository
$row['complete'] = (bool) $row['complete'];
$row['position'] = (int) $row['position'];
$statusId = $row['status_id'] === null ? null : (int) $row['status_id'];
$row['status_id'] = $statusId;
$row['status'] = $statusId === null
? null
: ['id' => $statusId, 'name' => (string) $row['status_name']];
unset($row['status_name']);
/** @var CardRow $row */
return $row;
}