feat: objects table with M2M words/pictures/pairs
- objects: status enum, JSONB selections, notes, blocked_topic, auto-timestamps - pairs placeholder table for future use - Junction tables: object_words, object_pictures, object_pairs - Full CRUD + link/unlink endpoints for all three relations - README updated Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
169
src/routes/objects.js
Normal file
169
src/routes/objects.js
Normal file
@@ -0,0 +1,169 @@
|
||||
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 o.*,
|
||||
COALESCE(json_agg(DISTINCT ow.word_id) FILTER (WHERE ow.word_id IS NOT NULL), '[]') AS word_ids,
|
||||
COALESCE(json_agg(DISTINCT op.picture_id) FILTER (WHERE op.picture_id IS NOT NULL), '[]') AS picture_ids,
|
||||
COALESCE(json_agg(DISTINCT opr.pair_id) FILTER (WHERE opr.pair_id IS NOT NULL), '[]') AS pair_ids
|
||||
FROM objects o
|
||||
LEFT JOIN object_words ow ON ow.object_id = o.id
|
||||
LEFT JOIN object_pictures op ON op.object_id = o.id
|
||||
LEFT JOIN object_pairs opr ON opr.object_id = o.id
|
||||
WHERE o.id = $1
|
||||
GROUP BY o.id`,
|
||||
[id]
|
||||
);
|
||||
return result.rows[0] || null;
|
||||
}
|
||||
|
||||
// GET /api/objects
|
||||
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 o.status = $3` : '';
|
||||
if (status) params.push(status);
|
||||
const result = await query(
|
||||
`SELECT o.*,
|
||||
COALESCE(json_agg(DISTINCT ow.word_id) FILTER (WHERE ow.word_id IS NOT NULL), '[]') AS word_ids,
|
||||
COALESCE(json_agg(DISTINCT op.picture_id) FILTER (WHERE op.picture_id IS NOT NULL), '[]') AS picture_ids,
|
||||
COALESCE(json_agg(DISTINCT opr.pair_id) FILTER (WHERE opr.pair_id IS NOT NULL), '[]') AS pair_ids
|
||||
FROM objects o
|
||||
LEFT JOIN object_words ow ON ow.object_id = o.id
|
||||
LEFT JOIN object_pictures op ON op.object_id = o.id
|
||||
LEFT JOIN object_pairs opr ON opr.object_id = o.id
|
||||
${where}
|
||||
GROUP BY o.id
|
||||
ORDER BY o.created_at DESC
|
||||
LIMIT $1 OFFSET $2`,
|
||||
params
|
||||
);
|
||||
res.json(result.rows);
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
// GET /api/objects/: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/objects
|
||||
router.post('/', async (req, res, next) => {
|
||||
try {
|
||||
const { selections, notes } = req.body;
|
||||
const result = await query(
|
||||
`INSERT INTO objects (selections, notes) VALUES ($1, $2) RETURNING *`,
|
||||
[selections ? JSON.stringify(selections) : null, notes || null]
|
||||
);
|
||||
res.status(201).json({ ...result.rows[0], word_ids: [], picture_ids: [], pair_ids: [] });
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
// PATCH /api/objects/:id
|
||||
router.patch('/:id', async (req, res, next) => {
|
||||
try {
|
||||
const allowed = ['status', 'selections', 'notes', 'blocked_topic', '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(', ')}` });
|
||||
|
||||
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 => f === 'selections' ? JSON.stringify(req.body[f]) : req.body[f]);
|
||||
values.push(req.params.id);
|
||||
|
||||
const result = await query(
|
||||
`UPDATE objects 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/objects/:id
|
||||
router.delete('/:id', async (req, res, next) => {
|
||||
try {
|
||||
const result = await query('DELETE FROM objects 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); }
|
||||
});
|
||||
|
||||
// --- Relations ---
|
||||
|
||||
// POST /api/objects/:id/words/:wordId
|
||||
router.post('/:id/words/:wordId', async (req, res, next) => {
|
||||
try {
|
||||
await query(`INSERT INTO object_words (object_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/objects/:id/words/:wordId
|
||||
router.delete('/:id/words/:wordId', async (req, res, next) => {
|
||||
try {
|
||||
await query(`DELETE FROM object_words WHERE object_id = $1 AND word_id = $2`,
|
||||
[req.params.id, req.params.wordId]);
|
||||
res.status(204).end();
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
// POST /api/objects/:id/pictures/:pictureId
|
||||
router.post('/:id/pictures/:pictureId', async (req, res, next) => {
|
||||
try {
|
||||
await query(`INSERT INTO object_pictures (object_id, picture_id) VALUES ($1, $2) ON CONFLICT DO NOTHING`,
|
||||
[req.params.id, req.params.pictureId]);
|
||||
res.status(204).end();
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
// DELETE /api/objects/:id/pictures/:pictureId
|
||||
router.delete('/:id/pictures/:pictureId', async (req, res, next) => {
|
||||
try {
|
||||
await query(`DELETE FROM object_pictures WHERE object_id = $1 AND picture_id = $2`,
|
||||
[req.params.id, req.params.pictureId]);
|
||||
res.status(204).end();
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
// POST /api/objects/:id/pairs/:pairId
|
||||
router.post('/:id/pairs/:pairId', async (req, res, next) => {
|
||||
try {
|
||||
await query(`INSERT INTO object_pairs (object_id, pair_id) VALUES ($1, $2) ON CONFLICT DO NOTHING`,
|
||||
[req.params.id, req.params.pairId]);
|
||||
res.status(204).end();
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
// DELETE /api/objects/:id/pairs/:pairId
|
||||
router.delete('/:id/pairs/:pairId', async (req, res, next) => {
|
||||
try {
|
||||
await query(`DELETE FROM object_pairs WHERE object_id = $1 AND pair_id = $2`,
|
||||
[req.params.id, req.params.pairId]);
|
||||
res.status(204).end();
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user