Skip to content

Commit

Permalink
Merge pull request #56 from WaifuAPI/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
kyrea authored Feb 5, 2024
2 parents dc02dc4 + 4a9b7d9 commit a295e2f
Show file tree
Hide file tree
Showing 8 changed files with 657 additions and 55 deletions.
57 changes: 35 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "waifu.it",
"version": "4.4.14",
"version": "4.5.14",
"description": "Random API Serving Anime stuff",
"author": "Aeryk",
"private": true,
Expand Down
78 changes: 78 additions & 0 deletions src/controllers/v4/images/husbando.js
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;
4 changes: 2 additions & 2 deletions src/controllers/v4/images/waifu.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ const getWaifu = async (req, res, next) => {

if (name) {
const sanitizedName = _.escapeRegExp(name.trim());
filter['names.en'] = { $regex: new RegExp(sanitizedName, 'i') }; // Case-insensitive regex match for English name
filter['name.full'] = { $regex: new RegExp(sanitizedName, 'i') }; // Case-insensitive regex match for English name
}

if (anime) {
const sanitizedAnime = _.escapeRegExp(anime.trim());
filter['from.name'] = { $regex: new RegExp(sanitizedAnime, 'i') }; // Case-insensitive regex match for anime name
filter['media.nodes[0].title.userPreferred'] = { $regex: new RegExp(sanitizedAnime, 'i') }; // Case-insensitive regex match for anime name
}

/**
Expand Down
Loading

0 comments on commit a295e2f

Please sign in to comment.