INNER CODE UNIT · TypeScript
getEpisodes
freeCodeCamp/mobile · mobile-api/src/routes.ts:54
async function getEpisodes(req: Request, res: Response) {
const podcast = await Podcast.findById(req.params.podcastId);
if (!podcast) throw Error('Podcast not found');
const episodes = await Episode.find({ podcastId: podcast._id })
.sort({ publicationDate: -1 })
.skip(parseInt((req.query?.page as string) || '0') * 20)
.limit(20);
res.json({ podcast, episodes });
}
router.get('/:podcastId/episodes/:episodeId', (req, res, next) => {
getEpisode(req, res).catch(next);
});
async function getEpisode(req: Request, res: Response) {
const episode = await Episode.findOne({
podcastId: req.params.podcastId,
_id: req.params.episodeId,