-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved the routes and controllers separately to different version folders
- Loading branch information
Showing
165 changed files
with
3,008 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomAngry.js → src/controllers/v3/gifs/randomAngry.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomBaka.js → src/controllers/v3/gifs/randomBaka.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomBite.js → src/controllers/v3/gifs/randomBite.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomBlush.js → src/controllers/v3/gifs/randomBlush.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomBonk.js → src/controllers/v3/gifs/randomBonk.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomBored.js → src/controllers/v3/gifs/randomBored.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomBully.js → src/controllers/v3/gifs/randomBully.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomBye.js → src/controllers/v3/gifs/randomBye.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomChase.js → src/controllers/v3/gifs/randomChase.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomCheer.js → src/controllers/v3/gifs/randomCheer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomCringe.js → src/controllers/v3/gifs/randomCringe.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomCry.js → src/controllers/v3/gifs/randomCry.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomCuddle.js → src/controllers/v3/gifs/randomCuddle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomDab.js → src/controllers/v3/gifs/randomDab.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomDance.js → src/controllers/v3/gifs/randomDance.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomDie.js → src/controllers/v3/gifs/randomDie.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomDisgust.js → src/controllers/v3/gifs/randomDisgust.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomFacepalm.js → src/controllers/v3/gifs/randomFacepalm.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomFeed.js → src/controllers/v3/gifs/randomFeed.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomGlomp.js → src/controllers/v3/gifs/randomGlomp.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomHappy.js → src/controllers/v3/gifs/randomHappy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomHi.js → src/controllers/v3/gifs/randomHi.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomHighfive.js → src/controllers/v3/gifs/randomHighfive.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomHold.js → src/controllers/v3/gifs/randomHold.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomHug.js → src/controllers/v3/gifs/randomHug.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomKick.js → src/controllers/v3/gifs/randomKick.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomKill.js → src/controllers/v3/gifs/randomKill.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomKissu.js → src/controllers/v3/gifs/randomKissu.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomLaugh.js → src/controllers/v3/gifs/randomLaugh.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomLick.js → src/controllers/v3/gifs/randomLick.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomLove.js → src/controllers/v3/gifs/randomLove.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomLurk.js → src/controllers/v3/gifs/randomLurk.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomMidfing.js → src/controllers/v3/gifs/randomMidfing.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/controllers/gifs/randomNervous.js → src/controllers/v3/gifs/randomNervous.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.