Skip to content

Commit

Permalink
feat(advancedSearch): feat: add search filter parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ghoshRitesh12 committed Mar 25, 2024
1 parent 488a359 commit fef106d
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions src/controllers/animeSearch.controller.ts
Original file line number Diff line number Diff line change
@@ -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<
Expand All @@ -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) {
Expand Down

0 comments on commit fef106d

Please sign in to comment.