docs: 更新项目文档,完善需求和技术细节
This commit is contained in:
@@ -2,6 +2,70 @@ const express = require('express');
|
||||
const router = express.Router();
|
||||
const dbConnector = require('../utils/dbConnector');
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/v1/cart:
|
||||
* get:
|
||||
* summary: 获取用户购物车
|
||||
* description: 获取当前用户的购物车商品列表及总计信息
|
||||
* tags:
|
||||
* - 购物车管理
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 成功获取购物车信息
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 200
|
||||
* message:
|
||||
* type: string
|
||||
* example: 获取成功
|
||||
* data:
|
||||
* type: object
|
||||
* properties:
|
||||
* items:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/CartItem'
|
||||
* total_amount:
|
||||
* type: number
|
||||
* example: 99.9
|
||||
* total_quantity:
|
||||
* type: integer
|
||||
* example: 3
|
||||
* 401:
|
||||
* description: 未授权访问
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 401
|
||||
* message:
|
||||
* type: string
|
||||
* example: 未授权访问
|
||||
* 500:
|
||||
* description: 服务器内部错误
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 500
|
||||
* message:
|
||||
* type: string
|
||||
* example: 服务器内部错误
|
||||
*/
|
||||
// 获取用户购物车
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
@@ -36,6 +100,101 @@ router.get('/', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/v1/cart/items:
|
||||
* post:
|
||||
* summary: 添加商品到购物车
|
||||
* description: 将指定商品添加到当前用户的购物车中
|
||||
* tags:
|
||||
* - 购物车管理
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required:
|
||||
* - product_id
|
||||
* - quantity
|
||||
* properties:
|
||||
* product_id:
|
||||
* type: integer
|
||||
* example: 1
|
||||
* description: 商品ID
|
||||
* quantity:
|
||||
* type: integer
|
||||
* example: 2
|
||||
* description: 商品数量
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 成功添加到购物车
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 200
|
||||
* message:
|
||||
* type: string
|
||||
* example: 添加成功
|
||||
* 400:
|
||||
* description: 请求参数错误
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 400
|
||||
* message:
|
||||
* type: string
|
||||
* example: 商品数量必须大于0
|
||||
* 401:
|
||||
* description: 未授权访问
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 401
|
||||
* message:
|
||||
* type: string
|
||||
* example: 未授权访问
|
||||
* 404:
|
||||
* description: 商品不存在
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 404
|
||||
* message:
|
||||
* type: string
|
||||
* example: 商品不存在
|
||||
* 500:
|
||||
* description: 服务器内部错误
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 500
|
||||
* message:
|
||||
* type: string
|
||||
* example: 服务器内部错误
|
||||
*/
|
||||
// 添加商品到购物车
|
||||
router.post('/items', async (req, res) => {
|
||||
try {
|
||||
@@ -50,33 +209,38 @@ router.post('/items', async (req, res) => {
|
||||
message: '商品不存在'
|
||||
});
|
||||
}
|
||||
|
||||
// 检查购物车是否已有该商品
|
||||
|
||||
if (quantity <= 0) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '商品数量必须大于0'
|
||||
});
|
||||
}
|
||||
|
||||
// 检查购物车中是否已存在该商品
|
||||
const existingItem = await dbConnector.query(
|
||||
'SELECT * FROM cart_items WHERE user_id = ? AND product_id = ?',
|
||||
[userId, product_id]
|
||||
);
|
||||
|
||||
|
||||
if (existingItem.length > 0) {
|
||||
// 更新数量
|
||||
const newQuantity = existingItem[0].quantity + quantity;
|
||||
await dbConnector.query(
|
||||
'UPDATE cart_items SET quantity = quantity + ?, updated_at = NOW() WHERE id = ?',
|
||||
[quantity, existingItem[0].id]
|
||||
'UPDATE cart_items SET quantity = ? WHERE id = ?',
|
||||
[newQuantity, existingItem[0].id]
|
||||
);
|
||||
} else {
|
||||
// 新增商品
|
||||
// 添加新商品到购物车
|
||||
await dbConnector.query(
|
||||
'INSERT INTO cart_items (user_id, product_id, quantity, created_at, updated_at) VALUES (?, ?, ?, NOW(), NOW())',
|
||||
'INSERT INTO cart_items (user_id, product_id, quantity) VALUES (?, ?, ?)',
|
||||
[userId, product_id, quantity]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '添加成功',
|
||||
data: {
|
||||
cart_item_id: existingItem.length > 0 ? existingItem[0].id : null
|
||||
}
|
||||
message: '添加成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('添加购物车失败:', error);
|
||||
@@ -88,31 +252,136 @@ router.post('/items', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/v1/cart/items/{id}:
|
||||
* put:
|
||||
* summary: 更新购物车商品数量
|
||||
* description: 更新购物车中指定商品的数量
|
||||
* tags:
|
||||
* - 购物车管理
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: 购物车商品项ID
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required:
|
||||
* - quantity
|
||||
* properties:
|
||||
* quantity:
|
||||
* type: integer
|
||||
* example: 3
|
||||
* description: 新的商品数量
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 成功更新购物车商品数量
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 200
|
||||
* message:
|
||||
* type: string
|
||||
* example: 更新成功
|
||||
* 400:
|
||||
* description: 请求参数错误
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 400
|
||||
* message:
|
||||
* type: string
|
||||
* example: 商品数量必须大于0
|
||||
* 401:
|
||||
* description: 未授权访问
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 401
|
||||
* message:
|
||||
* type: string
|
||||
* example: 未授权访问
|
||||
* 404:
|
||||
* description: 购物车商品项不存在
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 404
|
||||
* message:
|
||||
* type: string
|
||||
* example: 购物车商品项不存在
|
||||
* 500:
|
||||
* description: 服务器内部错误
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 500
|
||||
* message:
|
||||
* type: string
|
||||
* example: 服务器内部错误
|
||||
*/
|
||||
// 更新购物车商品数量
|
||||
router.put('/items/:id', async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { quantity } = req.body;
|
||||
const userId = req.user.id;
|
||||
|
||||
// 检查购物车项是否存在
|
||||
|
||||
if (quantity <= 0) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '商品数量必须大于0'
|
||||
});
|
||||
}
|
||||
|
||||
// 检查购物车商品项是否存在且属于当前用户
|
||||
const cartItem = await dbConnector.query(
|
||||
'SELECT * FROM cart_items WHERE id = ? AND user_id = ?',
|
||||
[id, userId]
|
||||
);
|
||||
|
||||
|
||||
if (cartItem.length === 0) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '购物车项不存在'
|
||||
message: '购物车商品项不存在'
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 更新数量
|
||||
await dbConnector.query(
|
||||
'UPDATE cart_items SET quantity = ?, updated_at = NOW() WHERE id = ?',
|
||||
'UPDATE cart_items SET quantity = ? WHERE id = ?',
|
||||
[quantity, id]
|
||||
);
|
||||
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '更新成功'
|
||||
@@ -127,23 +396,105 @@ router.put('/items/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/v1/cart/items/{id}:
|
||||
* delete:
|
||||
* summary: 删除购物车商品
|
||||
* description: 从购物车中删除指定商品
|
||||
* tags:
|
||||
* - 购物车管理
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: 购物车商品项ID
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 成功删除购物车商品
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 200
|
||||
* message:
|
||||
* type: string
|
||||
* example: 删除成功
|
||||
* 401:
|
||||
* description: 未授权访问
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 401
|
||||
* message:
|
||||
* type: string
|
||||
* example: 未授权访问
|
||||
* 404:
|
||||
* description: 购物车商品项不存在
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 404
|
||||
* message:
|
||||
* type: string
|
||||
* example: 购物车商品项不存在
|
||||
* 500:
|
||||
* description: 服务器内部错误
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 500
|
||||
* message:
|
||||
* type: string
|
||||
* example: 服务器内部错误
|
||||
*/
|
||||
// 删除购物车商品
|
||||
router.delete('/items/:id', async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const userId = req.user.id;
|
||||
|
||||
await dbConnector.query(
|
||||
'DELETE FROM cart_items WHERE id = ? AND user_id = ?',
|
||||
|
||||
// 检查购物车商品项是否存在且属于当前用户
|
||||
const cartItem = await dbConnector.query(
|
||||
'SELECT * FROM cart_items WHERE id = ? AND user_id = ?',
|
||||
[id, userId]
|
||||
);
|
||||
|
||||
|
||||
if (cartItem.length === 0) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '购物车商品项不存在'
|
||||
});
|
||||
}
|
||||
|
||||
// 删除购物车商品项
|
||||
await dbConnector.query('DELETE FROM cart_items WHERE id = ?', [id]);
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '删除成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('删除购物车失败:', error);
|
||||
console.error('删除购物车商品失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误',
|
||||
|
||||
Reference in New Issue
Block a user