From caec8b684d0afb5fb97ea6fd5a03e8001e2db648 Mon Sep 17 00:00:00 2001 From: Ritesh Ghosh Date: Sun, 17 Dec 2023 19:37:14 +0530 Subject: feat(estimatedSchedule): add `/schedule` endpoint controller --- src/controllers/estimatedSchedule.controller.ts | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/controllers/estimatedSchedule.controller.ts (limited to 'src/controllers') 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>, + 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; -- cgit v1.2.3 From 9b88ae9cc9f63f98bb1aae3de1f78d70132c72c3 Mon Sep 17 00:00:00 2001 From: Ritesh Ghosh Date: Sun, 17 Dec 2023 19:38:56 +0530 Subject: refactor: controllers import export --- src/controllers/index.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/controllers') diff --git a/src/controllers/index.ts b/src/controllers/index.ts index 21f9d10..6e90440 100644 --- a/src/controllers/index.ts +++ b/src/controllers/index.ts @@ -6,6 +6,7 @@ import getAnimeCategory from "./animeCategory.controller.js"; import getProducerAnimes from "./animeProducer.controller.js"; import getEpisodeServers from "./episodeServers.controller.js"; import getAnimeAboutInfo from "./animeAboutInfo.controller.js"; +import getEstimatedSchedule from "./estimatedSchedule.controller.js"; import getAnimeEpisodeSources from "./animeEpisodeSrcs.controller.js"; import getAnimeSearchSuggestion from "./animeSearchSuggestion.controller.js"; @@ -18,6 +19,7 @@ export { getEpisodeServers, getProducerAnimes, getAnimeAboutInfo, + getEstimatedSchedule, getAnimeEpisodeSources, getAnimeSearchSuggestion, }; -- cgit v1.2.3