From 3bc8ec2f38a951cc8d5461b5990b34bc4a20bcf0 Mon Sep 17 00:00:00 2001 From: capitanwesler Date: Sun, 21 Mar 2021 10:30:00 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9F=20Adding=20the=20route=20to=20the?= =?UTF-8?q?=20waifu=20generator=20and=20implementing=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/UtilsController.ts | 38 ++++++++++++++++++++++++++++++ src/routes.ts | 1 + 2 files changed, 39 insertions(+) diff --git a/src/controllers/UtilsController.ts b/src/controllers/UtilsController.ts index 34d58ae..69dd6dd 100644 --- a/src/controllers/UtilsController.ts +++ b/src/controllers/UtilsController.ts @@ -10,6 +10,7 @@ import ThemeModel, { Theme } from '../database/models/theme.model'; import ThemeParser from '../utils/animeTheme'; import { structureThemes } from '../utils/util'; import { getThemes } from '../utils/util'; +import WaifuModel, { Waifu } from '../database/models/waifu.model'; /* UtilsController - controller to parse the @@ -520,4 +521,41 @@ export default class UtilsController { res.status(500).json({ message: 'Aruppi lost in the shell' }); } } + + async getWaifuRandom(req: Request, res: Response, next: NextFunction) { + let waifuQuery: Waifu[] | null; + let waifuResult: any; + + try { + waifuQuery = await WaifuModel.aggregate([{ $sample: { size: 1 } }]); + } catch (err) { + return next(err); + } + + if (waifuQuery.length > 0) { + waifuResult = { + id: waifuQuery[0].id, + name: waifuQuery[0].name, + weight: waifuQuery[0].weight, + series: waifuQuery[0].series, + height: waifuQuery[0].height, + birthday: waifuQuery[0].birthday, + likes: waifuQuery[0].likes, + trash: waifuQuery[0].trash, + blood_type: waifuQuery[0].blood_type, + hip: waifuQuery[0].hip, + bust: waifuQuery[0].bust, + description: waifuQuery[0].description, + display_picture: waifuQuery[0].display_picture, + waist: waifuQuery[0].waist, + }; + } + + if (waifuResult) { + res.set('Cache-Control', 'no-store'); + res.status(200).json(waifuResult); + } else { + res.status(500).json({ message: 'Aruppi lost in the shell' }); + } + } } diff --git a/src/routes.ts b/src/routes.ts index 6f9bfd4..606a8e0 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -110,6 +110,7 @@ routes.get('/api/v4/randomTheme', utilsController.randomTheme); routes.get('/api/v4/artists/:id?', utilsController.getArtist); routes.get('/api/v4/destAnimePlatforms', utilsController.getDestAnimePlatforms); routes.get('/api/v4/platforms/:id?', utilsController.getPlatforms); +routes.get('/api/v4/generateWaifu/', utilsController.getWaifuRandom); /* Routes to handling the v3 deprecated */ // routes.get('/api/v3/*', (req: Request, res: Response, next: NextFunction) => {