This repository has been archived on 2025-11-04. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
qoverflow/server/routes/question/Search.js
2022-08-18 16:47:35 -04:00

36 lines
804 B
JavaScript

const config = require('server/config.json');
const Question = require('server/db/models/Question');
async function Search(req, res) {
const { creator, title, text, tags, createdAt } = req.query;
let searchQuery = {};
if (creator) {
searchQuery['creator'] = {
$regex: creator,
};
}
if (title) {
searchQuery['title'] = {
$regex: title,
};
}
if (text) {
searchQuery['text'] = {
$regex: text,
};
}
if (tags) {
searchQuery['tags'] = { $all: tags };
}
if (createdAt) {
searchQuery['createdAt'] = createdAt;
}
const questions = await Question.find(searchQuery).sort({ createdAt: 'asc' });
return res.send({ questions });
}
module.exports = Search;