feat: categories table, API, category field in articles

This commit is contained in:
Alexey Pavlov
2026-05-31 14:43:27 +03:00
parent b48c1854a2
commit e5e7e9ef98
4 changed files with 35 additions and 7 deletions
+28
View File
@@ -0,0 +1,28 @@
const express = require('express');
const router = express.Router();
const { query } = require('../config/db');
// GET /api/categories
router.get('/', async (_, res) => {
try {
const { rows } = await query('SELECT * FROM categories ORDER BY sort_order');
res.json(rows);
} catch (err) { res.status(500).json({ error: err.message }); }
});
// GET /api/categories/:slug/articles
router.get('/:slug/articles', async (req, res) => {
try {
const limit = Math.min(parseInt(req.query.limit) || 20, 100);
const offset = parseInt(req.query.offset) || 0;
const { rows } = await query(
`SELECT id,slug,title,excerpt,cover_url,tags,category,author,reading_time,published_at
FROM articles WHERE status='published' AND category=$1
ORDER BY published_at DESC LIMIT $2 OFFSET $3`,
[req.params.slug, limit, offset]
);
res.json(rows);
} catch (err) { res.status(500).json({ error: err.message }); }
});
module.exports = router;