0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-30 08:52:38 +00:00

initial commit

This commit is contained in:
Víctor Losada Hernández
2024-09-10 08:28:34 +02:00
parent 22678b15af
commit ea1d0714b4
3 changed files with 138 additions and 39 deletions

View File

@@ -29,12 +29,20 @@ const rendererConditions = (legacy, v3)=>{
return {}; // If all renderers selected, renderer field not needed in query for speed
};
const sortConditions = (sort, dir) => {
return {
[sort]: dir === 'asc' ? 1 : -1
};
}
const findBrews = async (req, res)=>{
const title = req.query.title || '';
const author = req.query.author || '';
const page = Math.max(parseInt(req.query.page) || 1, 1);
const count = Math.max(parseInt(req.query.count) || 20, 10);
const skip = (page - 1) * count;
const sort = req.query.sort || 'title';
const dir = req.query.dir || 'asc';
const combinedQuery = {
$and : [
@@ -55,6 +63,7 @@ const findBrews = async (req, res)=>{
await HomebrewModel.find(combinedQuery, projection)
.skip(skip)
.sort(sortConditions(sort, dir))
.limit(count)
.maxTimeMS(5000)
.exec()