Skip to content

Commit

Permalink
Moved the routes and controllers separately to different version folders
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrea committed Dec 19, 2023
1 parent 0cd0660 commit 56f75c9
Show file tree
Hide file tree
Showing 165 changed files with 3,008 additions and 382 deletions.
58 changes: 58 additions & 0 deletions src/controllers/v3/facts/randomFacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import createError from 'http-errors';
import Facts from '../../../models/schemas/Fact.js';
import tagsFilter from '../../../modules/tagsFilter.js';
import lengthFilter from '../../../modules/lengthFilter.js';
import Stats from '../../../models/schemas/Stat.js';

/**
* Gets a random anime fact with optional length and tags filters and updates system statistics.
*
* @param {Object} req - Express request object.
* @param {Object} res - Express response object.
* @param {Function} next - Express next middleware function.
*/
const getRandomFact = async (req, res, next) => {
try {
const { minLength, maxLength, tags } = req.query;

// Create a filter object based on the optional length and tags parameters
const filter = {};

// Apply length filter (if minLength or maxLength is provided)
if (minLength || maxLength) {
filter.length = lengthFilter(minLength, maxLength);
}

// Apply tags filter (if tags are provided)
if (tags) {
filter.tags = tagsFilter(tags);
}

// Aggregate to match the filter, select a random fact, and project excluding version field
const [result] = await Facts.aggregate([
{ $match: filter }, // Apply filters (if any)
{ $sample: { size: 1 } }, // Select a random document from the results
{ $project: { __v: 0 } },
]);

// If no fact is found, return a 404 error
if (!result) {
return next(createError(404, 'Could not find any matching fact'));
}

// Respond with the random fact
res.status(200).json(result);

// Update system statistics for facts
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { facts: 1 } });
} catch (error) {
// Update system statistics for failed requests
await Stats.findOneAndUpdate(
{ _id: 'systemstats' },
{ $inc: { failed_requests: 1 } }
);
return next(error);
}
};

