2026-09-03 18:30:52 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Repository;
|
|
|
|
|
|
|
|
|
|
use PDO;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-04 11:28:59 +01:00
|
|
|
* Data access for the `cards` table.
|
2026-09-03 18:30:52 +01:00
|
|
|
*
|
2026-09-04 13:37:17 +01:00
|
|
|
* `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}
|
2026-09-04 11:28:59 +01:00
|
|
|
* @phpstan-type CardRow array{
|
|
|
|
|
* id: int, project_id: int, text: string, complete: bool, position: int,
|
2026-09-04 13:37:17 +01:00
|
|
|
* status_id: int|null, status: CardStatus|null,
|
2026-09-03 18:30:52 +01:00
|
|
|
* created_at: string, updated_at: string
|
|
|
|
|
* }
|
|
|
|
|
*/
|
2026-09-04 11:28:59 +01:00
|
|
|
final class CardRepository
|
2026-09-03 18:30:52 +01:00
|
|
|
{
|
2026-09-04 13:37:17 +01:00
|
|
|
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;
|
|
|
|
|
|
2026-09-03 18:30:52 +01:00
|
|
|
public function __construct(private readonly PDO $pdo)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-04 13:37:17 +01:00
|
|
|
* 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.
|
|
|
|
|
*
|
2026-09-04 11:28:59 +01:00
|
|
|
* @return CardRow[]
|
2026-09-03 18:30:52 +01:00
|
|
|
*/
|
2026-09-04 11:28:59 +01:00
|
|
|
public function allForProject(int $projectId): array
|
2026-09-03 18:30:52 +01:00
|
|
|
{
|
|
|
|
|
$stmt = $this->pdo->prepare(
|
2026-09-04 13:37:17 +01:00
|
|
|
self::SELECT . ' WHERE c.project_id = :project ORDER BY c.status_id, c.position ASC, c.id ASC'
|
2026-09-03 18:30:52 +01:00
|
|
|
);
|
2026-09-04 11:28:59 +01:00
|
|
|
$stmt->execute(['project' => $projectId]);
|
2026-09-03 18:30:52 +01:00
|
|
|
|
|
|
|
|
return array_map($this->cast(...), $stmt->fetchAll());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-04 11:28:59 +01:00
|
|
|
* @return CardRow|null
|
2026-09-03 18:30:52 +01:00
|
|
|
*/
|
2026-09-04 11:28:59 +01:00
|
|
|
public function findInProject(int $id, int $projectId): ?array
|
2026-09-03 18:30:52 +01:00
|
|
|
{
|
2026-09-04 13:37:17 +01:00
|
|
|
$stmt = $this->pdo->prepare(self::SELECT . ' WHERE c.id = :id AND c.project_id = :project');
|
2026-09-04 11:28:59 +01:00
|
|
|
$stmt->execute(['id' => $id, 'project' => $projectId]);
|
2026-09-03 18:30:52 +01:00
|
|
|
|
|
|
|
|
$row = $stmt->fetch();
|
|
|
|
|
|
|
|
|
|
return $row === false ? null : $this->cast($row);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-04 11:28:59 +01:00
|
|
|
* @return CardRow
|
2026-09-03 18:30:52 +01:00
|
|
|
*/
|
2026-09-04 11:28:59 +01:00
|
|
|
public function create(int $projectId, string $text, bool $complete, ?int $position): array
|
2026-09-03 18:30:52 +01:00
|
|
|
{
|
2026-09-04 13:37:17 +01:00
|
|
|
// A new card has no status — it goes to the end of the inbox column.
|
|
|
|
|
$position ??= $this->nextPositionInColumn($projectId, null);
|
2026-09-03 18:30:52 +01:00
|
|
|
|
|
|
|
|
$stmt = $this->pdo->prepare(
|
2026-09-04 11:28:59 +01:00
|
|
|
'INSERT INTO cards (project_id, text, complete, position)
|
|
|
|
|
VALUES (:project, :text, :complete, :position)'
|
2026-09-03 18:30:52 +01:00
|
|
|
);
|
|
|
|
|
$stmt->execute([
|
2026-09-04 11:28:59 +01:00
|
|
|
'project' => $projectId,
|
2026-09-03 18:30:52 +01:00
|
|
|
'text' => $text,
|
|
|
|
|
'complete' => $complete ? 1 : 0,
|
|
|
|
|
'position' => $position,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-09-04 11:28:59 +01:00
|
|
|
/** @var CardRow $card */
|
|
|
|
|
$card = $this->findInProject((int) $this->pdo->lastInsertId(), $projectId);
|
2026-09-03 18:30:52 +01:00
|
|
|
|
2026-09-04 11:28:59 +01:00
|
|
|
return $card;
|
2026-09-03 18:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-04 13:37:17 +01:00
|
|
|
* 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
|
2026-09-04 11:28:59 +01:00
|
|
|
* @return CardRow
|
2026-09-03 18:30:52 +01:00
|
|
|
*/
|
2026-09-04 11:28:59 +01:00
|
|
|
public function update(int $id, int $projectId, array $fields): array
|
2026-09-03 18:30:52 +01:00
|
|
|
{
|
2026-09-04 13:37:17 +01:00
|
|
|
/** @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()];
|
2026-09-03 18:30:52 +01:00
|
|
|
$params = ['id' => $id];
|
|
|
|
|
|
|
|
|
|
if (array_key_exists('text', $fields)) {
|
|
|
|
|
$sets[] = 'text = :text';
|
|
|
|
|
$params['text'] = $fields['text'];
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists('complete', $fields)) {
|
|
|
|
|
$sets[] = 'complete = :complete';
|
|
|
|
|
$params['complete'] = $fields['complete'] ? 1 : 0;
|
|
|
|
|
}
|
2026-09-04 13:37:17 +01:00
|
|
|
if (array_key_exists('status_id', $fields)) {
|
|
|
|
|
$sets[] = 'status_id = :status_id';
|
|
|
|
|
$params['status_id'] = $fields['status_id'];
|
|
|
|
|
}
|
|
|
|
|
if ($movesColumn) {
|
2026-09-03 18:30:52 +01:00
|
|
|
$sets[] = 'position = :position';
|
2026-09-04 13:37:17 +01:00
|
|
|
$params['position'] = $this->nextPositionInColumn($projectId, $fields['status_id']);
|
2026-09-03 18:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
2026-09-04 13:37:17 +01:00
|
|
|
$sql = 'UPDATE cards SET ' . implode(', ', $sets) . ' WHERE id = :id';
|
2026-09-03 18:30:52 +01:00
|
|
|
|
2026-09-04 13:37:17 +01:00
|
|
|
if (!$movesColumn) {
|
|
|
|
|
$this->pdo->prepare($sql)->execute($params);
|
2026-09-03 18:30:52 +01:00
|
|
|
|
2026-09-04 13:37:17 +01:00
|
|
|
/** @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;
|
2026-09-03 18:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete(int $id): void
|
|
|
|
|
{
|
2026-09-04 11:28:59 +01:00
|
|
|
$this->pdo->prepare('DELETE FROM cards WHERE id = :id')->execute(['id' => $id]);
|
2026-09-03 18:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
2026-09-03 18:59:51 +01:00
|
|
|
/**
|
2026-09-04 13:37:17 +01:00
|
|
|
* IDs of every card in the project.
|
2026-09-03 18:59:51 +01:00
|
|
|
*
|
|
|
|
|
* @return int[]
|
|
|
|
|
*/
|
2026-09-04 11:28:59 +01:00
|
|
|
public function idsForProject(int $projectId): array
|
2026-09-03 18:59:51 +01:00
|
|
|
{
|
2026-09-04 13:37:17 +01:00
|
|
|
$stmt = $this->pdo->prepare('SELECT id FROM cards WHERE project_id = :project');
|
2026-09-04 11:28:59 +01:00
|
|
|
$stmt->execute(['project' => $projectId]);
|
2026-09-03 18:59:51 +01:00
|
|
|
|
|
|
|
|
return array_map(intval(...), $stmt->fetchAll(PDO::FETCH_COLUMN));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-04 13:37:17 +01:00
|
|
|
* IDs of the cards currently in one column, in position order.
|
2026-09-03 18:59:51 +01:00
|
|
|
*
|
2026-09-04 13:37:17 +01:00
|
|
|
* @return int[]
|
2026-09-03 18:59:51 +01:00
|
|
|
*/
|
2026-09-04 13:37:17 +01:00
|
|
|
public function idsInColumn(int $projectId, ?int $statusId): array
|
2026-09-03 18:59:51 +01:00
|
|
|
{
|
2026-09-04 13:37:17 +01:00
|
|
|
[$match, $params] = $this->columnMatch($statusId);
|
2026-09-03 18:59:51 +01:00
|
|
|
$stmt = $this->pdo->prepare(
|
2026-09-04 13:37:17 +01:00
|
|
|
"SELECT id FROM cards WHERE project_id = :project AND {$match} ORDER BY position ASC, id ASC"
|
2026-09-03 18:59:51 +01:00
|
|
|
);
|
2026-09-04 13:37:17 +01:00
|
|
|
$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);
|
2026-09-03 18:59:51 +01:00
|
|
|
|
|
|
|
|
$this->pdo->beginTransaction();
|
|
|
|
|
try {
|
2026-09-04 13:37:17 +01:00
|
|
|
$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,
|
|
|
|
|
]);
|
2026-09-03 18:59:51 +01:00
|
|
|
}
|
2026-09-04 13:37:17 +01:00
|
|
|
|
|
|
|
|
foreach ($sourceColumns as $source) {
|
|
|
|
|
if ($source !== $statusId) {
|
|
|
|
|
$this->repack($projectId, $source);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-03 18:59:51 +01:00
|
|
|
$this->pdo->commit();
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
$this->pdo->rollBack();
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 11:28:59 +01:00
|
|
|
return $this->allForProject($projectId);
|
2026-09-03 18:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
2026-09-04 13:37:17 +01:00
|
|
|
/**
|
|
|
|
|
* 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
|
2026-09-03 18:30:52 +01:00
|
|
|
{
|
2026-09-04 13:37:17 +01:00
|
|
|
if ($ids === []) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$placeholders = implode(',', array_fill(0, count($ids), '?'));
|
2026-09-03 18:30:52 +01:00
|
|
|
$stmt = $this->pdo->prepare(
|
2026-09-04 13:37:17 +01:00
|
|
|
"SELECT DISTINCT status_id FROM cards WHERE project_id = ? AND id IN ($placeholders)"
|
2026-09-03 18:30:52 +01:00
|
|
|
);
|
2026-09-04 13:37:17 +01:00
|
|
|
$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);
|
2026-09-03 18:30:52 +01:00
|
|
|
|
|
|
|
|
return (int) $stmt->fetchColumn();
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 13:37:17 +01:00
|
|
|
/**
|
|
|
|
|
* 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')";
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-03 18:30:52 +01:00
|
|
|
/**
|
|
|
|
|
* @param array<string, mixed> $row
|
2026-09-04 11:28:59 +01:00
|
|
|
* @return CardRow
|
2026-09-03 18:30:52 +01:00
|
|
|
*/
|
|
|
|
|
private function cast(array $row): array
|
|
|
|
|
{
|
|
|
|
|
$row['id'] = (int) $row['id'];
|
2026-09-04 11:28:59 +01:00
|
|
|
$row['project_id'] = (int) $row['project_id'];
|
2026-09-03 18:30:52 +01:00
|
|
|
$row['complete'] = (bool) $row['complete'];
|
|
|
|
|
$row['position'] = (int) $row['position'];
|
|
|
|
|
|
2026-09-04 13:37:17 +01:00
|
|
|
$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']);
|
|
|
|
|
|
2026-09-04 11:28:59 +01:00
|
|
|
/** @var CardRow $row */
|
2026-09-03 18:30:52 +01:00
|
|
|
return $row;
|
|
|
|
|
}
|
|
|
|
|
}
|