Files
project-manager/src/Repository/CardRepository.php
T

177 lines
5.0 KiB
PHP
Raw Normal View History

2026-09-03 18:30:52 +01:00
<?php
declare(strict_types=1);
namespace App\Repository;
use PDO;
/**
* Data access for the `cards` table.
2026-09-03 18:30:52 +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
* }
*/
final class CardRepository
2026-09-03 18:30:52 +01:00
{
public function __construct(private readonly PDO $pdo)
{
}
/**
* @return CardRow[]
2026-09-03 18:30:52 +01:00
*/
public function allForProject(int $projectId): array
2026-09-03 18:30:52 +01:00
{
$stmt = $this->pdo->prepare(
'SELECT * FROM cards WHERE project_id = :project ORDER BY position ASC, id ASC'
2026-09-03 18:30:52 +01:00
);
$stmt->execute(['project' => $projectId]);
2026-09-03 18:30:52 +01:00
return array_map($this->cast(...), $stmt->fetchAll());
}
/**
* @return CardRow|null
2026-09-03 18:30:52 +01:00
*/
public function findInProject(int $id, int $projectId): ?array
2026-09-03 18:30:52 +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);
}
/**
* @return CardRow
2026-09-03 18:30:52 +01:00
*/
public function create(int $projectId, string $text, bool $complete, ?int $position): array
2026-09-03 18:30:52 +01:00
{
$position ??= $this->nextPosition($projectId);
2026-09-03 18:30:52 +01:00
$stmt = $this->pdo->prepare(
'INSERT INTO cards (project_id, text, complete, position)
VALUES (:project, :text, :complete, :position)'
2026-09-03 18:30:52 +01:00
);
$stmt->execute([
'project' => $projectId,
2026-09-03 18:30:52 +01:00
'text' => $text,
'complete' => $complete ? 1 : 0,
'position' => $position,
]);
/** @var CardRow $card */
$card = $this->findInProject((int) $this->pdo->lastInsertId(), $projectId);
2026-09-03 18:30:52 +01:00
return $card;
2026-09-03 18:30:52 +01:00
}
/**
* @param array{text?: string, complete?: bool, position?: int} $fields
* @return CardRow
2026-09-03 18:30:52 +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'];
}
$stmt = $this->pdo->prepare('UPDATE cards SET ' . implode(', ', $sets) . ' WHERE id = :id');
2026-09-03 18:30:52 +01:00
$stmt->execute($params);
/** @var CardRow $card */
$card = $this->findInProject($id, $projectId);
2026-09-03 18:30:52 +01:00
return $card;
2026-09-03 18:30:52 +01:00
}
public function delete(int $id): void
{
$this->pdo->prepare('DELETE FROM cards WHERE id = :id')->execute(['id' => $id]);
2026-09-03 18:30:52 +01:00
}
/**
* IDs of every card in the project, in current position order.
*
* @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->execute(['project' => $projectId]);
return array_map(intval(...), $stmt->fetchAll(PDO::FETCH_COLUMN));
}
/**
* Assign positions 0..n-1 to the given cards in one transaction.
*
* @param int[] $orderedIds every card id in the project, exactly once
* @return CardRow[] the project's cards in their new order
*/
public function reorder(int $projectId, array $orderedIds): array
{
$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'
);
$this->pdo->beginTransaction();
try {
foreach (array_values($orderedIds) as $position => $id) {
$stmt->execute(['position' => $position, 'id' => $id, 'project' => $projectId]);
}
$this->pdo->commit();
} catch (\Throwable $e) {
$this->pdo->rollBack();
throw $e;
}
return $this->allForProject($projectId);
}
private function nextPosition(int $projectId): int
2026-09-03 18:30:52 +01:00
{
$stmt = $this->pdo->prepare(
'SELECT COALESCE(MAX(position), -1) + 1 FROM cards WHERE project_id = :project'
2026-09-03 18:30:52 +01:00
);
$stmt->execute(['project' => $projectId]);
2026-09-03 18:30:52 +01:00
return (int) $stmt->fetchColumn();
}
/**
* @param array<string, mixed> $row
* @return CardRow
2026-09-03 18:30:52 +01:00
*/
private function cast(array $row): array
{
$row['id'] = (int) $row['id'];
$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'];
/** @var CardRow $row */
2026-09-03 18:30:52 +01:00
return $row;
}
}