Fix sub-route shadowing: move /:id after sub-routes, add missing GETs

- pictures.js: move GET/POST/DELETE /:id/words/* BEFORE GET /:id
  so /:id/words is not shadowed; add POST /:id/words/:wordId
- words.js: move GET /:id after sub-routes; add GET /:id/pictures
  and GET /:id/categories
- objects.js: move GET /:id after sub-routes (/:id/words, /:id/pairs,
  /:id/pictures); add GET /:id/pictures

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-21 22:42:09 +02:00
parent 15fa315b3f
commit 6d1f610e3d
3 changed files with 94 additions and 41 deletions

View File

@@ -37,27 +37,6 @@ router.get('/', async (req, res, next) => {
} catch (err) { next(err); }
});
// GET /api/words/:id
router.get('/:id', async (req, res, next) => {
try {
const result = await query(
`SELECT w.*,
COALESCE(json_agg(DISTINCT p.id) FILTER (WHERE p.id IS NOT NULL), '[]') AS picture_ids,
COALESCE(json_agg(DISTINCT c.id) FILTER (WHERE c.id IS NOT NULL), '[]') AS category_ids
FROM words w
LEFT JOIN word_pictures wp ON wp.word_id = w.id
LEFT JOIN pictures p ON p.id = wp.picture_id
LEFT JOIN word_categories wc ON wc.word_id = w.id
LEFT JOIN categories c ON c.id = wc.category_id
WHERE w.id = $1
GROUP BY w.id`,
[req.params.id]
);
if (!result.rows.length) return res.status(404).json({ error: 'Not found' });
res.json(result.rows[0]);
} catch (err) { next(err); }
});
// POST /api/words
router.post('/', async (req, res, next) => {
try {
@@ -109,6 +88,55 @@ router.delete('/:id', async (req, res, next) => {
} catch (err) { next(err); }
});
// GET /api/words/:id — AFTER sub-routes to avoid shadowing
router.get('/:id', async (req, res, next) => {
try {
const result = await query(
`SELECT w.*,
COALESCE(json_agg(DISTINCT p.id) FILTER (WHERE p.id IS NOT NULL), '[]') AS picture_ids,
COALESCE(json_agg(DISTINCT c.id) FILTER (WHERE c.id IS NOT NULL), '[]') AS category_ids
FROM words w
LEFT JOIN word_pictures wp ON wp.word_id = w.id
LEFT JOIN pictures p ON p.id = wp.picture_id
LEFT JOIN word_categories wc ON wc.word_id = w.id
LEFT JOIN categories c ON c.id = wc.category_id
WHERE w.id = $1
GROUP BY w.id`,
[req.params.id]
);
if (!result.rows.length) return res.status(404).json({ error: 'Not found' });
res.json(result.rows[0]);
} catch (err) { next(err); }
});
// GET /api/words/:id/pictures — verknüpfte Bilder laden
router.get('/:id/pictures', async (req, res, next) => {
try {
const result = await query(
`SELECT p.* FROM pictures p
JOIN word_pictures wp ON wp.picture_id = p.id
WHERE wp.word_id = $1
ORDER BY p.created_at DESC`,
[req.params.id]
);
res.json(result.rows);
} catch (err) { next(err); }
});
// GET /api/words/:id/categories — verknüpfte Kategorien laden
router.get('/:id/categories', async (req, res, next) => {
try {
const result = await query(
`SELECT c.* FROM categories c
JOIN word_categories wc ON wc.category_id = c.id
WHERE wc.word_id = $1
ORDER BY c.name_de`,
[req.params.id]
);
res.json(result.rows);
} catch (err) { next(err); }
});
// POST /api/words/:id/pictures/:pictureId — Bild verknüpfen
router.post('/:id/pictures/:pictureId', async (req, res, next) => {
try {