From 78279f9acb282947e3fb4e5e470995c95e154bdf Mon Sep 17 00:00:00 2001 From: lunnnnnn Date: Tue, 5 Dec 2023 13:24:53 -0500 Subject: [PATCH] simple search backend --- back-end/src/routes/search.mjs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 back-end/src/routes/search.mjs diff --git a/back-end/src/routes/search.mjs b/back-end/src/routes/search.mjs new file mode 100644 index 0000000..b7679f5 --- /dev/null +++ b/back-end/src/routes/search.mjs @@ -0,0 +1,21 @@ + +import Event from '../models/Event.mjs'; + +const handleSearch = async (query) => { + try { + // Use regex to perform case-insensitive search on 'name' and 'location' + const results = await Event.find({ + $or: [ + { name: { $regex: query, $options: 'i' } }, + { location: { $regex: query, $options: 'i' } }, + ], + }); + + return results; + } catch (error) { + console.error(error); + throw new Error('Error while searching for events'); + } +}; + +export default handleSearch;