Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 22e17c17 authored by 65160381's avatar 65160381
Browse files

9.7

parent 3d69bd48
No related branches found
No related tags found
No related merge requests found
Pipeline #622 passed with warnings
......@@ -125,6 +125,30 @@ app.post('/login', async (req, res) => {
}
});
app.post('/api/products', async (req, res) => {
if (!req.session.user) {
return res.status(401).send('User not logged in');
}
const { product_name, product_price, product_img } = req.body;
const userId = req.session.user.id; // ใช้ข้อมูล user จาก session
if (!product_name || !product_price || !product_img) {
return res.status(400).send('All fields are required');
}
try {
// Insert ข้อมูลสินค้าในฐานข้อมูล
const query = 'INSERT INTO products (product_name, product_price, product_img, user_id) VALUES (?, ?, ?, ?)';
await pool.query(query, [product_name, product_price, product_img, userId]);
res.status(200).send('Product posted successfully');
} catch (error) {
console.error('Error adding product:', error);
res.status(500).send('Error posting product');
}
});
app.get('/api/getUser', (req, res) => {
if (req.session.user) {
res.json({
......
......@@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Post a Product</title>
<link rel="stylesheet" href="styles.css"> <!-- คุณสามารถเพิ่ม CSS ตามที่คุณต้องการ -->
<link rel="stylesheet" href="home.css"> <!-- คุณสามารถเพิ่ม CSS ตามที่คุณต้องการ -->
</head>
<body>
<header>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment