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 11:28:59 +01:00
|
|
|
* @phpstan-type CardRow array{
|
|
|
|
|
* id: int, project_id: int, text: string, complete: bool, position: int,
|
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
|
|
|
{
|
|
|
|
|
public function __construct(private readonly PDO $pdo)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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 11:28:59 +01:00
|
|
|
'SELECT * FROM cards WHERE project_id = :project ORDER BY position ASC, 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 11:28:59 +01:00
|
|
|
$stmt = $this->pdo->prepare('SELECT * FROM cards WHERE id = :id AND project_id = :project');
|
|
|
|
|
$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 11:28:59 +01:00
|
|
|
$position ??= $this->nextPosition($projectId);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array{text?: string, complete?: bool, position?: int} $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
|
|
|
{
|
|
|
|
|
$sets = ['updated_at = ' . "strftime('%Y-%m-%dT%H:%M:%SZ', 'now')"];
|
|
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists('position', $fields)) {
|
|
|
|
|
$sets[] = 'position = :position';
|
|
|
|
|
$params['position'] = $fields['position'];
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 11:28:59 +01:00
|
|
|
$stmt = $this->pdo->prepare('UPDATE cards SET ' . implode(', ', $sets) . ' WHERE id = :id');
|
2026-09-03 18:30:52 +01:00
|
|
|
$stmt->execute($params);
|
|
|
|
|
|
2026-09-04 11:28:59 +01:00
|
|
|
/** @var CardRow $card */
|
|
|
|
|
$card = $this->findInProject($id, $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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 11:28:59 +01:00
|
|
|
* IDs of every card in the project, in current position order.
|
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
|
|
|
{
|
|
|
|
|
$stmt = $this->pdo->prepare(
|
2026-09-04 11:28:59 +01:00
|
|
|
'SELECT id FROM cards WHERE project_id = :project ORDER BY position ASC, id ASC'
|
2026-09-03 18:59:51 +01:00
|
|
|
);
|
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 11:28:59 +01:00
|
|
|
* Assign positions 0..n-1 to the given cards in one transaction.
|
2026-09-03 18:59:51 +01:00
|
|
|
*
|
2026-09-04 11:28:59 +01:00
|
|
|
* @param int[] $orderedIds every card id in the project, exactly once
|
|
|
|
|
* @return CardRow[] the project's cards in their new order
|
2026-09-03 18:59:51 +01:00
|
|
|
*/
|
2026-09-04 11:28:59 +01:00
|
|
|
public function reorder(int $projectId, array $orderedIds): array
|
2026-09-03 18:59:51 +01:00
|
|
|
{
|
|
|
|
|
$stmt = $this->pdo->prepare(
|
2026-09-04 11:28:59 +01:00
|
|
|
'UPDATE cards SET position = :position,
|
2026-09-03 18:59:51 +01:00
|
|
|
updated_at = ' . "strftime('%Y-%m-%dT%H:%M:%SZ', 'now')" . '
|
2026-09-04 11:28:59 +01:00
|
|
|
WHERE id = :id AND project_id = :project'
|
2026-09-03 18:59:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->pdo->beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
foreach (array_values($orderedIds) as $position => $id) {
|
2026-09-04 11:28:59 +01:00
|
|
|
$stmt->execute(['position' => $position, 'id' => $id, 'project' => $projectId]);
|
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 11:28:59 +01:00
|
|
|
private function nextPosition(int $projectId): int
|
2026-09-03 18:30:52 +01:00
|
|
|
{
|
|
|
|
|
$stmt = $this->pdo->prepare(
|
2026-09-04 11:28:59 +01:00
|
|
|
'SELECT COALESCE(MAX(position), -1) + 1 FROM cards WHERE project_id = :project'
|
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 (int) $stmt->fetchColumn();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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 11:28:59 +01:00
|
|
|
/** @var CardRow $row */
|
2026-09-03 18:30:52 +01:00
|
|
|
return $row;
|
|
|
|
|
}
|
|
|
|
|
}
|