From 22e17c1726a1d66e90d8fb8d3a7d40832c06e5d2 Mon Sep 17 00:00:00 2001
From: 65160381 <65160381@go.buu.ac.th>
Date: Tue, 25 Mar 2025 03:58:20 +0700
Subject: [PATCH] 9.7

---
 app.js                   | 24 ++++++++++++++++++++++++
 public/post-product.html |  2 +-
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/app.js b/app.js
index f22d64d..4b1d6b9 100644
--- a/app.js
+++ b/app.js
@@ -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({
diff --git a/public/post-product.html b/public/post-product.html
index 837d342..5a80f5d 100644
--- a/public/post-product.html
+++ b/public/post-product.html
@@ -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>
-- 
GitLab