Skip to content

Commit a295e2f

Browse files
authored
Merge pull request #56 from WaifuAPI/staging
Staging
2 parents dc02dc4 + 4a9b7d9 commit a295e2f

File tree

8 files changed

+657
-55
lines changed

8 files changed

+657
-55
lines changed

package-lock.json

+35-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "waifu.it",
3-
"version": "4.4.14",
3+
"version": "4.5.14",
44
"description": "Random API Serving Anime stuff",
55
"author": "Aeryk",
66
"private": true,

src/controllers/v4/images/husbando.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import _ from 'lodash';
2+
import createError from 'http-errors';
3+
import Husbandos from '../../../models/schemas/Husbando.js';
4+
import Stats from '../../../models/schemas/Stat.js';
5+
6+
/**
7+
* Retrieves a random husbando and updates system statistics.
8+
*
9+
* @function
10+
* @param {Object} req - Express request object.
11+
* @param {Object} res - Express response object.
12+
* @param {Function} next - Express next middleware function.
13+
*
14+
* @throws {Error} If there is an error during husbando retrieval or database update.
15+
*
16+
* @returns {Object} JSON object containing the random husbando.
17+
* @example
18+
* // Example usage in Express route handler
19+
* getHusbando(req, res, next);
20+
*/
21+
const getHusbando = async (req, res, next) => {
22+
try {
23+
/**
24+
* Extracts character name and anime parameters from the request query.
25+
* @type {Object}
26+
*/
27+
const { name, anime } = req.query;
28+
29+
/**
30+
* Holds the filter object based on the optional character name and anime name parameters.
31+
* @type {Object}
32+
*/
33+
const filter = {};
34+
35+
/**
36+
* Adds conditions to the filter object based on request parameters.
37+
* @type {Object}
38+
*/
39+
if (name) {
40+
const sanitizedName = _.escapeRegExp(name.trim());
41+
filter['name.full'] = { $regex: new RegExp(sanitizedName, 'i') }; // Case-insensitive regex match for the English name
42+
}
43+
44+
if (anime) {
45+
const sanitizedAnime = _.escapeRegExp(anime.trim());
46+
filter['media.nodes[0].title.userPreferred'] = { $regex: new RegExp(sanitizedAnime, 'i') }; // Case-insensitive regex match for anime name
47+
}
48+
49+
/**
50+
* Holds the result of the random husbando retrieval.
51+
* @type {Object}
52+
*/
53+
const [result] = await Husbandos.aggregate([
54+
{ $match: filter }, // Apply filter conditions (if any)
55+
{ $sample: { size: 1 } },
56+
{ $project: { __v: 0 } },
57+
]);
58+
59+
// If no husbando is found, return a 404 error
60+
if (!result) {
61+
return next(createError(404, 'Could not find any matching husbando'));
62+
}
63+
64+
res.status(200).json(result);
65+
66+
// Update system statistics for husbandos
67+
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { husbandos: 1 } });
68+
} catch (error) {
69+
/**
70+
* Update system statistics for failed requests.
71+
* @type {Object}
72+
*/
73+
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } });
74+
next(error);
75+
}
76+
};
77+
78+
export default getHusbando;

src/controllers/v4/images/waifu.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ const getWaifu = async (req, res, next) => {
3939

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

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

5050
/**

0 commit comments

Comments
 (0)