DB ready
This commit is contained in:
@@ -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' })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user