diff --git a/routes/posts.js b/routes/posts.js index 9286dd0611a8306ef0731731f7218ad0693067b2..1285b885375ab6898270d658f41d94a93ed7a83f 100644 --- a/routes/posts.js +++ b/routes/posts.js @@ -143,4 +143,23 @@ router.get('/search', async (req, res) => { } }); +router.get('/:id', async (req, res) => { + try { + const postId = req.params.id; + const [rows] = await db.execute( + 'SELECT posts.*, users.username, categories.name AS category_name FROM posts JOIN users ON posts.user_id = users.id JOIN categories ON posts.category_id = categories.id WHERE posts.id = ?', + [postId] + ); + + if (rows.length === 0) { + return res.status(404).send('Post not found'); + } + + res.render('posts/show', { post: rows[0] }); + } catch (error) { + console.error(error); + res.status(500).send('Server error'); + } + }); + module.exports = router; diff --git a/views/posts/show.ejs b/views/posts/show.ejs new file mode 100644 index 0000000000000000000000000000000000000000..303f2020129e27c1686964b31e3fb02a2c84d696 --- /dev/null +++ b/views/posts/show.ejs @@ -0,0 +1,18 @@ +<%- include('../partials/header') %> +<link rel="stylesheet" href="/css/main.css"> +<link rel="stylesheet" href="/css/pages/post.css"> + +<div class="main-content"> + <article class="post-detail card shadow-sm"> + <div class="card-body"> + <h1 class="card-title fw-bold text-primary"><%= post.title %></h1> + <div class="post-meta text-muted mb-3"> + <span><i class="bi bi-person-circle"></i> <%= post.username %></span> | + <span><i class="bi bi-tag"></i> <%= post.category_name %></span> | + <span><i class="bi bi-clock"></i> <%= new Date(post.created_at).toLocaleString() %></span> + </div> + <p class="card-text"><%= post.content %></p> + </div> + </article> + <a href="/posts" class="btn btn-secondary mt-3"><i class="bi bi-arrow-left"></i> Back to all posts</a> +</div>