0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-28 02:42:39 +00:00

use $text instead

This commit is contained in:
Víctor Losada Hernández
2024-05-06 22:59:09 +02:00
parent 752b2caaf4
commit e75c556443

View File

@@ -3,61 +3,105 @@ const router = require('express').Router();
const asyncHandler = require('express-async-handler');
const archive = {
archiveApi : router,
/* Searches for matching title, also attempts to partial match */
findBrews : async (req, res, next)=>{
try {
console.table(req.query);
archiveApi: router,
/* Searches for matching title, also attempts to partial match */
findBrews: async (req, res, next) => {
try {
/*
const dbName = HomebrewModel.db.name;
console.log("Database Name:", dbName);
const collectionName = HomebrewModel.collection.name;
console.log("Collection Name:", collectionName);
*/
console.table(req.query);
const title = req.query.title || '';
const page = Math.max(parseInt(req.query.page) || 1, 1);
const minPageSize = 6;
const pageSize = Math.max(parseInt(req.query.size) || 10, minPageSize);
const skip = (page - 1) * pageSize;
const titleQuery = {
title : { $regex: decodeURIComponent(title), $options: 'i' },
$or: [],
published : true
};
const title = req.query.title || '';
const page = Math.max(parseInt(req.query.page) || 1, 1);
const minPageSize = 6;
const pageSize = Math.max(
parseInt(req.query.size) || 10,
minPageSize
);
const skip = (page - 1) * pageSize;
if (req.query.legacy === 'true') {
titleQuery.$or.push({ renderer: 'legacy' });
const brewsQuery = {
$or: [],
published: true,
};
if (req.query.legacy === 'true') {
brewsQuery.$or.push({ renderer: 'legacy' });
}
if (req.query.v3 === 'true') {
titleQuery.$or.push({ renderer: 'V3' });
brewsQuery.$or.push({ renderer: 'V3' });
}
// If user wants to use RegEx it needs to format like /text/
const titleConditionsArray =
title.startsWith('/') && title.endsWith('/')
? [
{
'title': {
$regex: title.slice(1, -1),
$options: 'i',
},
},
]
: buildTitleConditions(title);
function buildTitleConditions(inputString) {
return [
{
$text: {
$search: inputString,
$caseSensitive: false,
},
},
];
}
const titleQuery = {
$and: [brewsQuery, ...titleConditionsArray],
};
const projection = {
editId : 0,
googleId : 0,
text : 0,
textBin : 0,
};
const brews = await HomebrewModel.find(titleQuery, projection)
const projection = {
editId: 0,
googleId: 0,
text: 0,
textBin: 0,
};
const brews = await HomebrewModel.find(titleQuery, projection)
.skip(skip)
.limit(pageSize)
.maxTimeMS(5000)
.exec();
const totalBrews = await HomebrewModel.countDocuments(titleQuery).maxTimeMS(5000);
const totalPages = Math.ceil(totalBrews / pageSize);
//console.log('Total brews: ', totalBrews);
//console.log('Total pages: ', totalPages);
return res.json({ brews, page, totalPages, totalBrews});
} catch (error) {
console.error(error);
const totalBrews = await HomebrewModel.countDocuments(
titleQuery
).maxTimeMS(5000);
const totalPages = Math.ceil(totalBrews / pageSize);
console.log('Total brews: ', totalBrews);
console.log('Total pages: ', totalPages);
return res.json({ brews, page, totalPages, totalBrews });
} catch (error) {
console.error(error);
if (error.response && error.response.status === 503) {
return res.status(503).json({ errorCode: '503', message: 'Service Unavailable' });
return res
.status(503)
.json({ errorCode: '503', message: 'Service Unavailable' });
} else {
return res.status(500).json({ errorCode: '500', message: 'Internal Server Error' });
return res.status(500).json({
errorCode: '500',
message: 'Internal Server Error',
});
}
}
}
}
},
};
router.get('/api/archive', asyncHandler(archive.findBrews));
module.exports = router;
module.exports = router;