export default getRandomFact;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Angry from '../../models/schemas/Angry.js';
import Stats from '../../models/schemas/Stat.js';
import Angry from '../../../models/schemas/Angry.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Angry
const getRandomAngry = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Baka from '../../models/schemas/Baka.js';
import Stats from '../../models/schemas/Stat.js';
import Baka from '../../../models/schemas/Baka.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Baka
const getRandomBaka = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Bite from '../../models/schemas/Bite.js';
import Stats from '../../models/schemas/Stat.js';
import Bite from '../../../models/schemas/Bite.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Bite
const getRandomBite = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Blush from '../../models/schemas/Blush.js';
import Stats from '../../models/schemas/Stat.js';
import Blush from '../../../models/schemas/Blush.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Blush
const getRandomBlush = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Bonk from '../../models/schemas/Bonk.js';
import Stats from '../../models/schemas/Stat.js';
import Bonk from '../../../models/schemas/Bonk.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Bonk
const getRandomBonk = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Bored from '../../models/schemas/Bored.js';
import Stats from '../../models/schemas/Stat.js';
import Bored from '../../../models/schemas/Bored.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Bored
const getRandomBored = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Bully from '../../models/schemas/Bully.js';
import Stats from '../../models/schemas/Stat.js';
import Bully from '../../../models/schemas/Bully.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Bully
const getRandomBully = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Bye from '../../models/schemas/Bye.js';
import Stats from '../../models/schemas/Stat.js';
import Bye from '../../../models/schemas/Bye.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Bye
const getRandomBye = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Chase from '../../models/schemas/Chase.js';
import Stats from '../../models/schemas/Stat.js';
import Chase from '../../../models/schemas/Chase.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Chase
const getRandomChase = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Cheer from '../../models/schemas/Cheer.js';
import Stats from '../../models/schemas/Stat.js';
import Cheer from '../../../models/schemas/Cheer.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Cheer
const getRandomCheer = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Cringe from '../../models/schemas/Cringe.js';
import Stats from '../../models/schemas/Stat.js';
import Cringe from '../../../models/schemas/Cringe.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Cringe
const getRandomCringe = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Cry from '../../models/schemas/Cry.js';
import Stats from '../../models/schemas/Stat.js';
import Cry from '../../../models/schemas/Cry.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Cry
const getRandomCry = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Cuddle from '../../models/schemas/Cuddle.js';
import Stats from '../../models/schemas/Stat.js';
import Cuddle from '../../../models/schemas/Cuddle.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Cuddle
const getRandomCuddle = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Dab from '../../models/schemas/Dab.js';
import Stats from '../../models/schemas/Stat.js';
import Dab from '../../../models/schemas/Dab.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Dab
const getRandomDab = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Dance from '../../models/schemas/Dance.js';
import Stats from '../../models/schemas/Stat.js';
import Dance from '../../../models/schemas/Dance.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Dance
const getRandomDance = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Die from '../../models/schemas/Die.js';
import Stats from '../../models/schemas/Stat.js';
import Die from '../../../models/schemas/Die.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Die
const getRandomDie = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Disgust from '../../models/schemas/Disgust.js';
import Stats from '../../models/schemas/Stat.js';
import Disgust from '../../../models/schemas/Disgust.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Disgust
const getRandomDisgust = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Facepalm from '../../models/schemas/Facepalm.js';
import Stats from '../../models/schemas/Stat.js';
import Facepalm from '../../../models/schemas/Facepalm.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Facepalm
const getRandomFacepalm = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Feed from '../../models/schemas/Feed.js';
import Stats from '../../models/schemas/Stat.js';
import Feed from '../../../models/schemas/Feed.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Feed
const getRandomFeed = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Glomp from '../../models/schemas/Glomp.js';
import Stats from '../../models/schemas/Stat.js';
import Glomp from '../../../models/schemas/Glomp.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Glomp
const getRandomGlomp = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Happy from '../../models/schemas/Happy.js';
import Stats from '../../models/schemas/Stat.js';
import Happy from '../../../models/schemas/Happy.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Happy
const getRandomHappy = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Hi from '../../models/schemas/Hi.js';
import Stats from '../../models/schemas/Stat.js';
import Hi from '../../../models/schemas/Hi.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Hi
const getRandomHi = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Highfive from '../../models/schemas/Highfive.js';
import Stats from '../../models/schemas/Stat.js';
import Highfive from '../../../models/schemas/Highfive.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Highfive
const getRandomHighfive = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Hold from '../../models/schemas/Hold.js';
import Stats from '../../models/schemas/Stat.js';
import Hold from '../../../models/schemas/Hold.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Hold
const getRandomHold = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Hug from '../../models/schemas/Hug.js';
import Stats from '../../models/schemas/Stat.js';
import Hug from '../../../models/schemas/Hug.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Hug
const getRandomHug = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Kick from '../../models/schemas/Kick.js';
import Stats from '../../models/schemas/Stat.js';
import Kick from '../../../models/schemas/Kick.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Kick
const getRandomKick = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Kill from '../../models/schemas/Kill.js';
import Stats from '../../models/schemas/Stat.js';
import Kill from '../../../models/schemas/Kill.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Kill
const getRandomKill = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Kissu from '../../models/schemas/Kissu.js';
import Stats from '../../models/schemas/Stat.js';
import Kissu from '../../../models/schemas/Kissu.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Kissu
const getRandomKissu = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Laugh from '../../models/schemas/Laugh.js';
import Stats from '../../models/schemas/Stat.js';
import Laugh from '../../../models/schemas/Laugh.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Laugh
const getRandomLaugh = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Lick from '../../models/schemas/Lick.js';
import Stats from '../../models/schemas/Stat.js';
import Lick from '../../../models/schemas/Lick.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Lick
const getRandomLick = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Love from '../../models/schemas/Love.js';
import Stats from '../../models/schemas/Stat.js';
import Love from '../../../models/schemas/Love.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Love
const getRandomLove = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Lurk from '../../models/schemas/Lurk.js';
import Stats from '../../models/schemas/Stat.js';
import Lurk from '../../../models/schemas/Lurk.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Lurk
const getRandomLurk = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Midfing from '../../models/schemas/Midfing.js';
import Stats from '../../models/schemas/Stat.js';
import Midfing from '../../../models/schemas/Midfing.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Midfing
const getRandomMidfing = async (req, res, next) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import createError from 'http-errors';
import Nervous from '../../models/schemas/Nervous.js';
import Stats from '../../models/schemas/Stat.js';
import Nervous from '../../../models/schemas/Nervous.js';
import Stats from '../../../models/schemas/Stat.js';

// Get random Anime Nervous
const getRandomNervous = async (req, res, next) => {
Expand Down
Loading

0 comments on commit 56f75c9

Please sign in to comment.