Skip to content

Commit

Permalink
Added query filter using character name or anime
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrea committed Jan 8, 2024
1 parent 72b3670 commit 48288c4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
38 changes: 34 additions & 4 deletions src/controllers/v4/images/waifu.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import createError from 'http-errors';
import Waifus from '../../../models/schemas/Waifu.js';
import Stats from '../../../models/schemas/Stat.js';

Expand All @@ -14,20 +15,49 @@ import Stats from '../../../models/schemas/Stat.js';
* @returns {Object} JSON object containing the random waifu.
* @example
* // Example usage in Express route handler
* getRandomWaifu(req, res, next);
* getWaifu(req, res, next);
*/
const getRandomWaifu = async (req, res, next) => {
const getWaifu = 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 = {};

/**
* Add conditions to filter object based on request parameters
* @type {Object}
*/

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

Check failure

Code scanning / CodeQL

Regular expression injection High

This regular expression is constructed from a
user-provided value
.
}

if (anime) {
filter['from.name'] = { $regex: new RegExp(anime, 'i') }; // Case-insensitive regex match for anime name

Check failure

Code scanning / CodeQL

Regular expression injection High

This regular expression is constructed from a
user-provided value
.
}

/**
* Holds the result of the random waifu retrieval.
* @type {Object}
*/
const [result] = await Waifus.aggregate([
// Select a random document from the results
{ $match: filter }, // Apply filter conditions (if any)
{ $sample: { size: 1 } },
{ $project: { __v: 0 } },
]);

// If no fact is found, return a 404 error
if (!result) {
return next(createError(404, 'Could not find any matching waifu'));
}
/**
* Responds with a JSON object containing the random waifu.
* @type {Object}
Expand Down Expand Up @@ -61,4 +91,4 @@ const getRandomWaifu = async (req, res, next) => {
}
};

export default getRandomWaifu;
export default getWaifu;
6 changes: 3 additions & 3 deletions src/routes/v4/images/waifu.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router } from 'express';
import getRandomWaifu from '../../../controllers/v4/images/waifu.js';
import getWaifu from '../../../controllers/v4/images/waifu.js';
import createRateLimiter from '../../../middlewares/rateLimit.js';
import authorize from '../../../middlewares/authorize.js';
import incrementData from '../../../middlewares/database/add.js';
Expand All @@ -13,7 +13,7 @@ router
/**
* @api {get} v4/waifu Get a Random Waifu
* @apiDescription Retrieve a random Waifu.
* @apiName getRandomWaifu
* @apiName getWaifu
* @apiGroup images
* @apiPermission user
*
Expand All @@ -31,7 +31,7 @@ router
* @returns {function} Express middleware function that handles rate limiting.
*
*/
.get(authorize(config.roles.USER), createRateLimiter(), getRandomWaifu)
.get(authorize(config.roles.USER), createRateLimiter(), getWaifu)
/**
* @api {post} v4/waifu Increment Waifu Data
* @apiDescription Increment data related to Waifus (only accessible by database moderators).
Expand Down

0 comments on commit 48288c4

Please sign in to comment.