diff --git a/app.js b/app.js index e8d94816ec97ae00b200a80e2c20e668100d636e..1c162ab245c100611669b02eaa1cba3544829355 100644 --- a/app.js +++ b/app.js @@ -68,6 +68,9 @@ app.get('/register', (req, res) => { res.sendFile(path.join(__dirname, 'public', 'register.html')); }); +app.get('/post-product', (req, res) => { + res.sendFile(path.join(__dirname, 'public', 'post-product.html')); +}); // Route สำหรับการ logout app.get('/logout', (req, res) => { // ลบข้อมูลใน session @@ -268,6 +271,48 @@ app.get('/dashboard', isAuthenticated, (req, res) => { res.status(200).json({ message: `Welcome ${req.session.user.username}` }); }); +// ให้บริการไฟล์ static เช่น รูปภาพที่ถูกอัพโหลด +app.use('/uploads', express.static(path.join(__dirname, 'uploads'))); + +// ตั้งค่าการจัดเก็บไฟล์ +const storage = multer.diskStorage({ + destination: function(req, file, cb) { + cb(null, './uploads/'); // กำหนดโฟลเดอร์ที่ใช้เก็บไฟล์ + }, + filename: function(req, file, cb) { + cb(null, Date.now() + path.extname(file.originalname)); // ตั้งชื่อไฟล์ใหม่ (ใช้ timestamp) + } +}); + +const upload = multer({ storage: storage }); + + +// ฟอร์มสำหรับโพสต์สินค้าที่รองรับการอัพโหลดไฟล์ +app.post('/api/products', upload.single('productImg'), (req, res) => { + const { productName, productPrice } = req.body; + const productImg = req.file ? `/uploads/${req.file.filename}` : null; + const userId = req.session.user.id; // ค่าของ user_id จาก session หรือ JWT + + if (!productName || !productPrice || !productImg) { + return res.status(400).json({ message: 'All fields are required' }); + } + + // คำสั่ง SQL สำหรับเพิ่มสินค้าใหม่ในฐานข้อมูล + const query = ` + INSERT INTO products (product_name, product_price, product_img, user_id) + VALUES (?, ?, ?, ?) + `; + + // บันทึกข้อมูลสินค้าในฐานข้อมูล + pool.query(query, [productName, productPrice, productImg, userId], (err, results) => { + if (err) { + console.error('Error adding product:', err); + return res.status(500).send('Error adding product'); + } + res.json({ message: 'Product added successfully', productId: results.insertId }); + }); +}); + // Start the server const port = process.env.PORT || 3000; app.listen(port, () => { diff --git a/public/Post.html b/public/Post.html new file mode 100644 index 0000000000000000000000000000000000000000..61f1720ebe30afbdab93abfd2313d0b9f7db21d2 --- /dev/null +++ b/public/Post.html @@ -0,0 +1,54 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Post a New Product</title> + <link rel="stylesheet" href="styles/login®ister.css"> +</head> +<body> + <h1>Post a New Product</h1> + + <!-- ฟอร์มสำหรับโพสต์สินค้า --> + <form id="postProductForm" action="/api/products" method="POST" enctype="multipart/form-data"> + <label for="productName">Product Name:</label> + <input type="text" id="productName" name="productName" required> + + <label for="productPrice">Price:</label> + <input type="number" id="productPrice" name="productPrice" required> + + <label for="productImg">Product Image:</label> + <input type="file" id="productImg" name="productImg" required> + + <button type="submit">Post Product</button> + </form> + + <script> + // การส่งข้อมูลฟอร์มโดยใช้ JavaScript (ใช้ fetch API) + const form = document.getElementById('postProductForm'); + form.addEventListener('submit', async function(event) { + event.preventDefault(); // ป้องกันการ submit ฟอร์มแบบปกติ + + const formData = new FormData(form); // สร้าง FormData จากฟอร์ม + try { + const response = await fetch('/api/products', { + method: 'POST', + body: formData + }); + + const data = await response.json(); + if (response.ok) { + alert('Product posted successfully!'); + console.log(data); + } else { + alert('Failed to post product!'); + console.error(data); + } + } catch (error) { + console.error('Error:', error); + alert('An error occurred while posting the product.'); + } + }); + </script> +</body> +</html> diff --git a/public/index.html b/public/index.html index 2109e18f14c9fd80aa039b03e61199e07e8f0b2c..1a056c0aca086a93e9e8091f302792ee0e33cf33 100644 --- a/public/index.html +++ b/public/index.html @@ -27,6 +27,10 @@ </div> </header> + <div class="Post"> + <button onclick="window.location.href='Post.html'">Post New Product</button> + </div> + <div class="products" id="product-list"> <!-- Products will be dynamically loaded here --> </div>