-
-
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.
Merge pull request #56 from WaifuAPI/staging
Staging
- Loading branch information
Showing
8 changed files
with
657 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import _ from 'lodash'; | ||
import createError from 'http-errors'; | ||
import Husbandos from '../../../models/schemas/Husbando.js'; | ||
import Stats from '../../../models/schemas/Stat.js'; | ||
|
||
/** | ||
* Retrieves a random husbando and updates system statistics. | ||
* | ||
* @function | ||
* @param {Object} req - Express request object. | ||
* @param {Object} res - Express response object. | ||
* @param {Function} next - Express next middleware function. | ||
* | ||
* @throws {Error} If there is an error during husbando retrieval or database update. | ||
* | ||
* @returns {Object} JSON object containing the random husbando. | ||
* @example | ||
* // Example usage in Express route handler | ||
* getHusbando(req, res, next); | ||
*/ | ||
const getHusbando = async (req, res, next) => { | ||
try { | ||
/** | ||
* Extracts character name and anime parameters from the request query. | ||
* @type {Object} | ||
*/ | ||
const { name, anime } = req.query; | ||
|
||
/** | ||
* Holds the filter object based on the optional character name and anime name parameters. | ||
* @type {Object} | ||
*/ | ||
const filter = {}; | ||
|
||
/** | ||
* Adds conditions to the filter object based on request parameters. | ||
* @type {Object} | ||
*/ | ||
if (name) { | ||
const sanitizedName = _.escapeRegExp(name.trim()); | ||
filter['name.full'] = { $regex: new RegExp(sanitizedName, 'i') }; // Case-insensitive regex match for the English name | ||
} | ||
|
||
if (anime) { | ||
const sanitizedAnime = _.escapeRegExp(anime.trim()); | ||
filter['media.nodes[0].title.userPreferred'] = { $regex: new RegExp(sanitizedAnime, 'i') }; // Case-insensitive regex match for anime name | ||
} | ||
|
||
/** | ||
* Holds the result of the random husbando retrieval. | ||
* @type {Object} | ||
*/ | ||
const [result] = await Husbandos.aggregate([ | ||
{ $match: filter }, // Apply filter conditions (if any) | ||
{ $sample: { size: 1 } }, | ||
{ $project: { __v: 0 } }, | ||
]); | ||
|
||
// If no husbando is found, return a 404 error | ||
if (!result) { | ||
return next(createError(404, 'Could not find any matching husbando')); | ||
} | ||
|
||
res.status(200).json(result); | ||
|
||
// Update system statistics for husbandos | ||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { husbandos: 1 } }); | ||
} catch (error) { | ||
/** | ||
* Update system statistics for failed requests. | ||
* @type {Object} | ||
*/ | ||
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); | ||
next(error); | ||
} | ||
}; | ||
|
||
export default getHusbando; |
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.