112 lines
4.5 KiB
JavaScript
112 lines
4.5 KiB
JavaScript
const express = require('express');
|
|
const db = require('../db-sqlite');
|
|
const { authenticate } = require('../middleware/auth');
|
|
|
|
module.exports = function(app) {
|
|
const router = express.Router();
|
|
|
|
router.get('/', authenticate, async (req, res) => {
|
|
try {
|
|
const productsResult = await db.query('SELECT id, name, category_id, unit, price, stock, min_stock, created_at, updated_at FROM products');
|
|
const products = productsResult.rows;
|
|
res.json({ success: true, data: products, count: products.length });
|
|
} catch (error) {
|
|
console.error('获取商品列表失败:', error);
|
|
res.status(500).json({ success: false, message: '获取商品列表失败' });
|
|
}
|
|
});
|
|
|
|
router.get('/template', authenticate, async (req, res) => {
|
|
try {
|
|
const templateResult = await db.query('SELECT id, name, category_id, unit, price, stock, min_stock, created_at, updated_at FROM products WHERE id = 1');
|
|
const template = templateResult.rows[0];
|
|
res.json({ success: true, data: template });
|
|
} catch (error) {
|
|
console.error('获取商品模板失败:', error);
|
|
res.status(500).json({ success: false, message: '获取商品模板失败' });
|
|
}
|
|
});
|
|
|
|
router.get('/:id', authenticate, async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const productResult = await db.query('SELECT id, name, category_id, unit, price, stock, min_stock, created_at, updated_at FROM products WHERE id = ?', [id]);
|
|
|
|
if (productResult.rows.length === 0) {
|
|
return res.status(404).json({ success: false, message: '商品不存在' });
|
|
}
|
|
|
|
const product = productResult.rows[0];
|
|
res.json({ success: true, data: product });
|
|
} catch (error) {
|
|
console.error('获取商品详情失败:', error);
|
|
res.status(500).json({ success: false, message: '获取商品详情失败' });
|
|
}
|
|
});
|
|
|
|
router.post('/', authenticate, async (req, res) => {
|
|
try {
|
|
const { name, category_id, unit, price, stock, min_stock } = req.body;
|
|
|
|
if (!name) {
|
|
return res.status(400).json({ success: false, message: '商品名称为必填项' });
|
|
}
|
|
|
|
const result = await db.query(
|
|
'INSERT INTO products (name, category_id, unit, price, stock, min_stock,
|
|
created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)',
|
|
[name, category_id, unit, price, stock, min_stock]
|
|
);
|
|
|
|
const newProductResult = await db.query('SELECT id, name, category_id, unit, price, stock, min_stock, created_at, updated_at FROM products WHERE id = ?', [result.lastID]);
|
|
const newProduct = newProductResult.rows[0];
|
|
|
|
console.log(`商品 ${name} 创建成功后端,由 ${req.user.username} 操作`);
|
|
res.json({ success: true, data: newProduct });
|
|
} catch (error) {
|
|
console.error('创建商品失败:', error);
|
|
res.status(500).json({ success: false, message: '创建商品失败' });
|
|
}
|
|
});
|
|
|
|
router.put('/:id', authenticate, async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const { name, category_id, unit, price, stock, min_stock } = req.body;
|
|
|
|
await db.query(
|
|
'UPDATE products SET name = ?, category_id = ?, unit = ?, price = ?, stock = ?, min_stock = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?',
|
|
[name, category_id, unit, price, stock, min_stock, id]
|
|
);
|
|
|
|
const updatedProductResult = await db.query('SELECT id, name, category_id, unit, price, stock, min_stock, created_at, updated_at FROM products WHERE id = ?', [id]);
|
|
const updatedProduct = updatedProductResult.rows[0];
|
|
|
|
res.json({ success: true, data: updatedProduct });
|
|
} catch (error) {
|
|
console.error('更新商品信息失败:', error);
|
|
res.status(500).json({ success: false, message: '更新商品信息失败' });
|
|
}
|
|
});
|
|
|
|
router.delete('/:id', authenticate, async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
|
|
const existingProduct = await db.query('SELECT id FROM products WHERE id = ?', [id]);
|
|
if (existingProduct.rows.length === 0) {
|
|
return res.status(404).json({ success: false, message: '商品不存在' });
|
|
}
|
|
|
|
await db.query('DELETE FROM products WHERE id = ?', [id]);
|
|
|
|
res.json({ success: true, message: '商品删除成功' });
|
|
} catch (error) {
|
|
console.error('删除商品失败:', error);
|
|
res.status(500).json({ success: false, message: '删除商品失败' });
|
|
}
|
|
});
|
|
|
|
app.use('/api/products', router);
|
|
};
|