Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 298d031e authored by 65160268's avatar 65160268
Browse files

Update 2 files

- /views/thread-view.ejs
- /server.js
parent 9ccd2919
No related branches found
No related tags found
No related merge requests found
...@@ -32,7 +32,7 @@ const connection = mysql.createConnection({ ...@@ -32,7 +32,7 @@ const connection = mysql.createConnection({
connection.connect(err => { connection.connect(err => {
if (err) { if (err) {
console.error('Error connecting to MySQL:', err); console.error('Error connecting to MySQL:', err);
process.exit(1); process.exit(1);
} }
console.log('Connected to MySQL Successfully'); console.log('Connected to MySQL Successfully');
...@@ -195,17 +195,28 @@ app.get('/thread/delete/:id', isAuthenticated, (req, res) => { ...@@ -195,17 +195,28 @@ app.get('/thread/delete/:id', isAuthenticated, (req, res) => {
// หน้าแสดง thread พร้อมคอมเมนต์ // หน้าแสดง thread พร้อมคอมเมนต์
app.get('/thread/:id', (req, res) => { app.get('/thread/:id', (req, res) => {
const threadId = req.params.id; const threadId = req.params.id;
connection.query( connection.query(
`SELECT threads.*, users.username AS ownerName FROM threads JOIN users ON threads.user_id = users.id WHERE threads.id = ?`, `SELECT threads.*, users.username AS ownerName
FROM threads
JOIN users ON threads.user_id = users.id
WHERE threads.id = ?`,
[threadId], [threadId],
(err, results) => { (err, results) => {
if (err || results.length === 0) return res.send('ไม่พบ thread ที่ต้องการ'); if (err || results.length === 0) return res.send('ไม่พบ thread ที่ต้องการ');
const thread = results[0]; const thread = results[0];
connection.query( connection.query(
`SELECT comments.*, users.username AS commentUser FROM comments JOIN users ON comments.user_id = users.id WHERE comments.thread_id = ? ORDER BY comments.id DESC`, `SELECT comments.*, users.username AS commentUser, users.email
FROM comments
JOIN users ON comments.user_id = users.id
WHERE comments.thread_id = ?
ORDER BY comments.id DESC`,
[threadId], [threadId],
(err2, comments) => { (err2, comments) => {
if (err2) return res.send('เกิดข้อผิดพลาดในการดึงคอมเมนต์'); if (err2) return res.send('เกิดข้อผิดพลาดในการดึงคอมเมนต์');
res.render('thread-view', { res.render('thread-view', {
username: req.session.username, username: req.session.username,
userId: req.session.userId, userId: req.session.userId,
...@@ -218,6 +229,7 @@ app.get('/thread/:id', (req, res) => { ...@@ -218,6 +229,7 @@ app.get('/thread/:id', (req, res) => {
); );
}); });
// เพิ่มคอมเมนต์ใหม่ // เพิ่มคอมเมนต์ใหม่
app.post('/thread/:id/comment', isAuthenticated, (req, res) => { app.post('/thread/:id/comment', isAuthenticated, (req, res) => {
const threadId = req.params.id; const threadId = req.params.id;
......
...@@ -54,9 +54,9 @@ ...@@ -54,9 +54,9 @@
<!-- แสดงชื่อผู้คอมเมนต์และข้อความ --> <!-- แสดงชื่อผู้คอมเมนต์และข้อความ -->
<strong><%= c.commentUser %>:</strong> <%= c.comment %> <strong><%= c.commentUser %>:</strong> <%= c.comment %>
<!-- แสดง Comment ID เล็ก ๆ --> <!-- แสดง Email -->
<span class="text-muted" style="font-size: 0.8em;"> <span class="text-muted" style="font-size: 0.8em;">
(Comment ID: <%= c.id %>) <b>(User Email: <%= c.email %>)</b>
</span> </span>
<!-- ปุ่มลบคอมเมนต์ (เฉพาะเจ้าของคอมเมนต์) --> <!-- ปุ่มลบคอมเมนต์ (เฉพาะเจ้าของคอมเมนต์) -->
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment