Product editor search enhancements
This commit is contained in:
@@ -1058,7 +1058,16 @@ router.get('/search-products', async (req, res) => {
|
||||
// Build WHERE clause with additional filters
|
||||
let whereClause;
|
||||
if (pid) {
|
||||
whereClause = `\n WHERE p.pid = ${connection.escape(Number(pid))}`;
|
||||
const pids = String(pid).split(',').map(Number).filter(n => !isNaN(n) && n > 0);
|
||||
if (pids.length === 0) {
|
||||
connection.release();
|
||||
return res.status(400).json({ error: 'Invalid pid parameter' });
|
||||
}
|
||||
if (pids.length === 1) {
|
||||
whereClause = `\n WHERE p.pid = ${connection.escape(pids[0])}`;
|
||||
} else {
|
||||
whereClause = `\n WHERE p.pid IN (${pids.map(p => connection.escape(p)).join(',')})`;
|
||||
}
|
||||
} else {
|
||||
whereClause = `
|
||||
WHERE (
|
||||
|
||||
@@ -473,21 +473,31 @@ router.get('/search', async (req, res) => {
|
||||
return [like, like, like, like, like];
|
||||
});
|
||||
|
||||
const { rows } = await pool.query(`
|
||||
SELECT pid, title, sku, barcode, brand, line, regular_price, image_175
|
||||
FROM products p
|
||||
WHERE ${conditions.join(' AND ')}
|
||||
ORDER BY
|
||||
CASE WHEN p.sku ILIKE $${params.length + 1} THEN 0
|
||||
WHEN p.barcode ILIKE $${params.length + 1} THEN 1
|
||||
WHEN p.title ILIKE $${params.length + 1} THEN 2
|
||||
ELSE 3
|
||||
END,
|
||||
p.total_sold DESC NULLS LAST
|
||||
LIMIT 50
|
||||
`, [...params, `%${q.trim()}%`]);
|
||||
const whereClause = conditions.join(' AND ');
|
||||
const searchParams = [...params, `%${q.trim()}%`];
|
||||
|
||||
res.json(rows);
|
||||
const [{ rows }, { rows: countRows }] = await Promise.all([
|
||||
pool.query(`
|
||||
SELECT pid, title, sku, barcode, brand, line, regular_price, image_175
|
||||
FROM products p
|
||||
WHERE ${whereClause}
|
||||
ORDER BY
|
||||
CASE WHEN p.sku ILIKE $${params.length + 1} THEN 0
|
||||
WHEN p.barcode ILIKE $${params.length + 1} THEN 1
|
||||
WHEN p.title ILIKE $${params.length + 1} THEN 2
|
||||
ELSE 3
|
||||
END,
|
||||
p.total_sold DESC NULLS LAST
|
||||
LIMIT 50
|
||||
`, searchParams),
|
||||
pool.query(`
|
||||
SELECT COUNT(*)::int AS total
|
||||
FROM products p
|
||||
WHERE ${whereClause}
|
||||
`, params),
|
||||
]);
|
||||
|
||||
res.json({ results: rows, total: countRows[0].total });
|
||||
} catch (error) {
|
||||
console.error('Error searching products:', error);
|
||||
res.status(500).json({ error: 'Search failed' });
|
||||
|
||||
Reference in New Issue
Block a user