From 0a6f49c3016f6b7ff5ffd358099057983a04025d Mon Sep 17 00:00:00 2001 From: patrickjieraldjuan Date: Fri, 2 Feb 2024 21:13:55 +0800 Subject: [PATCH] Updated UI and added search function --- controllers/productControllers.js | 14 +++----------- routes/productRoutes.js | 2 +- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/controllers/productControllers.js b/controllers/productControllers.js index d2582e7..041c4ee 100644 --- a/controllers/productControllers.js +++ b/controllers/productControllers.js @@ -104,19 +104,11 @@ exports.getAllActiveProducts = async (req, res) => { // search products controller exports.searchProducts = async (req, res) => { 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({ name: { $regex: new RegExp(productName, 'i') } }); - const products = await Product.find({ - $or: [ - { name: regex }, - // Add more fields to search if needed - ], - }); - - res.json(products); + res.json({products}); } catch (error) { console.error(error); res.status(500).json({ message: 'Error searching products' }); diff --git a/routes/productRoutes.js b/routes/productRoutes.js index 52f7a9e..5935aae 100644 --- a/routes/productRoutes.js +++ b/routes/productRoutes.js @@ -21,7 +21,7 @@ router.get(`/all`, verify, verifyAdmin, productControllers.getAllProducts); router.get(`/`, productControllers.getAllActiveProducts); // Route for searching products -router.get(`/search`, productControllers.searchProducts); +router.post(`/search`, productControllers.searchProducts); // Route for updating products router.put(`/:productId`, verify, verifyAdmin, productControllers.updateProduct);