Skip to content

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