From 4d7f08b939b1b239fe94567da16fc1f56498e77d Mon Sep 17 00:00:00 2001
From: 65160258 <65160258@go.buu.ac.th>
Date: Fri, 21 Mar 2025 17:56:20 +0700
Subject: [PATCH] show

---
 routes/posts.js      | 19 +++++++++++++++++++
 views/posts/show.ejs | 18 ++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 views/posts/show.ejs

diff --git a/routes/posts.js b/routes/posts.js
index 9286dd0..1285b88 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 0000000..303f202
--- /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>
-- 
GitLab