Renamed 'user_id' to 'author' & added population to the search results

This commit is contained in:
Mathias Wagner 2022-07-09 17:38:38 +02:00
parent 5dacd265b8
commit 20755e4063
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -6,12 +6,12 @@ const {mongo} = require("mongoose");
module.exports.getAddonById = async (id) => {
if (!mongo.ObjectId.isValid(id)) return null;
return await Addon.findById(id).exec();
return await Addon.findById(id).populate('author', 'username email').exec();
}
// Gets an addon by the user id and addon name
module.exports.getAddonFromUser = async (user_id, addon_name) => {
return await Addon.findOne({user_id, name: addon_name}).exec();
module.exports.getAddonFromUser = async (author, addon_name) => {
return await Addon.findOne({author, name: addon_name}).populate('author', 'username email').exec();
}
// Updates an addon by id
@ -29,7 +29,7 @@ module.exports.listAddonsByAuthorName = async (author_name) => {
const user = await getUserByName(author_name);
if (user === null) return;
return await Addon.find({user_id: user._id}, {__v: 0}).exec();
return await Addon.find({author: user._id}, {__v: 0}).populate('author', 'username email').exec();
}
// Gets an addon by the author and addon name
@ -37,18 +37,18 @@ module.exports.getAddonByName = async (author_name, addon_name) => {
const user = await getUserByName(author_name);
if (user === null) return;
return await Addon.findOne({user_id: user._id, name: {$regex: addon_name, $options: 'i'}}, {__v: 0}).exec();
return await Addon.findOne({author: user._id, name: {$regex: addon_name, $options: 'i'}}, {__v: 0}).populate('author', 'username email').exec();
}
// Searches the database for multiple addons by a query
module.exports.searchAddon = async (query, limit = 25) => {
let options = {$regex: ".*" + query + ".*", $options: "i"};
return await Addon.find({$or: [{name: options}, {info: options}, {description: options}]}, '', {limit}).exec();
return await Addon.find({$or: [{name: options}, {info: options}, {description: options}]}, '', {limit}).populate('author', 'username email').exec();
}
// Checks if the provided addon already exists (by the id of the author & the name of the addon)
module.exports.addonExists = async (author_id, addon_name) => {
if (!mongo.ObjectId.isValid(author_id)) return;
module.exports.addonExists = async (author, addon_name) => {
if (!mongo.ObjectId.isValid(author)) return;
return await Addon.findOne({user_id: author_id, name: {$regex: addon_name, $options: 'i'}}).exec() !== null;
return await Addon.findOne({author, name: {$regex: addon_name, $options: 'i'}}).exec() !== null;
}