aboutsummaryrefslogtreecommitdiff
path: root/src/controllers/animeSearch.controller.ts
blob: b0990c32d74b7fb3f1a3ed9214df4a8e3c61264d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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;