Gitlab@Informatics

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

9

parent 8c5eaa7d
No related branches found
No related tags found
No related merge requests found
Pipeline #612 passed with warnings
...@@ -68,6 +68,9 @@ app.get('/register', (req, res) => { ...@@ -68,6 +68,9 @@ app.get('/register', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'register.html')); 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 // Route สำหรับการ logout
app.get('/logout', (req, res) => { app.get('/logout', (req, res) => {
// ลบข้อมูลใน session // ลบข้อมูลใน session
...@@ -268,6 +271,48 @@ app.get('/dashboard', isAuthenticated, (req, res) => { ...@@ -268,6 +271,48 @@ app.get('/dashboard', isAuthenticated, (req, res) => {
res.status(200).json({ message: `Welcome ${req.session.user.username}` }); 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 // Start the server
const port = process.env.PORT || 3000; const port = process.env.PORT || 3000;
app.listen(port, () => { app.listen(port, () => {
......
<!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&register.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>
...@@ -27,6 +27,10 @@ ...@@ -27,6 +27,10 @@
</div> </div>
</header> </header>
<div class="Post">
<button onclick="window.location.href='Post.html'">Post New Product</button>
</div>
<div class="products" id="product-list"> <div class="products" id="product-list">
<!-- Products will be dynamically loaded here --> <!-- Products will be dynamically loaded here -->
</div> </div>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment