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:
@@ -319,6 +319,16 @@ async function migrate() {
|
|||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
|
||||||
|
// pairs.answer_type → TEXT[] (multi-select)
|
||||||
|
await query(`ALTER TABLE pairs DROP CONSTRAINT IF EXISTS pairs_answer_type_check`).catch(() => {});
|
||||||
|
await query(`
|
||||||
|
ALTER TABLE pairs ALTER COLUMN answer_type TYPE TEXT[]
|
||||||
|
USING ARRAY[answer_type]::TEXT[]
|
||||||
|
`).catch(() => {});
|
||||||
|
|
||||||
|
// statements.answer — boolean nullable (for yes/no correct answer)
|
||||||
|
await query(`ALTER TABLE statements ADD COLUMN IF NOT EXISTS answer BOOLEAN`);
|
||||||
|
|
||||||
// objects_created on pictures
|
// objects_created on pictures
|
||||||
await query(`ALTER TABLE pictures ADD COLUMN IF NOT EXISTS objects_created BOOLEAN NOT NULL DEFAULT false`);
|
await query(`ALTER TABLE pictures ADD COLUMN IF NOT EXISTS objects_created BOOLEAN NOT NULL DEFAULT false`);
|
||||||
await query(`ALTER TABLE pictures ADD COLUMN IF NOT EXISTS objects_created_at TIMESTAMPTZ`);
|
await query(`ALTER TABLE pictures ADD COLUMN IF NOT EXISTS objects_created_at TIMESTAMPTZ`);
|
||||||
|
|||||||
@@ -2,7 +2,14 @@ const router = require('express').Router();
|
|||||||
const { query } = require('../db');
|
const { query } = require('../db');
|
||||||
|
|
||||||
const STATUSES = ['draft', 'blocked', 'published'];
|
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 = {
|
const STATUS_TIMESTAMP = {
|
||||||
published: 'published_at',
|
published: 'published_at',
|
||||||
@@ -41,14 +48,15 @@ router.post('/', async (req, res, next) => {
|
|||||||
const { answer_type, difficulty_level, question_id,
|
const { answer_type, difficulty_level, question_id,
|
||||||
positive_statement_id, negative_statement_id, blocked_topic } = req.body;
|
positive_statement_id, negative_statement_id, blocked_topic } = req.body;
|
||||||
|
|
||||||
if (!answer_type || !ANSWER_TYPES.includes(answer_type))
|
const atErr = validateAnswerType(answer_type);
|
||||||
return res.status(400).json({ error: `answer_type required, must be one of: ${ANSWER_TYPES.join(', ')}` });
|
if (atErr) return res.status(400).json({ error: atErr });
|
||||||
|
const atArr = Array.isArray(answer_type) ? answer_type : [answer_type];
|
||||||
|
|
||||||
const result = await query(
|
const result = await query(
|
||||||
`INSERT INTO pairs
|
`INSERT INTO pairs
|
||||||
(answer_type, difficulty_level, question_id, positive_statement_id, negative_statement_id, blocked_topic)
|
(answer_type, difficulty_level, question_id, positive_statement_id, negative_statement_id, blocked_topic)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`,
|
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]
|
positive_statement_id || null, negative_statement_id || null, blocked_topic || null]
|
||||||
);
|
);
|
||||||
res.status(201).json(result.rows[0]);
|
res.status(201).json(result.rows[0]);
|
||||||
|
|||||||
@@ -58,16 +58,17 @@ router.post('/', async (req, res, next) => {
|
|||||||
const {
|
const {
|
||||||
negative_sentence_de, negative_sentence_en, negative_sentence_sv,
|
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,
|
||||||
blocked_topic,
|
blocked_topic, answer,
|
||||||
} = req.body;
|
} = req.body;
|
||||||
const result = await query(
|
const result = await query(
|
||||||
`INSERT INTO statements
|
`INSERT INTO statements
|
||||||
(negative_sentence_de, negative_sentence_en, negative_sentence_sv,
|
(negative_sentence_de, negative_sentence_en, negative_sentence_sv,
|
||||||
positive_sentence_de, positive_sentence_en, positive_sentence_sv, blocked_topic)
|
positive_sentence_de, positive_sentence_en, positive_sentence_sv,
|
||||||
VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING *`,
|
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,
|
[negative_sentence_de || null, negative_sentence_en || null, negative_sentence_sv || null,
|
||||||
positive_sentence_de || null, positive_sentence_en || null, positive_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: [] });
|
res.status(201).json({ ...result.rows[0], positive_word_ids: [], negative_word_ids: [] });
|
||||||
} catch (err) { next(err); }
|
} catch (err) { next(err); }
|
||||||
@@ -78,7 +79,8 @@ router.patch('/:id', async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
const allowed = ['status', 'blocked_topic', 'published_at', 'blocked_at',
|
const allowed = ['status', 'blocked_topic', 'published_at', 'blocked_at',
|
||||||
'negative_sentence_de', 'negative_sentence_en', 'negative_sentence_sv',
|
'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));
|
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 (!fields.length) return res.status(400).json({ error: 'No valid fields provided' });
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user