Skip to content

Instantly share code, notes, and snippets.

@WoolDoughnut310
Created October 28, 2022 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WoolDoughnut310/31590a78961c72ed8724d99d71450de3 to your computer and use it in GitHub Desktop.
Save WoolDoughnut310/31590a78961c72ed8724d99d71450de3 to your computer and use it in GitHub Desktop.
router.get(
"/recommend",
asyncHandler(async (req: Request, res: Response) => {
const db = getDb();
const collection = db.collection<Song>("song");
// /songs/recommend?limit=N
const limit = parseInt(req.params.limit) || 20;
// Array of song IDs
const favourites = req.user.favourites;
let suggestions: ObjectId[];
if (!favourites) {
// Getting any songs
const songs = await collection
.find({}, { limit, projection: { _id: 1 } })
.toArray();
suggestions = songs.map((song) => song._id);
} else {
// Suggesting
const favouriteCluster = await getFavouriteCluster(favourites);
suggestions = await getSuggestions(favouriteCluster, favourites);
}
res.status(200).json(suggestions);
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment