diff --git a/src/app/controllers/lib/populators.ts b/src/app/controllers/lib/populators.ts index 7ca9012..365391b 100644 --- a/src/app/controllers/lib/populators.ts +++ b/src/app/controllers/lib/populators.ts @@ -12,6 +12,7 @@ import { WebController, WebError } from "../../../lib/dtplib.js"; import { ImageService } from "../../services/image.js"; import { UserService } from "../../services/user.js"; import { BroadcastShowService } from "app/services/broadcast-show.js"; +import { EpisodeService } from "app/services/episode.js"; export function populateAccountByEmail( controller: WebController @@ -94,6 +95,33 @@ export function populateBroadcastShow( }; } +export function populateEpisode(controller: WebController): RequestHandler { + const episodeService = controller.getService("episode"); + return async function ( + _req: Request, + res: Response, + next: NextFunction, + episodeId?: string + ): Promise { + assert(episodeId, "Episode ID is required"); + try { + res.locals.show = await episodeService.getById( + Types.ObjectId.createFromHexString(episodeId) + ); + if (!res.locals.show) { + throw new WebError(404, "Broadcast show not found"); + } + return next(); + } catch (error) { + controller.log.error("failed to populate episode ID", { + episodeId, + error, + }); + return next(error); + } + }; +} + export function populateImageId(controller: WebController): RequestHandler { const imageService = controller.platform.components.getService("image");