feat: statements table with positive/negative words M2M

- Trilingual positive + negative sentences, status, auto-timestamps
- statement_positive_words + statement_negative_words junction tables
- /api/statements CRUD + link/unlink for both word relations

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-21 10:16:56 +02:00
parent 227247d51c
commit 9eac7b47fc
3 changed files with 202 additions and 4 deletions

158
src/routes/statements.js Normal file
View File

@@ -0,0 +1,158 @@
const router = require('express').Router();
const { query } = require('../db');
const STATUSES = ['draft', 'blocked', 'published'];
const STATUS_TIMESTAMP = { published: 'published_at', blocked: 'blocked_at' };
async function getWithRelations(id) {
const result = await query(
`SELECT s.*,
COALESCE(json_agg(DISTINCT spw.word_id) FILTER (WHERE spw.word_id IS NOT NULL), '[]') AS positive_word_ids,
COALESCE(json_agg(DISTINCT snw.word_id) FILTER (WHERE snw.word_id IS NOT NULL), '[]') AS negative_word_ids
FROM statements s
LEFT JOIN statement_positive_words spw ON spw.statement_id = s.id
LEFT JOIN statement_negative_words snw ON snw.statement_id = s.id
WHERE s.id = $1
GROUP BY s.id`,
[id]
);
return result.rows[0] || null;
}
// GET /api/statements
router.get('/', async (req, res, next) => {
try {
const { status, limit = 50, offset = 0 } = req.query;
const params = [Math.min(parseInt(limit), 500), parseInt(offset)];
const where = status ? `WHERE s.status = $3` : '';
if (status) params.push(status);
const result = await query(
`SELECT s.*,
COALESCE(json_agg(DISTINCT spw.word_id) FILTER (WHERE spw.word_id IS NOT NULL), '[]') AS positive_word_ids,
COALESCE(json_agg(DISTINCT snw.word_id) FILTER (WHERE snw.word_id IS NOT NULL), '[]') AS negative_word_ids
FROM statements s
LEFT JOIN statement_positive_words spw ON spw.statement_id = s.id
LEFT JOIN statement_negative_words snw ON snw.statement_id = s.id
${where}
GROUP BY s.id
ORDER BY s.created_at DESC
LIMIT $1 OFFSET $2`,
params
);
res.json(result.rows);
} catch (err) { next(err); }
});
// GET /api/statements/:id
router.get('/:id', async (req, res, next) => {
try {
const row = await getWithRelations(req.params.id);
if (!row) return res.status(404).json({ error: 'Not found' });
res.json(row);
} catch (err) { next(err); }
});
// POST /api/statements
router.post('/', async (req, res, next) => {
try {
const {
negative_sentence_de, negative_sentence_en, negative_sentence_se,
positive_sentence_de, positive_sentence_en, positive_sentence_se,
blocked_topic,
} = req.body;
const result = await query(
`INSERT INTO statements
(negative_sentence_de, negative_sentence_en, negative_sentence_se,
positive_sentence_de, positive_sentence_en, positive_sentence_se, blocked_topic)
VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING *`,
[negative_sentence_de || null, negative_sentence_en || null, negative_sentence_se || null,
positive_sentence_de || null, positive_sentence_en || null, positive_sentence_se || null,
blocked_topic || null]
);
res.status(201).json({ ...result.rows[0], positive_word_ids: [], negative_word_ids: [] });
} catch (err) { next(err); }
});
// PATCH /api/statements/:id
router.patch('/:id', async (req, res, next) => {
try {
const allowed = ['status', 'blocked_topic', 'published_at', 'blocked_at',
'negative_sentence_de', 'negative_sentence_en', 'negative_sentence_se',
'positive_sentence_de', 'positive_sentence_en', 'positive_sentence_se'];
const fields = Object.keys(req.body).filter(k => allowed.includes(k));
if (!fields.length) return res.status(400).json({ error: 'No valid fields provided' });
if (req.body.status && !STATUSES.includes(req.body.status))
return res.status(400).json({ error: `status must be one of: ${STATUSES.join(', ')}` });
const tsField = STATUS_TIMESTAMP[req.body.status];
if (tsField && !req.body[tsField]) {
fields.push(tsField);
req.body[tsField] = new Date().toISOString();
}
const setClauses = fields.map((f, i) => `${f} = $${i + 1}`).join(', ');
const values = [...fields.map(f => req.body[f]), req.params.id];
const result = await query(
`UPDATE statements SET ${setClauses} WHERE id = $${fields.length + 1} RETURNING *`,
values
);
if (!result.rows.length) return res.status(404).json({ error: 'Not found' });
res.json(result.rows[0]);
} catch (err) { next(err); }
});
// DELETE /api/statements/:id
router.delete('/:id', async (req, res, next) => {
try {
const result = await query('DELETE FROM statements WHERE id = $1 RETURNING id', [req.params.id]);
if (!result.rows.length) return res.status(404).json({ error: 'Not found' });
res.status(204).end();
} catch (err) { next(err); }
});
// POST /api/statements/:id/positive-words/:wordId
router.post('/:id/positive-words/:wordId', async (req, res, next) => {
try {
await query(
`INSERT INTO statement_positive_words (statement_id, word_id) VALUES ($1,$2) ON CONFLICT DO NOTHING`,
[req.params.id, req.params.wordId]
);
res.status(204).end();
} catch (err) { next(err); }
});
// DELETE /api/statements/:id/positive-words/:wordId
router.delete('/:id/positive-words/:wordId', async (req, res, next) => {
try {
await query(
`DELETE FROM statement_positive_words WHERE statement_id = $1 AND word_id = $2`,
[req.params.id, req.params.wordId]
);
res.status(204).end();
} catch (err) { next(err); }
});
// POST /api/statements/:id/negative-words/:wordId
router.post('/:id/negative-words/:wordId', async (req, res, next) => {
try {
await query(
`INSERT INTO statement_negative_words (statement_id, word_id) VALUES ($1,$2) ON CONFLICT DO NOTHING`,
[req.params.id, req.params.wordId]
);
res.status(204).end();
} catch (err) { next(err); }
});
// DELETE /api/statements/:id/negative-words/:wordId
router.delete('/:id/negative-words/:wordId', async (req, res, next) => {
try {
await query(
`DELETE FROM statement_negative_words WHERE statement_id = $1 AND word_id = $2`,
[req.params.id, req.params.wordId]
);
res.status(204).end();
} catch (err) { next(err); }
});
module.exports = router;