Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add filter by title to get all items by title #2

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/routes/api/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down