feat: pairs answer_type TEXT[], statements.answer bool
- pairs: answer_type changed from VARCHAR(20) to TEXT[] (multi-value) POST/PATCH now accept arrays like ['text','yes_no','word'] - statements: add answer BOOLEAN (nullable) for yes/no correct answer - migration: ALTER TABLE pairs + ADD COLUMN statements.answer Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,7 +2,14 @@ const router = require('express').Router();
|
||||
const { query } = require('../db');
|
||||
|
||||
const STATUSES = ['draft', 'blocked', 'published'];
|
||||
const ANSWER_TYPES = ['yes_no', 'text', 'word'];
|
||||
const ANSWER_TYPES = new Set(['yes_no', 'text', 'word']);
|
||||
|
||||
function validateAnswerType(val) {
|
||||
const arr = Array.isArray(val) ? val : (val ? [val] : []);
|
||||
if (!arr.length) return 'answer_type must be a non-empty array of: yes_no, text, word';
|
||||
if (!arr.every(t => ANSWER_TYPES.has(t))) return `answer_type values must be: ${[...ANSWER_TYPES].join(', ')}`;
|
||||
return null;
|
||||
}
|
||||
|
||||
const STATUS_TIMESTAMP = {
|
||||
published: 'published_at',
|
||||
@@ -41,14 +48,15 @@ router.post('/', async (req, res, next) => {
|
||||
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 atErr = validateAnswerType(answer_type);
|
||||
if (atErr) return res.status(400).json({ error: atErr });
|
||||
const atArr = Array.isArray(answer_type) ? answer_type : [answer_type];
|
||||
|
||||
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,
|
||||
[atArr, difficulty_level || null, question_id || null,
|
||||
positive_statement_id || null, negative_statement_id || null, blocked_topic || null]
|
||||
);
|
||||
res.status(201).json(result.rows[0]);
|
||||
|
||||
@@ -58,16 +58,17 @@ router.post('/', async (req, res, next) => {
|
||||
const {
|
||||
negative_sentence_de, negative_sentence_en, negative_sentence_sv,
|
||||
positive_sentence_de, positive_sentence_en, positive_sentence_sv,
|
||||
blocked_topic,
|
||||
blocked_topic, answer,
|
||||
} = req.body;
|
||||
const result = await query(
|
||||
`INSERT INTO statements
|
||||
(negative_sentence_de, negative_sentence_en, negative_sentence_sv,
|
||||
positive_sentence_de, positive_sentence_en, positive_sentence_sv, blocked_topic)
|
||||
VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING *`,
|
||||
positive_sentence_de, positive_sentence_en, positive_sentence_sv,
|
||||
blocked_topic, answer)
|
||||
VALUES ($1,$2,$3,$4,$5,$6,$7,$8) RETURNING *`,
|
||||
[negative_sentence_de || null, negative_sentence_en || null, negative_sentence_sv || null,
|
||||
positive_sentence_de || null, positive_sentence_en || null, positive_sentence_sv || null,
|
||||
blocked_topic || null]
|
||||
blocked_topic || null, answer !== undefined ? answer : null]
|
||||
);
|
||||
res.status(201).json({ ...result.rows[0], positive_word_ids: [], negative_word_ids: [] });
|
||||
} catch (err) { next(err); }
|
||||
@@ -78,7 +79,8 @@ 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_sv',
|
||||
'positive_sentence_de', 'positive_sentence_en', 'positive_sentence_sv'];
|
||||
'positive_sentence_de', 'positive_sentence_en', 'positive_sentence_sv',
|
||||
'answer'];
|
||||
const fields = Object.keys(req.body).filter(k => allowed.includes(k));
|
||||
if (!fields.length) return res.status(400).json({ error: 'No valid fields provided' });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user