fix: POST /api/words akzeptiert conc_m + ON CONFLICT (titel_en) DO UPDATE
Ermöglicht sicheres CSV-Upsert via API (Brysbaert-Import). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -71,18 +71,23 @@ function autoTranslatedStatus(row) {
|
|||||||
// POST /api/words
|
// POST /api/words
|
||||||
router.post('/', async (req, res, next) => {
|
router.post('/', async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const { titel_de, titel_en, titel_sv, difficulty_level, status } = req.body;
|
const { titel_de, titel_en, titel_sv, difficulty_level, status, conc_m } = req.body;
|
||||||
if (status && !STATUSES.includes(status))
|
if (status && !STATUSES.includes(status))
|
||||||
return res.status(400).json({ error: `status must be one of: ${STATUSES.join(', ')}` });
|
return res.status(400).json({ error: `status must be one of: ${STATUSES.join(', ')}` });
|
||||||
// Auto: alle 3 Sprachen direkt mitgeliefert + kein expliziter Status → 'translated'
|
// Auto: alle 3 Sprachen direkt mitgeliefert + kein expliziter Status → 'translated'
|
||||||
const allLangs = titel_de && titel_en && titel_sv;
|
const allLangs = titel_de && titel_en && titel_sv;
|
||||||
const effectiveStatus = status || (allLangs ? 'translated' : 'requested');
|
const effectiveStatus = status || (allLangs ? 'translated' : 'requested');
|
||||||
const result = await query(
|
const result = await query(
|
||||||
`INSERT INTO words (titel_de, titel_en, titel_sv, difficulty_level, status, requested_at)
|
`INSERT INTO words (titel_de, titel_en, titel_sv, difficulty_level, status, conc_m, requested_at)
|
||||||
VALUES ($1, $2, $3, $4, $5, NOW()) RETURNING *`,
|
VALUES ($1, $2, $3, $4, $5, $6, NOW())
|
||||||
[titel_de || null, titel_en || null, titel_sv || null, difficulty_level || null, effectiveStatus]
|
ON CONFLICT (titel_en) DO UPDATE SET conc_m = EXCLUDED.conc_m
|
||||||
|
RETURNING *, (xmax = 0) AS is_insert`,
|
||||||
|
[titel_de || null, titel_en || null, titel_sv || null,
|
||||||
|
difficulty_level || null, effectiveStatus, conc_m ?? null]
|
||||||
);
|
);
|
||||||
res.status(201).json({ ...result.rows[0], picture_ids: [], category_ids: [] });
|
const row = result.rows[0];
|
||||||
|
const { is_insert: _, ...word } = row;
|
||||||
|
res.status(row.is_insert ? 201 : 200).json({ ...word, picture_ids: [], category_ids: [] });
|
||||||
} catch (err) { next(err); }
|
} catch (err) { next(err); }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user