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

MongoDB API Emelie Nyberg #506

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

EmelieNyberg
Copy link

Copy link
Contributor

@HIPPIEKICK HIPPIEKICK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid work! Great job combining the queries 💃

Comment on lines +72 to +95
const { sorted, category, release_year } = req.query;

// Sorted rating from highest to lowest http://localhost:3000/videogames?sorted=true
if (sorted) {
const sortedGames = await Game.find().sort({ rating: -1 }); // Sort after rating, highest first
return res.json(sortedGames);
}

// Dynamic query-object so that you can filter on both category and release_year
// For example http://localhost:3000/videogames?category=racing&release_year=2017
const query = {};

// For example http://localhost:3000/videogames?category=racing
if (category) {
query.category = { $regex: category, $options: "i" }; // Case-insensitive search on category. for example its ok if database says "Racing" and url says "racing"
}

// For example http://localhost:3000/videogames?release_year=2017
if (release_year) {
query.release_year = +release_year; // Filtrera på release_year
}

// Get games depending on the dynamic query-object
const videogames = await Game.find(query);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants