Skip to content

Conversation

@JoyceKuode
Copy link

Comment on lines +75 to +97
const query = {};

// Add artist filter to the query if provided
if (artist) query.artistName = new RegExp(artist, "i"); // Case-insensitive regex search

// Add genre filter to the query if provided
if (genre) query.genre = new RegExp(genre, "i");

// Add BMP filter to query if provided
if (bpm) query.bpm = Number(bpm);

// Add popularity filter to query if provided
if (popularity) query.popularity = { $gte: Number(popularity) }; // Greater than or equal

// Initialize sorting object
let sortBy = {};
if (sort) {
const sortOrder = order === "desc" ? -1 : 1; // Set order (-1 for Descending, 1 for ascending)
sortBy[sort] = sortOrder; // Dynamically set sort field and order
}

// Query database using query object and sort options
const tracks = await Music.find(query).sort(sortBy);
Copy link
Contributor

Choose a reason for hiding this comment

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

Well done query-building ⭐

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