feat: pairs table with questions/statements placeholders
- pairs: status, answer_type enum (yes_no/text/word), difficulty_level, FK to questions + 2x statements (positive/negative), auto-timestamps - questions + statements placeholder tables for future use - Safe ALTER TABLE migration for existing pairs placeholder - /api/pairs CRUD route, answer_type required on create Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
98
src/routes/pairs.js
Normal file
98
src/routes/pairs.js
Normal file
@@ -0,0 +1,98 @@
|
||||
const router = require('express').Router();
|
||||
const { query } = require('../db');
|
||||
|
||||
const STATUSES = ['draft', 'blocked', 'published'];
|
||||
const ANSWER_TYPES = ['yes_no', 'text', 'word'];
|
||||
|
||||
const STATUS_TIMESTAMP = {
|
||||
published: 'published_at',
|
||||
blocked: 'blocked_at',
|
||||
};
|
||||
|
||||
// GET /api/pairs
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
const { status, answer_type, limit = 50, offset = 0 } = req.query;
|
||||
const conditions = [];
|
||||
const params = [Math.min(parseInt(limit), 500), parseInt(offset)];
|
||||
if (status) { params.push(status); conditions.push(`status = $${params.length}`); }
|
||||
if (answer_type) { params.push(answer_type); conditions.push(`answer_type = $${params.length}`); }
|
||||
const where = conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
|
||||
const result = await query(
|
||||
`SELECT * FROM pairs ${where} ORDER BY created_at DESC LIMIT $1 OFFSET $2`,
|
||||
params
|
||||
);
|
||||
res.json(result.rows);
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
// GET /api/pairs/:id
|
||||
router.get('/:id', async (req, res, next) => {
|
||||
try {
|
||||
const result = await query('SELECT * FROM pairs WHERE id = $1', [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/pairs
|
||||
router.post('/', async (req, res, next) => {
|
||||
try {
|
||||
const { answer_type, difficulty_level, question_id,
|
||||
positive_statement_id, negative_statement_id, blocked_topic } = req.body;
|
||||
|
||||
if (!answer_type || !ANSWER_TYPES.includes(answer_type))
|
||||
return res.status(400).json({ error: `answer_type required, must be one of: ${ANSWER_TYPES.join(', ')}` });
|
||||
|
||||
const result = await query(
|
||||
`INSERT INTO pairs
|
||||
(answer_type, difficulty_level, question_id, positive_statement_id, negative_statement_id, blocked_topic)
|
||||
VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`,
|
||||
[answer_type, difficulty_level || null, question_id || null,
|
||||
positive_statement_id || null, negative_statement_id || null, blocked_topic || null]
|
||||
);
|
||||
res.status(201).json(result.rows[0]);
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
// PATCH /api/pairs/:id
|
||||
router.patch('/:id', async (req, res, next) => {
|
||||
try {
|
||||
const allowed = ['status', 'answer_type', 'difficulty_level', 'blocked_topic',
|
||||
'question_id', 'positive_statement_id', 'negative_statement_id',
|
||||
'published_at', 'blocked_at'];
|
||||
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(', ')}` });
|
||||
if (req.body.answer_type && !ANSWER_TYPES.includes(req.body.answer_type))
|
||||
return res.status(400).json({ error: `answer_type must be one of: ${ANSWER_TYPES.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 pairs 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/pairs/:id
|
||||
router.delete('/:id', async (req, res, next) => {
|
||||
try {
|
||||
const result = await query('DELETE FROM pairs 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); }
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user