forked from admin/zeropost-engine
feat: categories table, API, category field in articles
This commit is contained in:
@@ -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;
|
||||
Reference in New Issue
Block a user