-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Update API code to ES6 from CommonJS
- Loading branch information
Showing
156 changed files
with
1,780 additions
and
1,539 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
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
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 |
---|---|---|
@@ -1,44 +1,58 @@ | ||
const createError = require('http-errors') | ||
const Facts = require('../../models/schemas/Facts') | ||
const tagsFilter = require('../../utils/tagsFilter') | ||
const lengthFilter = require('../../utils/lengthFilter') | ||
const Stats = require('../../models/schemas/Stat') | ||
|
||
// Get random Anime Fact | ||
module.exports = async function getRandomFact(req, res, next) { | ||
import createError from 'http-errors'; | ||
import Facts from '../../models/schemas/Facts.js'; | ||
import tagsFilter from '../../utils/tagsFilter.js'; | ||
import lengthFilter from '../../utils/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 | ||
const { minLength, maxLength, tags } = req.query; | ||
|
||
const filter = {} | ||
// 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) | ||
filter.length = lengthFilter(minLength, maxLength); | ||
} | ||
|
||
// Apply tags filter (if tags are provided) | ||
if (tags) { | ||
filter.tags = tagsFilter(tags) | ||
filter.tags = tagsFilter(tags); | ||
} | ||
|
||
// Aggregate to match the filter, select a random fact, and project excluding version field | ||
const [result] = await Facts.aggregate([ | ||
// Apply filters (if any) | ||
{ $match: filter }, | ||
// Select a random document from the results | ||
{ $sample: { size: 1 } }, | ||
{ $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')) | ||
return next(createError(404, 'Could not find any matching fact')); | ||
} | ||
|
||
res.status(200).json(result) | ||
// Respond with the random fact | ||
res.status(200).json(result); | ||
|
||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { facts: 1 } }) | ||
// 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) | ||
); | ||
return next(error); | ||
} | ||
} | ||
}; | ||
|
||
export default getRandomFact; |
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 |
---|---|---|
@@ -1,28 +1,31 @@ | ||
const createError = require('http-errors') | ||
const Angry = require('../../models/schemas/Angry') | ||
const Stats = require('../../models/schemas/Stat') | ||
|
||
import createError from 'http-errors'; | ||
import Angry from '../../models/schemas/Angry.js'; | ||
import Stats from '../../models/schemas/Stat.js'; | ||
|
||
// Get random Anime Angry | ||
module.exports = async function getRandomAngry(req, res, next) { | ||
const getRandomAngry = async (req, res, next) => { | ||
try { | ||
const [result] = await Angry.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 Angry Gif')) | ||
return next(createError(404, 'Could not find any Angry Gif')); | ||
} | ||
|
||
res.status(200).json(result) | ||
res.status(200).json(result); | ||
|
||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { angry: 1 } }) | ||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { angry: 1 } }); | ||
} catch (error) { | ||
await Stats.findOneAndUpdate( | ||
{ _id: 'systemstats' }, | ||
{ $inc: { failed_requests: 1 } } | ||
) | ||
return next(error) | ||
); | ||
return next(error); | ||
} | ||
} | ||
}; | ||
|
||
export default getRandomAngry; |
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 |
---|---|---|
@@ -1,28 +1,31 @@ | ||
const createError = require('http-errors') | ||
const Baka = require('../../models/schemas/Baka') | ||
const Stats = require('../../models/schemas/Stat') | ||
|
||
import createError from 'http-errors'; | ||
import Baka from '../../models/schemas/Baka.js'; | ||
import Stats from '../../models/schemas/Stat.js'; | ||
|
||
// Get random Anime Baka | ||
module.exports = async function getRandomBaka(req, res, next) { | ||
const getRandomBaka = async (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')) | ||
return next(createError(404, 'Could not find any Baka Gif')); | ||
} | ||
|
||
res.status(200).json(result) | ||
res.status(200).json(result); | ||
|
||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { baka: 1 } }) | ||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { baka: 1 } }); | ||
} catch (error) { | ||
await Stats.findOneAndUpdate( | ||
{ _id: 'systemstats' }, | ||
{ $inc: { failed_requests: 1 } } | ||
) | ||
return next(error) | ||
); | ||
return next(error); | ||
} | ||
} | ||
}; | ||
|
||
export default getRandomBaka; |
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 |
---|---|---|
@@ -1,28 +1,31 @@ | ||
const createError = require('http-errors') | ||
const Bite = require('../../models/schemas/Bite') | ||
const Stats = require('../../models/schemas/Stat') | ||
|
||
import createError from 'http-errors'; | ||
import Bite from '../../models/schemas/Bite.js'; | ||
import Stats from '../../models/schemas/Stat.js'; | ||
|
||
// Get random Anime Bite | ||
module.exports = async function getRandomBite(req, res, next) { | ||
const getRandomBite = async (req, res, next) => { | ||
try { | ||
const [result] = await Bite.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 Bite Gif')) | ||
return next(createError(404, 'Could not find any Bite Gif')); | ||
} | ||
|
||
res.status(200).json(result) | ||
res.status(200).json(result); | ||
|
||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bite: 1 } }) | ||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bite: 1 } }); | ||
} catch (error) { | ||
await Stats.findOneAndUpdate( | ||
{ _id: 'systemstats' }, | ||
{ $inc: { failed_requests: 1 } } | ||
) | ||
return next(error) | ||
); | ||
return next(error); | ||
} | ||
} | ||
}; | ||
|
||
export default getRandomBite; |
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 |
---|---|---|
@@ -1,28 +1,31 @@ | ||
const createError = require('http-errors') | ||
const Blush = require('../../models/schemas/Blush') | ||
const Stats = require('../../models/schemas/Stat') | ||
|
||
import createError from 'http-errors'; | ||
import Blush from '../../models/schemas/Blush.js'; | ||
import Stats from '../../models/schemas/Stat.js'; | ||
|
||
// Get random Anime Blush | ||
module.exports = async function getRandomBlush(req, res, next) { | ||
const getRandomBlush = async (req, res, next) => { | ||
try { | ||
const [result] = await Blush.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 Blush Gif')) | ||
return next(createError(404, 'Could not find any Blush Gif')); | ||
} | ||
|
||
res.status(200).json(result) | ||
res.status(200).json(result); | ||
|
||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { blush: 1 } }) | ||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { blush: 1 } }); | ||
} catch (error) { | ||
await Stats.findOneAndUpdate( | ||
{ _id: 'systemstats' }, | ||
{ $inc: { failed_requests: 1 } } | ||
) | ||
return next(error) | ||
); | ||
return next(error); | ||
} | ||
} | ||
}; | ||
|
||
export default getRandomBlush; |
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 |
---|---|---|
@@ -1,28 +1,31 @@ | ||
const createError = require('http-errors') | ||
const Bonk = require('../../models/schemas/Bonk') | ||
const Stats = require('../../models/schemas/Stat') | ||
|
||
import createError from 'http-errors'; | ||
import Bonk from '../../models/schemas/Bonk.js'; | ||
import Stats from '../../models/schemas/Stat.js'; | ||
|
||
// Get random Anime Bonk | ||
module.exports = async function getRandomBonk(req, res, next) { | ||
const getRandomBonk = async (req, res, next) => { | ||
try { | ||
const [result] = await Bonk.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 Bonk Gif')) | ||
return next(createError(404, 'Could not find any Bonk Gif')); | ||
} | ||
|
||
res.status(200).json(result) | ||
res.status(200).json(result); | ||
|
||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bonk: 1 } }) | ||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bonk: 1 } }); | ||
} catch (error) { | ||
await Stats.findOneAndUpdate( | ||
{ _id: 'systemstats' }, | ||
{ $inc: { failed_requests: 1 } } | ||
) | ||
return next(error) | ||
); | ||
return next(error); | ||
} | ||
} | ||
}; | ||
|
||
export default getRandomBonk; |
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 |
---|---|---|
@@ -1,28 +1,31 @@ | ||
const createError = require('http-errors') | ||
const Bored = require('../../models/schemas/Bored') | ||
const Stats = require('../../models/schemas/Stat') | ||
|
||
import createError from 'http-errors'; | ||
import Bored from '../../models/schemas/Bored.js'; | ||
import Stats from '../../models/schemas/Stat.js'; | ||
|
||
// Get random Anime Bored | ||
module.exports = async function getRandomBored(req, res, next) { | ||
const getRandomBored = async (req, res, next) => { | ||
try { | ||
const [result] = await Bored.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 Bored Gif')) | ||
return next(createError(404, 'Could not find any Bored Gif')); | ||
} | ||
|
||
res.status(200).json(result) | ||
res.status(200).json(result); | ||
|
||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bored: 1 } }) | ||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bored: 1 } }); | ||
} catch (error) { | ||
await Stats.findOneAndUpdate( | ||
{ _id: 'systemstats' }, | ||
{ $inc: { failed_requests: 1 } } | ||
) | ||
return next(error) | ||
); | ||
return next(error); | ||
} | ||
} | ||
}; | ||
|
||
export default getRandomBored; |
Oops, something went wrong.