This commit is contained in:
2025-09-30 22:10:00 +02:00
parent 5856eda62b
commit f36fe6276c
5 changed files with 348 additions and 236 deletions

View File

@@ -211,7 +211,18 @@ app.get('/posts', async (_req, res) => {
app.get('/posts/:id', async (req, res) => {
const { id } = req.params
try {
const result = await query('SELECT * FROM blog_posts WHERE id = $1', [id])
// Try to parse as integer ID first, otherwise treat as slug
const numericId = parseInt(id, 10)
let result
if (!isNaN(numericId) && numericId.toString() === id) {
// It's a numeric ID
result = await query('SELECT * FROM blog_posts WHERE id = $1', [numericId])
} else {
// It's a slug
result = await query('SELECT * FROM blog_posts WHERE slug = $1', [id])
}
if (result.rows.length === 0) {
return res.status(404).json({ error: 'Post not found' })
}