Add/update product table

This commit is contained in:
2025-01-09 20:47:17 -05:00
parent 7bf3852324
commit afe8510751
15 changed files with 1782 additions and 61 deletions

View File

@@ -6,11 +6,74 @@ const { importProductsFromCSV } = require('../utils/csvImporter');
// Configure multer for file uploads
const upload = multer({ dest: 'uploads/' });
// Get all products
// Get all products with pagination, filtering, and sorting
router.get('/', async (req, res) => {
const pool = req.app.locals.pool;
try {
const [rows] = await pool.query(`
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 50;
const offset = (page - 1) * limit;
const search = req.query.search || '';
const category = req.query.category || 'all';
const vendor = req.query.vendor || 'all';
const stockStatus = req.query.stockStatus || 'all';
const minPrice = parseFloat(req.query.minPrice) || 0;
const maxPrice = req.query.maxPrice ? parseFloat(req.query.maxPrice) : null;
const sortColumn = req.query.sortColumn || 'title';
const sortDirection = req.query.sortDirection === 'desc' ? 'DESC' : 'ASC';
// Build the WHERE clause
const conditions = ['visible = true'];
const params = [];
if (search) {
conditions.push('(title LIKE ? OR SKU LIKE ?)');
params.push(`%${search}%`, `%${search}%`);
}
if (category !== 'all') {
conditions.push('categories = ?');
params.push(category);
}
if (vendor !== 'all') {
conditions.push('vendor = ?');
params.push(vendor);
}
if (stockStatus !== 'all') {
switch (stockStatus) {
case 'out_of_stock':
conditions.push('stock_quantity = 0');
break;
case 'low_stock':
conditions.push('stock_quantity > 0 AND stock_quantity <= 5');
break;
case 'in_stock':
conditions.push('stock_quantity > 5');
break;
}
}
if (minPrice > 0) {
conditions.push('price >= ?');
params.push(minPrice);
}
if (maxPrice) {
conditions.push('price <= ?');
params.push(maxPrice);
}
// Get total count for pagination
const [countResult] = await pool.query(
`SELECT COUNT(*) as total FROM products WHERE ${conditions.join(' AND ')}`,
params
);
const total = countResult[0].total;
// Get paginated results
const query = `
SELECT
product_id,
title,
@@ -23,12 +86,37 @@ router.get('/', async (req, res) => {
brand,
categories,
visible,
managing_stock
managing_stock,
image
FROM products
WHERE visible = true
ORDER BY title ASC
`);
res.json(rows);
WHERE ${conditions.join(' AND ')}
ORDER BY ${sortColumn} ${sortDirection}
LIMIT ? OFFSET ?
`;
const [rows] = await pool.query(query, [...params, limit, offset]);
// Get unique categories and vendors for filters
const [categories] = await pool.query(
'SELECT DISTINCT categories FROM products WHERE visible = true AND categories IS NOT NULL AND categories != "" ORDER BY categories'
);
const [vendors] = await pool.query(
'SELECT DISTINCT vendor FROM products WHERE visible = true AND vendor IS NOT NULL AND vendor != "" ORDER BY vendor'
);
res.json({
products: rows,
pagination: {
total,
pages: Math.ceil(total / limit),
currentPage: page,
limit
},
filters: {
categories: categories.map(c => c.categories),
vendors: vendors.map(v => v.vendor)
}
});
} catch (error) {
console.error('Error fetching products:', error);
res.status(500).json({ error: 'Failed to fetch products' });
@@ -71,4 +159,63 @@ router.post('/import', upload.single('file'), async (req, res) => {
}
});
// Update a product
router.put('/:id', async (req, res) => {
const pool = req.app.locals.pool;
try {
const {
title,
sku,
stock_quantity,
price,
regular_price,
cost_price,
vendor,
brand,
categories,
visible,
managing_stock
} = req.body;
const [result] = await pool.query(
`UPDATE products
SET title = ?,
sku = ?,
stock_quantity = ?,
price = ?,
regular_price = ?,
cost_price = ?,
vendor = ?,
brand = ?,
categories = ?,
visible = ?,
managing_stock = ?
WHERE product_id = ?`,
[
title,
sku,
stock_quantity,
price,
regular_price,
cost_price,
vendor,
brand,
categories,
visible,
managing_stock,
req.params.id
]
);
if (result.affectedRows === 0) {
return res.status(404).json({ error: 'Product not found' });
}
res.json({ message: 'Product updated successfully' });
} catch (error) {
console.error('Error updating product:', error);
res.status(500).json({ error: 'Failed to update product' });
}
});
module.exports = router;