diff options
| author | Ritesh Ghosh <[email protected]> | 2024-03-25 18:58:09 +0530 |
|---|---|---|
| committer | Ritesh Ghosh <[email protected]> | 2024-03-25 18:58:09 +0530 |
| commit | fef106da27270dcb86031e511a3cc428e40f41ff (patch) | |
| tree | b9ad8f1cc21835b15c746353a0fa2420bdc057ef | |
| parent | 488a3593d20ce4591f120c0f17663b86242a2ecd (diff) | |
| download | aniwatch-api-fef106da27270dcb86031e511a3cc428e40f41ff.tar.xz aniwatch-api-fef106da27270dcb86031e511a3cc428e40f41ff.zip | |
feat(advancedSearch): feat: add search filter parsing
| -rw-r--r-- | src/controllers/animeSearch.controller.ts | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/src/controllers/animeSearch.controller.ts b/src/controllers/animeSearch.controller.ts index c7ec57a..8937b0a 100644 --- a/src/controllers/animeSearch.controller.ts +++ b/src/controllers/animeSearch.controller.ts @@ -1,7 +1,24 @@ import createHttpError from "http-errors"; import { type RequestHandler } from "express"; import { scrapeAnimeSearch } from "../parsers/index.js"; -import { type AnimeSearchQueryParams } from "../types/controllers/index.js"; +import type { + SearchFilters, + AnimeSearchQueryParams, +} from "../types/controllers/index.js"; + +const searchFilters: Record<string, boolean> = { + filter: true, + type: true, + status: true, + rated: true, + score: true, + season: true, + language: true, + start_date: true, + end_date: true, + sort: true, + genres: true, +} as const; // /anime/search?q=${query}&page=${page} const getAnimeSearch: RequestHandler< @@ -11,18 +28,24 @@ const getAnimeSearch: RequestHandler< AnimeSearchQueryParams > = async (req, res, next) => { try { - const query: 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 (query === null) { + let { q: query, page, ...filters } = req.query; + + query = query ? decodeURIComponent(query) : undefined; + const pageNo = page ? Number(decodeURIComponent(page as string)) : 1; + + if (query === undefined) { throw createHttpError.BadRequest("Search keyword required"); } - const data = await scrapeAnimeSearch(query, page); + const parsedFilters: SearchFilters = {}; + for (const key in filters) { + if (searchFilters[key]) { + parsedFilters[key as keyof SearchFilters] = + filters[key as keyof SearchFilters]; + } + } + + const data = await scrapeAnimeSearch(query, pageNo, parsedFilters); res.status(200).json(data); } catch (err: any) { |
