diff --git a/backend/routes/api/items.js b/backend/routes/api/items.js index 84a8af9..cea7c0d 100644 --- a/backend/routes/api/items.js +++ b/backend/routes/api/items.js @@ -40,6 +40,7 @@ router.get("/", auth.optional, function(req, res, next) { var query = {}; var limit = 100; var offset = 0; + var title = ''; if (typeof req.query.limit !== "undefined") { limit = req.query.limit; @@ -53,6 +54,11 @@ router.get("/", auth.optional, function(req, res, next) { query.tagList = { $in: [req.query.tag] }; } + + if (typeof req.query.title !== "undefined" && req.query.title !== '') { + query.title = { $regex: req.query.title, $options: 'i' }; // Case-insensitive search by title + } + Promise.all([ req.query.seller ? User.findOne({ username: req.query.seller }) : null, req.query.favorited ? User.findOne({ username: req.query.favorited }) : null diff --git a/frontend/src/agent.js b/frontend/src/agent.js index 972f3e3..e6d56d0 100644 --- a/frontend/src/agent.js +++ b/frontend/src/agent.js @@ -52,8 +52,13 @@ const Tags = { const limit = (count, p) => `limit=${count}&offset=${p ? p * count : 0}`; const omitSlug = (item) => Object.assign({}, item, { slug: undefined }); +// const filterByTitle = (title)=> title ? `&title=${title}` : ''; + const Items = { - all: (page) => requests.get(`/items?${limit(1000, page)}`), + all: (page, title = '') => { + const query = `${limit(1000, page)}${title ? `&title=${encode(title)}` : ''}`; + return requests.get(`/items?${query}`); + }, bySeller: (seller, page) => requests.get(`/items?seller=${encode(seller)}&${limit(500, page)}`), byTag: (tag, page) =>