diff options
| author | Ritesh Ghosh <[email protected]> | 2023-12-17 19:37:14 +0530 |
|---|---|---|
| committer | Ritesh Ghosh <[email protected]> | 2023-12-17 19:37:14 +0530 |
| commit | caec8b684d0afb5fb97ea6fd5a03e8001e2db648 (patch) | |
| tree | 704d956b5837b7b024ef9abeefb35106703c9fcb | |
| parent | dfed9d90e39ab60632103920453abc928f6a181f (diff) | |
| download | aniwatch-api-caec8b684d0afb5fb97ea6fd5a03e8001e2db648.tar.xz aniwatch-api-caec8b684d0afb5fb97ea6fd5a03e8001e2db648.zip | |
feat(estimatedSchedule): add `/schedule` endpoint controller
| -rw-r--r-- | src/controllers/estimatedSchedule.controller.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/controllers/estimatedSchedule.controller.ts b/src/controllers/estimatedSchedule.controller.ts new file mode 100644 index 0000000..bead83e --- /dev/null +++ b/src/controllers/estimatedSchedule.controller.ts @@ -0,0 +1,36 @@ +import createHttpError from "http-errors"; +import { type RequestHandler } from "express"; +import { scrapeEstimatedSchedule } from "../parsers/index.js"; +import { type EstimatedScheduleQueryParams } from "../models/controllers/index.js"; + +// /anime/schedule?date=${date} +const getEstimatedSchedule: RequestHandler< + unknown, + Awaited<ReturnType<typeof scrapeEstimatedSchedule>>, + unknown, + EstimatedScheduleQueryParams +> = async (req, res, next) => { + try { + const dateQuery = req.query.date + ? decodeURIComponent(req.query.date as string) + : null; + + if (dateQuery === null) { + throw createHttpError.BadRequest("Date payload required"); + } + if (!/^\d{4}-\d{2}-\d{2}$/.test(dateQuery)) { + throw createHttpError.BadRequest( + "Invalid date payload format. Months and days must have 2 digits" + ); + } + + const data = await scrapeEstimatedSchedule(dateQuery); + + res.status(200).json(data); + } catch (err: any) { + console.error(err); + next(err); + } +}; + +export default getEstimatedSchedule; |
