Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 4a5cf9c5 authored by 65160270's avatar 65160270
Browse files

update-cart

parent ac519176
Branches
No related tags found
No related merge requests found
...@@ -34,13 +34,6 @@ router.get('/', async (req, res) => { ...@@ -34,13 +34,6 @@ router.get('/', async (req, res) => {
// เพิ่มสินค้าลงตะกร้า // เพิ่มสินค้าลงตะกร้า
router.post('/add', async (req, res) => { router.post('/add', async (req, res) => {
console.log("Session User:", req.session.user);
console.log("Received Data:", req.body);
if (!req.session.user) {
return res.status(401).send('Unauthorized');
}
const { productId, quantity } = req.body; const { productId, quantity } = req.body;
try { try {
const [[product]] = await pool.query( const [[product]] = await pool.query(
...@@ -55,12 +48,14 @@ router.post('/add', async (req, res) => { ...@@ -55,12 +48,14 @@ router.post('/add', async (req, res) => {
return res.status(400).send('สินค้ามีจำนวนไม่เพียงพอ'); return res.status(400).send('สินค้ามีจำนวนไม่เพียงพอ');
} }
// ตรวจสอบว่าสินค้านี้มีอยู่ในตะกร้าหรือยัง
const [[existingItem]] = await pool.query( const [[existingItem]] = await pool.query(
'SELECT id, quantity FROM cart_items WHERE user_id = ? AND product_id = ?', 'SELECT id, quantity FROM cart_items WHERE session_id = ? AND product_id = ?',
[req.session.user.id, productId] [req.session.id, productId]
); );
if (existingItem) { if (existingItem) {
// อัปเดตจำนวนสินค้า
const newQuantity = existingItem.quantity + parseInt(quantity); const newQuantity = existingItem.quantity + parseInt(quantity);
if (newQuantity > product.stock) { if (newQuantity > product.stock) {
return res.status(400).send('สินค้ามีจำนวนไม่เพียงพอ'); return res.status(400).send('สินค้ามีจำนวนไม่เพียงพอ');
...@@ -70,35 +65,31 @@ router.post('/add', async (req, res) => { ...@@ -70,35 +65,31 @@ router.post('/add', async (req, res) => {
[newQuantity, existingItem.id] [newQuantity, existingItem.id]
); );
} else { } else {
// เพิ่มสินค้าใหม่
await pool.query( await pool.query(
'INSERT INTO cart_items (user_id, product_id, quantity) VALUES (?, ?, ?)', 'INSERT INTO cart_items (session_id, product_id, quantity) VALUES (?, ?, ?)',
[req.session.user.id, productId, parseInt(quantity)] [req.session.id, productId, parseInt(quantity)]
); );
} }
res.redirect('/cart'); res.redirect('/cart');
} catch (error) { } catch (error) {
console.error("Error adding to cart:", error); console.error(error);
res.status(500).send('Error adding to cart.'); res.status(500).send('Error adding to cart');
} }
}); });
// อัพเดทจำนวนสินค้าในตะกร้า // อัพเดทจำนวนสินค้าในตะกร้า
router.post('/update', async (req, res) => { router.post('/update', async (req, res) => {
console.log("Received Data:", req.body); console.log("Received Data:", req.body);
if (!req.session.user) {
return res.status(401).send('Unauthorized');
}
const { cartItemId, quantity } = req.body; const { cartItemId, quantity } = req.body;
try { try {
const [[cartItem]] = await pool.query( const [[cartItem]] = await pool.query(
`SELECT cart_items.*, products.price, products.stock `SELECT cart_items.*, products.price, products.stock
FROM cart_items FROM cart_items
JOIN products ON cart_items.product_id = products.id JOIN products ON cart_items.product_id = products.id
WHERE cart_items.id = ? AND cart_items.user_id = ?`, WHERE cart_items.id = ? AND cart_items.session_id = ?`,
[cartItemId, req.session.user.id] // ใช้ user_id [cartItemId, req.session.id]
); );
if (!cartItem) { if (!cartItem) {
...@@ -110,11 +101,11 @@ router.post('/update', async (req, res) => { ...@@ -110,11 +101,11 @@ router.post('/update', async (req, res) => {
} }
await pool.query( await pool.query(
'UPDATE cart_items SET quantity = ? WHERE id = ? AND user_id = ?', 'UPDATE cart_items SET quantity = ? WHERE id = ? AND session_id = ?',
[parseInt(quantity), cartItemId, req.session.user.id] [parseInt(quantity), cartItemId, req.session.id]
); );
const total = await calculateTotal(req.session.user.id); const total = await calculateTotal(req.session.id);
res.json({ total }); res.json({ total });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
...@@ -122,7 +113,6 @@ router.post('/update', async (req, res) => { ...@@ -122,7 +113,6 @@ router.post('/update', async (req, res) => {
} }
}); });
// ลบสินค้าออกจากตะกร้า // ลบสินค้าออกจากตะกร้า
router.post('/remove', async (req, res) => { router.post('/remove', async (req, res) => {
const { cartItemId } = req.body; const { cartItemId } = req.body;
......
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
<label>จำนวนสินค้า:</label> <label>จำนวนสินค้า:</label>
<input type="number" name="stock" required> <input type="number" name="stock" required>
<br> <br>
<label>คำบรรยายสินค้า:</label>
<textarea name="description" required><%= product.description %></textarea>
<br>
<label>เลือกไฟล์รูปสินค้า:</label> <label>เลือกไฟล์รูปสินค้า:</label>
<input type="file" name="image" required> <input type="file" name="image" required>
<br> <br>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment