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);