diff options
| author | Ritesh Ghosh <[email protected]> | 2024-03-25 19:02:36 +0530 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-25 19:02:36 +0530 |
| commit | 176a7a4bd722ff421a0e3836fa444bd912bcbe4e (patch) | |
| tree | b06e873dc3eca4752a29d6356c2a5b936f5cd40f /src/controllers/animeSearch.controller.ts | |
| parent | e136af03bf174c0ea46a5c3eec9f3599ad8cd2be (diff) | |
| parent | 79d0bdf05f86c5d5411f9473889442000786322f (diff) | |
| download | aniwatch-api-176a7a4bd722ff421a0e3836fa444bd912bcbe4e.tar.xz aniwatch-api-176a7a4bd722ff421a0e3836fa444bd912bcbe4e.zip | |
Merge pull request #34 from ghoshRitesh12/advanced-search
Add advanced search feature
Diffstat (limited to 'src/controllers/animeSearch.controller.ts')
| -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) { |
