Updated UI and added search function

master
parent 4ccedb1d22
commit 0a6f49c301

@ -104,19 +104,11 @@ exports.getAllActiveProducts = async (req, res) => {
// search products controller // search products controller
exports.searchProducts = async (req, res) => { exports.searchProducts = async (req, res) => {
try { try {
const { query } = req.body; const { productName } = req.body;
// Build the search query using case-insensitive regex
const regex = new RegExp(escapeRegex(query), 'i');
const products = await Product.find({ const products = await Product.find({ name: { $regex: new RegExp(productName, 'i') } });
$or: [
{ name: regex },
// Add more fields to search if needed
],
});
res.json(products); res.json({products});
} catch (error) { } catch (error) {
console.error(error); console.error(error);
res.status(500).json({ message: 'Error searching products' }); res.status(500).json({ message: 'Error searching products' });

@ -21,7 +21,7 @@ router.get(`/all`, verify, verifyAdmin, productControllers.getAllProducts);
router.get(`/`, productControllers.getAllActiveProducts); router.get(`/`, productControllers.getAllActiveProducts);
// Route for searching products // Route for searching products
router.get(`/search`, productControllers.searchProducts); router.post(`/search`, productControllers.searchProducts);
// Route for updating products // Route for updating products
router.put(`/:productId`, verify, verifyAdmin, productControllers.updateProduct); router.put(`/:productId`, verify, verifyAdmin, productControllers.updateProduct);

Loading…
Cancel
Save