Project scope shifts from a todo list to a project-management app. This is a straight terminology rename across code, comments, migrations, tests, and docs — no behaviour change. - DB: table todo_lists -> projects, todo_items -> cards, column todo_items.list_id -> cards.project_id, indexes renamed. Migrations 003/004 rewritten in place (destructive; recreate the volume with `down -v`). - API: /api/lists -> /api/projects, nested /items -> /cards, reorder body item_ids -> card_ids, JSON keys list/lists/item/items -> project/projects/ card/cards, item_count -> card_count, list_id -> project_id, and the matching error messages. - PHP: TodoList/TodoItem Repository + Controller -> Project/Card; shared SQL aliases l/i -> p/c. - Frontend: stores lists.ts/items.ts -> projects.ts/cards.ts (useProjectsStore / useCardsStore, MAX_PROJECTS), ListView -> ProjectView, TodoItemRow -> CardRow, route /lists/:id -> /projects/:id (name "project"), types TodoList/ TodoItem -> Project/Card, and all UI copy. CSS .lists*/.list-head* -> .projects*/.project-head*, .item* -> .card-row* (kept the generic .card panel class), .items -> .cards. - Product name in the header, PWA manifest, index.html title and package descriptions -> "Project Manager" / "Projects". Backend suite: 37 passing. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
144 lines
4.1 KiB
PHP
144 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repository;
|
|
|
|
use PDO;
|
|
|
|
/**
|
|
* Data access for the `projects` table.
|
|
*
|
|
* @phpstan-type ProjectRow array{
|
|
* id: int, owner_id: int, title: string, description: string,
|
|
* card_count: int, completed_count: int, created_at: string, updated_at: string
|
|
* }
|
|
*/
|
|
final class ProjectRepository
|
|
{
|
|
private const SELECT = <<<'SQL'
|
|
SELECT p.id, p.owner_id, p.title, p.description, p.created_at, p.updated_at,
|
|
(SELECT COUNT(*) FROM cards c WHERE c.project_id = p.id) AS card_count,
|
|
(SELECT COUNT(*) FROM cards c WHERE c.project_id = p.id AND c.complete = 1) AS completed_count
|
|
FROM projects p
|
|
SQL;
|
|
|
|
public function __construct(private readonly PDO $pdo)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Every project owned by the user, always sorted alphabetically by title
|
|
* (case-insensitive). There is deliberately no other ordering option.
|
|
*
|
|
* @return ProjectRow[]
|
|
*/
|
|
public function allForOwner(int $ownerId): array
|
|
{
|
|
$stmt = $this->pdo->prepare(
|
|
self::SELECT . ' WHERE p.owner_id = :owner ORDER BY p.title COLLATE NOCASE ASC, p.id ASC'
|
|
);
|
|
$stmt->execute(['owner' => $ownerId]);
|
|
|
|
return array_map($this->cast(...), $stmt->fetchAll());
|
|
}
|
|
|
|
public function countForOwner(int $ownerId): int
|
|
{
|
|
$stmt = $this->pdo->prepare('SELECT COUNT(*) FROM projects WHERE owner_id = :owner');
|
|
$stmt->execute(['owner' => $ownerId]);
|
|
|
|
return (int) $stmt->fetchColumn();
|
|
}
|
|
|
|
/**
|
|
* @return ProjectRow|null
|
|
*/
|
|
public function findOwnedBy(int $id, int $ownerId): ?array
|
|
{
|
|
$stmt = $this->pdo->prepare(self::SELECT . ' WHERE p.id = :id AND p.owner_id = :owner');
|
|
$stmt->execute(['id' => $id, 'owner' => $ownerId]);
|
|
|
|
$row = $stmt->fetch();
|
|
|
|
return $row === false ? null : $this->cast($row);
|
|
}
|
|
|
|
/**
|
|
* @return ProjectRow
|
|
*/
|
|
public function create(int $ownerId, string $title, string $description): array
|
|
{
|
|
$stmt = $this->pdo->prepare(
|
|
'INSERT INTO projects (owner_id, title, description) VALUES (:owner, :title, :description)'
|
|
);
|
|
$stmt->execute([
|
|
'owner' => $ownerId,
|
|
'title' => $title,
|
|
'description' => $description,
|
|
]);
|
|
|
|
/** @var ProjectRow $project */
|
|
$project = $this->findOwnedBy((int) $this->pdo->lastInsertId(), $ownerId);
|
|
|
|
return $project;
|
|
}
|
|
|
|
/**
|
|
* @param array{title?: string, description?: string} $fields
|
|
* @return ProjectRow
|
|
*/
|
|
public function update(int $id, int $ownerId, array $fields): array
|
|
{
|
|
$sets = ['updated_at = ' . $this->nowExpr()];
|
|
$params = ['id' => $id];
|
|
|
|
foreach (['title', 'description'] as $column) {
|
|
if (array_key_exists($column, $fields)) {
|
|
$sets[] = "{$column} = :{$column}";
|
|
$params[$column] = $fields[$column];
|
|
}
|
|
}
|
|
|
|
$stmt = $this->pdo->prepare('UPDATE projects SET ' . implode(', ', $sets) . ' WHERE id = :id');
|
|
$stmt->execute($params);
|
|
|
|
/** @var ProjectRow $project */
|
|
$project = $this->findOwnedBy($id, $ownerId);
|
|
|
|
return $project;
|
|
}
|
|
|
|
public function delete(int $id): void
|
|
{
|
|
$this->pdo->prepare('DELETE FROM projects WHERE id = :id')->execute(['id' => $id]);
|
|
}
|
|
|
|
/** Bump updated_at, e.g. when the project's cards change. */
|
|
public function touch(int $id): void
|
|
{
|
|
$this->pdo->prepare('UPDATE projects SET updated_at = ' . $this->nowExpr() . ' WHERE id = :id')
|
|
->execute(['id' => $id]);
|
|
}
|
|
|
|
private function nowExpr(): string
|
|
{
|
|
return "strftime('%Y-%m-%dT%H:%M:%SZ', 'now')";
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $row
|
|
* @return ProjectRow
|
|
*/
|
|
private function cast(array $row): array
|
|
{
|
|
$row['id'] = (int) $row['id'];
|
|
$row['owner_id'] = (int) $row['owner_id'];
|
|
$row['card_count'] = (int) $row['card_count'];
|
|
$row['completed_count'] = (int) $row['completed_count'];
|
|
|
|
/** @var ProjectRow $row */
|
|
return $row;
|
|
}
|
|
}
|