From 39e80ff7edc3b87ee85c29b0d85de375586b0287 Mon Sep 17 00:00:00 2001 From: Ritesh Ghosh Date: Wed, 2 Aug 2023 21:19:17 +0530 Subject: feat: added `animeSearch` controller --- src/controllers/animeSearch.controller.ts | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/controllers/animeSearch.controller.ts (limited to 'src') diff --git a/src/controllers/animeSearch.controller.ts b/src/controllers/animeSearch.controller.ts new file mode 100644 index 0000000..b0990c3 --- /dev/null +++ b/src/controllers/animeSearch.controller.ts @@ -0,0 +1,33 @@ +import { scrapeAnimeSearch } from "../parsers"; +import createHttpError from "http-errors"; +import { AnimeCategories } from "../models"; +import { Request, Response, NextFunction, Handler } from "express"; + +// /anime/search?q=${query}&page=${page} +const getAnimeSearch: Handler = async ( + req: Request, + res: Response, + next: NextFunction +) => { + try { + const q: string | null = req.query.q + ? decodeURIComponent(req.query.q as string) + : null; + const page: number = req.query.page + ? Number(decodeURIComponent(req.query?.page as string)) + : 1; + + if (q === null) { + throw createHttpError.BadGateway("Search keyword required"); + } + + const data = await scrapeAnimeSearch(q, page); + + res.status(200).json(data); + } catch (err: any) { + // console.error(err); + next(err); + } +}; + +export default getAnimeSearch; -- cgit v1.2.3