Add stage 3: todo list and item CRUD API

Two migrations add todo_lists (owner_id FK to users, title, description) and
todo_items (list_id FK, text, complete, position), both with ON DELETE
CASCADE.

New endpoints under /api/lists, all behind AuthMiddleware:
  - lists: index / store / show / update (PATCH) / destroy
  - items: nested under a list, same five verbs
Lists are owner-scoped — another user's or a missing list responds 404, never
403. New items append after the highest position unless one is given; the
list carries item_count / completed_count. Item PATCH is partial and never
renumbers siblings.

Adds App\Support\Validator for request-body checks, TodoList/TodoItem
repositories, and body()/user() helpers on the Controller base. Feature tests
move their shared harness into tests/ApiTestCase; TodoTest covers CRUD,
ownership isolation, ordering, completion counts, validation and cascade
delete. Full suite: 15 passing.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-03 18:30:52 +01:00
co-authored by Claude Sonnet 5
parent 5e3b8dbd7e
commit 5a0d29a308
12 changed files with 1093 additions and 4 deletions
+28 -1
View File
@@ -5,7 +5,11 @@ declare(strict_types=1);
use App\Auth\AuthMiddleware;
use App\Auth\JwtService;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\TodoItemController;
use App\Http\Controllers\TodoListController;
use App\Http\JsonErrorHandler;
use App\Repository\TodoItemRepository;
use App\Repository\TodoListRepository;
use App\Repository\UserRepository;
use App\Support\Config;
use App\Support\Database;
@@ -30,14 +34,23 @@ $errorMiddleware->setDefaultErrorHandler(
// --- Wiring -----------------------------------------------------------------
$users = new UserRepository($database->pdo());
$todoLists = new TodoListRepository($database->pdo());
$todoItems = new TodoItemRepository($database->pdo());
$jwt = new JwtService($config->jwtSecret, $config->jwtTtl);
$authController = new AuthController($users, $jwt);
$listController = new TodoListController($todoLists);
$itemController = new TodoItemController($todoLists, $todoItems);
$authMiddleware = new AuthMiddleware($jwt, $users);
// --- Routes ---------------------------------------------------------------
$app->group('/api', function (RouteCollectorProxy $group) use ($authController, $authMiddleware) {
$app->group('/api', function (RouteCollectorProxy $group) use (
$authController,
$listController,
$itemController,
$authMiddleware,
) {
$group->get('/health', function (Request $request, Response $response): Response {
$response->getBody()->write((string) json_encode(['status' => 'ok']));
return $response->withHeader('Content-Type', 'application/json');
@@ -47,6 +60,20 @@ $app->group('/api', function (RouteCollectorProxy $group) use ($authController,
$group->post('/auth/login', [$authController, 'login']);
$group->get('/me', [$authController, 'me'])->add($authMiddleware);
$group->group('/lists', function (RouteCollectorProxy $lists) use ($listController, $itemController) {
$lists->get('', [$listController, 'index']);
$lists->post('', [$listController, 'store']);
$lists->get('/{listId:[0-9]+}', [$listController, 'show']);
$lists->patch('/{listId:[0-9]+}', [$listController, 'update']);
$lists->delete('/{listId:[0-9]+}', [$listController, 'destroy']);
$lists->get('/{listId:[0-9]+}/items', [$itemController, 'index']);
$lists->post('/{listId:[0-9]+}/items', [$itemController, 'store']);
$lists->get('/{listId:[0-9]+}/items/{itemId:[0-9]+}', [$itemController, 'show']);
$lists->patch('/{listId:[0-9]+}/items/{itemId:[0-9]+}', [$itemController, 'update']);
$lists->delete('/{listId:[0-9]+}/items/{itemId:[0-9]+}', [$itemController, 'destroy']);
})->add($authMiddleware);
});
return $app;