Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 16287b80 authored by 65160381's avatar 65160381
Browse files

5

parent 4ab2b18e
Branches
No related tags found
No related merge requests found
Pipeline #584 passed with warnings
......@@ -49,9 +49,13 @@ pool.getConnection()
});
// ตัวอย่าง route ทดสอบ
const indexController = require('./controllers/indexController');
app.get('/', indexController.getProducts);
app.get('/', (req, res) => {
if (req.session.user) {
res.sendFile(path.join(__dirname, 'public', 'index.ejs'));
} else {
}
});
// Start the server
const port = process.env.PORT || 3000;
......
const pool = require('../db');
exports.getProducts = async (req, res) => {
try {
const [rows] = await pool.query('SELECT * FROM products');
res.render('index', { products: rows });
} catch (err) {
res.status(500).send('Database error: ' + err.message);
}
};
exports.getProductDetail = async (req, res) => {
const productId = req.params.id;
try {
const [rows] = await pool.query(`
SELECT p.*, u.email AS owner
FROM products p
LEFT JOIN users u ON p.owner = u.email
WHERE p.product_id = ?`, [productId]);
if (rows.length === 0) {
return res.status(404).send('ไม่พบสินค้านี้');
}
res.render('product', { product: rows[0], currentUserEmail: req.session.userIdEmail || '' });
} catch (err) {
res.status(500).send('Database error: ' + err.message);
}
};
\ No newline at end of file
......@@ -116,4 +116,4 @@
displayUsername(); // Display user info when page loads
</script>
</body>
</html>
\ No newline at end of file
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment