From 7c47c92aed9158ee077d17c4739d6d86a30a599a Mon Sep 17 00:00:00 2001 From: lunnnnnn Date: Tue, 5 Dec 2023 23:21:52 -0500 Subject: [PATCH] Simple searchRouter --- back-end/src/routes/search.mjs | 21 ------------------ back-end/src/routes/searchRouter.mjs | 32 ++++++++++++++++------------ front-end/src/index.css | 2 +- 3 files changed, 19 insertions(+), 36 deletions(-) delete mode 100644 back-end/src/routes/search.mjs diff --git a/back-end/src/routes/search.mjs b/back-end/src/routes/search.mjs deleted file mode 100644 index b7679f5..0000000 --- a/back-end/src/routes/search.mjs +++ /dev/null @@ -1,21 +0,0 @@ - -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; diff --git a/back-end/src/routes/searchRouter.mjs b/back-end/src/routes/searchRouter.mjs index 57625b9..61f061b 100644 --- a/back-end/src/routes/searchRouter.mjs +++ b/back-end/src/routes/searchRouter.mjs @@ -1,24 +1,28 @@ import Artwork from '../models/Artwork.mjs'; +import { getArts } from './modifyFavListRouter.mjs'; export const searchRouter = async (req, res) => { - const { query } = req.query; - + const { city, artinfo, timeline } = req.query; try { let searchQuery = {}; - - if (query) { - searchQuery = { - $or: [ - { Year: query }, - { artist: { $regex: query, $options: 'i' } }, - { title: { $regex: query, $options: 'i' } }, - { location: { $regex: query, $options: 'i' } } - ] - }; + if (city || artinfo || timeline) { + searchQuery = {}; + if (city) { + searchQuery.city = { $regex: city, $options: 'i' }; + } + searchQuery.$or = [ + { artinfo: { $regex: artinfo, $options: 'i' } }, + { title: { $regex: artinfo, $options: 'i' } }, + ]; + if (!artinfo) { + searchQuery.$or.push({ artinfo: { $exists: false } }); + } + if (timeline) { + searchQuery.year = { $gte: timeline.startYear, $lte: timeline.endYear }; + } } - - const artworks = await Artwork.find(searchQuery).limit(10); + const artworks = getArts(searchQuery); res.json(artworks); } catch (error) { res.status(500).json({ message: "Internal server error." }); diff --git a/front-end/src/index.css b/front-end/src/index.css index ded6e1b..e15fe3f 100644 --- a/front-end/src/index.css +++ b/front-end/src/index.css @@ -44,4 +44,4 @@ } @layer utilities { -} +} \ No newline at end of file