forked from admin/zeropost-engine
feat: PATCH/DELETE /api/articles/:id, GET /api/articles/id/:id
This commit is contained in:
@@ -50,6 +50,54 @@ router.post('/generate', async (req, res) => {
|
||||
});
|
||||
|
||||
|
||||
// GET /api/articles/id/:id — одна статья по числовому id
|
||||
router.get('/id/:id', async (req, res) => {
|
||||
try {
|
||||
const { query } = require('../config/db');
|
||||
const { rows } = await query('SELECT * FROM articles WHERE id=$1', [req.params.id]);
|
||||
if (!rows.length) return res.status(404).json({ error: 'Not found' });
|
||||
res.json(rows[0]);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// PATCH /api/articles/:id — обновить статью
|
||||
router.patch('/:id', async (req, res) => {
|
||||
try {
|
||||
const { query } = require('../config/db');
|
||||
const { title, excerpt, content, tags, status, seo_title, seo_descr, cover_url } = req.body;
|
||||
const fields = [];
|
||||
const vals = [];
|
||||
let i = 1;
|
||||
if (title !== undefined) { fields.push(`title=${i++}`); vals.push(title); }
|
||||
if (excerpt !== undefined) { fields.push(`excerpt=${i++}`); vals.push(excerpt); }
|
||||
if (content !== undefined) { fields.push(`content=${i++}`); vals.push(content); }
|
||||
if (tags !== undefined) { fields.push(`tags=${i++}`); vals.push(tags); }
|
||||
if (status !== undefined) { fields.push(`status=${i++}`); vals.push(status); }
|
||||
if (seo_title !== undefined) { fields.push(`seo_title=${i++}`); vals.push(seo_title); }
|
||||
if (seo_descr !== undefined) { fields.push(`seo_descr=${i++}`); vals.push(seo_descr); }
|
||||
if (cover_url !== undefined) { fields.push(`cover_url=${i++}`); vals.push(cover_url); }
|
||||
if (!fields.length) return res.status(400).json({ error: 'Nothing to update' });
|
||||
fields.push(`updated_at=NOW()`);
|
||||
vals.push(req.params.id);
|
||||
const { rows } = await query(
|
||||
`UPDATE articles SET ${fields.join(', ')} WHERE id=${i} RETURNING *`,
|
||||
vals
|
||||
);
|
||||
if (!rows.length) return res.status(404).json({ error: 'Not found' });
|
||||
res.json(rows[0]);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// DELETE /api/articles/:id — удалить статью
|
||||
router.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const { query } = require('../config/db');
|
||||
const { rowCount } = await query('DELETE FROM articles WHERE id=$1', [req.params.id]);
|
||||
if (!rowCount) return res.status(404).json({ error: 'Not found' });
|
||||
res.json({ ok: true });
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// POST /api/articles/backfill-covers — досгенерировать обложки для статей без них
|
||||
router.post('/backfill-covers', async (req, res) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user