|
| 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; |
0 commit comments