bug修复
This commit is contained in:
132
routes/cart.js
132
routes/cart.js
@@ -473,71 +473,6 @@ router.put('/:id', auth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/cart/{id}:
|
||||
* delete:
|
||||
* summary: 删除购物车商品
|
||||
* tags: [Cart]
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: 购物车项ID
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 删除购物车商品成功
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* message:
|
||||
* type: string
|
||||
* 401:
|
||||
* description: 未授权
|
||||
* 404:
|
||||
* description: 购物车项不存在
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.delete('/:id', auth, async (req, res) => {
|
||||
try {
|
||||
const cartItemId = req.params.id;
|
||||
const userId = req.user.id;
|
||||
|
||||
// 检查购物车项是否存在且属于当前用户
|
||||
const [cartItems] = await getDB().execute(
|
||||
'SELECT id FROM cart_items WHERE id = ? AND user_id = ?',
|
||||
[cartItemId, userId]
|
||||
);
|
||||
|
||||
if (cartItems.length === 0) {
|
||||
return res.status(404).json({ success: false, message: '购物车项不存在' });
|
||||
}
|
||||
|
||||
// 删除购物车项
|
||||
await getDB().execute(
|
||||
'DELETE FROM cart_items WHERE id = ?',
|
||||
[cartItemId]
|
||||
);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: '删除购物车商品成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('删除购物车商品失败:', error);
|
||||
res.status(500).json({ success: false, message: '删除购物车商品失败' });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/cart/batch:
|
||||
@@ -654,6 +589,7 @@ router.delete('/clear', auth, async (req, res) => {
|
||||
success: true,
|
||||
message: '清空购物车成功'
|
||||
});
|
||||
console.log(11111111111111)
|
||||
} catch (error) {
|
||||
console.error('清空购物车失败:', error);
|
||||
res.status(500).json({ success: false, message: '清空购物车失败' });
|
||||
@@ -932,4 +868,70 @@ router.post('/checkout', auth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/cart/{id}:
|
||||
* delete:
|
||||
* summary: 删除购物车商品
|
||||
* tags: [Cart]
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: 购物车项ID
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 删除购物车商品成功
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* message:
|
||||
* type: string
|
||||
* 401:
|
||||
* description: 未授权
|
||||
* 404:
|
||||
* description: 购物车项不存在
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.delete('/:id', auth, async (req, res) => {
|
||||
try {
|
||||
console.log(111111111)
|
||||
const cartItemId = req.params.id;
|
||||
const userId = req.user.id;
|
||||
|
||||
// 检查购物车项是否存在且属于当前用户
|
||||
const [cartItems] = await getDB().execute(
|
||||
'SELECT id FROM cart_items WHERE id = ? AND user_id = ?',
|
||||
[cartItemId, userId]
|
||||
);
|
||||
|
||||
if (cartItems.length === 0) {
|
||||
return res.status(404).json({ success: false, message: '购物车项不存在' });
|
||||
}
|
||||
|
||||
// 删除购物车项
|
||||
await getDB().execute(
|
||||
'DELETE FROM cart_items WHERE id = ?',
|
||||
[cartItemId]
|
||||
);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: '删除购物车商品成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('删除购物车商品失败:', error);
|
||||
res.status(500).json({ success: false, message: '删除购物车商品失败' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user