Created
October 28, 2022 15:16
-
-
Save WoolDoughnut310/49d4cc3df65886437b79c0d714db9cf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Response } from "express"; | |
import { Filter, ObjectId } from "mongodb"; | |
import getDb from "../db"; | |
import { Song } from "../db/types"; | |
import { Request } from "../types"; | |
import asyncHandler from "express-async-handler"; | |
... | |
router.get( | |
"/", | |
asyncHandler(async (req: Request, res: Response) => { | |
const db = getDb(); | |
const collection = db.collection<Song>("song"); | |
let filter: Filter<Song> = {}; | |
if (typeof req.query.user === "string") { | |
filter = { userId: new ObjectId(req.query.user) }; | |
} | |
const songs = await collection | |
.find(filter) | |
.sort({ date: -1 }) | |
.toArray(); | |
res.status(200).json(songs); | |
}) | |
); | |
export default router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment