Skip to content

Commit

Permalink
Merge pull request #174 from agiledev-students-fall2023/Search
Browse files Browse the repository at this point in the history
Simple searchRouter
  • Loading branch information
lesleyzhao authored Dec 6, 2023
2 parents d3ffef0 + 7c47c92 commit ef1d8f6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 36 deletions.
21 changes: 0 additions & 21 deletions back-end/src/routes/search.mjs

This file was deleted.

32 changes: 18 additions & 14 deletions back-end/src/routes/searchRouter.mjs
Original file line number Diff line number Diff line change
@@ -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." });
Expand Down
2 changes: 1 addition & 1 deletion front-end/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
}

@layer utilities {
}
}

0 comments on commit ef1d8f6

Please sign in to comment.