diff options
| author | Ritesh Ghosh <[email protected]> | 2023-08-02 21:19:17 +0530 |
|---|---|---|
| committer | Ritesh Ghosh <[email protected]> | 2023-08-02 21:19:17 +0530 |
| commit | 39e80ff7edc3b87ee85c29b0d85de375586b0287 (patch) | |
| tree | c27e90620a197bfd2662909a09199261e8ea12b8 /src/controllers | |
| parent | 22ef7a54f6b3b01366cbad15ab2ca62574d30ecb (diff) | |
| download | aniwatch-api-39e80ff7edc3b87ee85c29b0d85de375586b0287.tar.xz aniwatch-api-39e80ff7edc3b87ee85c29b0d85de375586b0287.zip | |
feat: added `animeSearch` controller
Diffstat (limited to 'src/controllers')
| -rw-r--r-- | src/controllers/animeSearch.controller.ts | 33 |
1 files changed, 33 insertions, 0 deletions
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; |
