feat(api): projects routes
Add lib/api/routes/projects.js: list by space (with status filter), create scoped to space, get/patch/delete by id. FK violation from unknown space_id maps to 400 invalid_space. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
79
lib/api/routes/projects.js
Normal file
79
lib/api/routes/projects.js
Normal file
@@ -0,0 +1,79 @@
|
||||
import { Router } from 'express';
|
||||
import { z } from 'zod';
|
||||
import * as repo from '../../db/repos/projects.js';
|
||||
import { validate } from '../validate.js';
|
||||
import { NotFoundError, ValidationError, asyncWrap } from '../errors.js';
|
||||
|
||||
const STATUSES = ['idea', 'active', 'paused', 'done', 'abandoned'];
|
||||
|
||||
const createSchema = z.object({
|
||||
slug: z.string().min(1).max(64).regex(/^[a-z0-9-]+$/),
|
||||
name: z.string().min(1).max(200),
|
||||
description: z.string().optional(),
|
||||
status: z.enum(STATUSES).optional(),
|
||||
started_at: z.string().datetime().nullable().optional()
|
||||
});
|
||||
|
||||
const patchSchema = z.object({
|
||||
slug: z.string().min(1).max(64).regex(/^[a-z0-9-]+$/).optional(),
|
||||
name: z.string().min(1).max(200).optional(),
|
||||
description: z.string().nullable().optional(),
|
||||
status: z.enum(STATUSES).optional(),
|
||||
started_at: z.string().datetime().nullable().optional(),
|
||||
completed_at: z.string().datetime().nullable().optional()
|
||||
});
|
||||
|
||||
const idParams = z.object({ id: z.string().uuid() });
|
||||
const spaceParams = z.object({ space_id: z.string().uuid() });
|
||||
|
||||
export const spacesScopedRouter = Router({ mergeParams: true });
|
||||
export const router = Router();
|
||||
|
||||
spacesScopedRouter.get('/',
|
||||
validate({ params: spaceParams, query: z.object({ status: z.enum(STATUSES).optional() }) }),
|
||||
asyncWrap(async (req, res) => {
|
||||
res.json(await repo.listBySpace(req.params.space_id, { status: req.validatedQuery.status }));
|
||||
})
|
||||
);
|
||||
|
||||
spacesScopedRouter.post('/',
|
||||
validate({ params: spaceParams, body: createSchema }),
|
||||
asyncWrap(async (req, res) => {
|
||||
try {
|
||||
const row = await repo.create({ ...req.body, space_id: req.params.space_id }, req.actor);
|
||||
res.status(201).json(row);
|
||||
} catch (e) {
|
||||
if (e.code === '23503') throw new ValidationError('invalid space', { space_id: req.params.space_id });
|
||||
throw e;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
router.get('/:id',
|
||||
validate({ params: idParams }),
|
||||
asyncWrap(async (req, res) => {
|
||||
const row = await repo.getById(req.params.id);
|
||||
if (!row) throw new NotFoundError('project not found');
|
||||
res.json(row);
|
||||
})
|
||||
);
|
||||
|
||||
router.patch('/:id',
|
||||
validate({ params: idParams, body: patchSchema }),
|
||||
asyncWrap(async (req, res) => {
|
||||
const existing = await repo.getById(req.params.id);
|
||||
if (!existing) throw new NotFoundError('project not found');
|
||||
const row = await repo.update(req.params.id, req.body, req.actor);
|
||||
res.json(row);
|
||||
})
|
||||
);
|
||||
|
||||
router.delete('/:id',
|
||||
validate({ params: idParams }),
|
||||
asyncWrap(async (req, res) => {
|
||||
const existing = await repo.getById(req.params.id);
|
||||
if (!existing) throw new NotFoundError('project not found');
|
||||
await repo.del(req.params.id, req.actor);
|
||||
res.status(204).end();
|
||||
})
|
||||
);
|
||||
Reference in New Issue
Block a user