From 121bc98a8935bbc5a63a0a7fd9cb850d0bee46b1 Mon Sep 17 00:00:00 2001 From: capitanwesler Date: Sun, 14 Mar 2021 12:43:20 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B3=20Implementing=20the=20search=20fu?= =?UTF-8?q?nction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/DirectoryController.ts | 28 ++++++++++++++++++++++++++ src/routes.ts | 1 + 2 files changed, 29 insertions(+) diff --git a/src/controllers/DirectoryController.ts b/src/controllers/DirectoryController.ts index df6cbde..499d699 100644 --- a/src/controllers/DirectoryController.ts +++ b/src/controllers/DirectoryController.ts @@ -181,4 +181,32 @@ export default class DirectoryController { res.status(500).json({ message: 'Aruppi lost in the shell' }); } } + + async search(req: Request, res: Response, next: NextFunction) { + const { title } = req.params; + let results: Anime[] | null; + + try { + results = await AnimeModel.find({ + title: { $regex: new RegExp(title, 'i') }, + }); + } catch (err) { + return next(err); + } + + const resultAnimes: any[] = results.map((item: any) => { + return { + id: item.id, + title: item.title, + type: item.type, + image: item.poster, + }; + }); + + if (resultAnimes.length > 0) { + res.status(200).json({ search: resultAnimes }); + } else { + res.status(500).json({ message: 'Aruppi lost in the shell' }); + } + } } diff --git a/src/routes.ts b/src/routes.ts index 48be5ea..f2583d2 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -89,6 +89,7 @@ routes.get('/api/v4/season/:year/:type', directoryController.getSeason); routes.get('/api/v4/allSeasons', directoryController.allSeasons); routes.get('/api/v4/laterSeasons', directoryController.laterSeasons); routes.get('/api/v4/moreInfo/:title', directoryController.getMoreInfo); +routes.get('/api/v4/search/:title', directoryController.search); /* Utils Controller */ routes.get('/api/v4/anitakume', utilsController.getAnitakume);