From d2e1022e75ec41577cb8a8f4a3e249e2b1760fb0 Mon Sep 17 00:00:00 2001 From: aetrnyx Date: Mon, 6 Dec 2021 18:30:11 +0530 Subject: [PATCH] Added new baka endpoint and bumped the version --- package-lock.json | 2 +- package.json | 2 +- src/controllers/gifs/randomBaka.js | 28 ++++++++++++++++++++++++++++ src/models/index.js | 2 ++ src/models/schemas/Baka.js | 8 ++++++++ src/routes.js | 2 ++ 6 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/controllers/gifs/randomBaka.js create mode 100644 src/models/schemas/Baka.js diff --git a/package-lock.json b/package-lock.json index c7c5d76..590d969 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "airi", - "version": "1.2.8", + "version": "1.3.8", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 2d1c255..5f5c1ee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "airi", - "version": "1.2.8", + "version": "1.3.8", "description": "Random API Serving Anime stuff", "author": "Aeryk", "private": true, diff --git a/src/controllers/gifs/randomBaka.js b/src/controllers/gifs/randomBaka.js new file mode 100644 index 0000000..56d1559 --- /dev/null +++ b/src/controllers/gifs/randomBaka.js @@ -0,0 +1,28 @@ +const createError = require('http-errors') +const requestIp = require('request-ip') +const moment = require('moment') +const Baka = require('../../models/schemas/Baka') + +// Get random Anime Baka +module.exports = async function getRandomBaka(req, res, next) { + try { + const [result] = await Baka.aggregate([ + // Select a random document from the results + { $sample: { size: 1 } }, + { $project: { __v: 0, _id: 0 } }, + ]) + + if (!result) { + return next(createError(404, 'Could not find any Baka Gif')) + } + + res.status(200).json(result) + console.log( + `${req.method} | ${moment(Date.now()).format()} ${requestIp.getClientIp( + req + )} to ${req.path} - ${JSON.stringify(req.query)}` + ) + } catch (error) { + return next(error) + } +} diff --git a/src/models/index.js b/src/models/index.js index 48db8d0..6ba9975 100644 --- a/src/models/index.js +++ b/src/models/index.js @@ -66,6 +66,7 @@ const Smug = require('./schemas/Smug') const Think = require('./schemas/Think') const Thumbsup = require('./schemas/Thumbsup') const Tickle = require('./schemas/Tickle') +const Baka = require('./schemas/Baka') module.exports = { Tags, @@ -136,4 +137,5 @@ module.exports = { Think, Thumbsup, Tickle, + Baka, } diff --git a/src/models/schemas/Baka.js b/src/models/schemas/Baka.js new file mode 100644 index 0000000..ccd7903 --- /dev/null +++ b/src/models/schemas/Baka.js @@ -0,0 +1,8 @@ +const { Schema, model } = require('mongoose') + +const BakaSchema = new Schema({ + _id: { type: Number }, + url: { type: String, required: true }, +}) + +module.exports = model('Baka', BakaSchema) diff --git a/src/routes.js b/src/routes.js index 9ddd3df..f60a7bb 100644 --- a/src/routes.js +++ b/src/routes.js @@ -69,6 +69,7 @@ const randomLove = require('./controllers/gifs/randomLove') const randomLurk = require('./controllers/gifs/randomLurk') const randomNervous = require('./controllers/gifs/randomNervous') const randomNom = require('./controllers/gifs/randomNom') +const randomBaka = require('./controllers/gifs/randomBaka') const router = Router() @@ -163,5 +164,6 @@ router.get('/api/love', Limiter, authHandler, randomLove) router.get('/api/lurk', Limiter, authHandler, randomLurk) router.get('/api/nervous', Limiter, authHandler, randomNervous) router.get('/api/nom', Limiter, authHandler, randomNom) +router.get('/api/baka', Limiter, authHandler, randomBaka) module.exports = router