|
|
|
@ -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' });
|
|
|
|
|