mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-07 07:42:39 +00:00
use $text instead
This commit is contained in:
@@ -3,61 +3,105 @@ const router = require('express').Router();
|
|||||||
const asyncHandler = require('express-async-handler');
|
const asyncHandler = require('express-async-handler');
|
||||||
|
|
||||||
const archive = {
|
const archive = {
|
||||||
archiveApi : router,
|
archiveApi: router,
|
||||||
/* Searches for matching title, also attempts to partial match */
|
/* Searches for matching title, also attempts to partial match */
|
||||||
findBrews : async (req, res, next)=>{
|
findBrews: async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
console.table(req.query);
|
/*
|
||||||
|
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 title = req.query.title || '';
|
||||||
const page = Math.max(parseInt(req.query.page) || 1, 1);
|
const page = Math.max(parseInt(req.query.page) || 1, 1);
|
||||||
const minPageSize = 6;
|
const minPageSize = 6;
|
||||||
const pageSize = Math.max(parseInt(req.query.size) || 10, minPageSize);
|
const pageSize = Math.max(
|
||||||
const skip = (page - 1) * pageSize;
|
parseInt(req.query.size) || 10,
|
||||||
|
minPageSize
|
||||||
const titleQuery = {
|
);
|
||||||
title : { $regex: decodeURIComponent(title), $options: 'i' },
|
const skip = (page - 1) * pageSize;
|
||||||
$or: [],
|
|
||||||
published : true
|
|
||||||
};
|
|
||||||
|
|
||||||
if (req.query.legacy === 'true') {
|
const brewsQuery = {
|
||||||
titleQuery.$or.push({ renderer: 'legacy' });
|
$or: [],
|
||||||
|
published: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (req.query.legacy === 'true') {
|
||||||
|
brewsQuery.$or.push({ renderer: 'legacy' });
|
||||||
|
}
|
||||||
|
|
||||||
if (req.query.v3 === 'true') {
|
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 = {
|
const projection = {
|
||||||
editId : 0,
|
editId: 0,
|
||||||
googleId : 0,
|
googleId: 0,
|
||||||
text : 0,
|
text: 0,
|
||||||
textBin : 0,
|
textBin: 0,
|
||||||
};
|
};
|
||||||
const brews = await HomebrewModel.find(titleQuery, projection)
|
const brews = await HomebrewModel.find(titleQuery, projection)
|
||||||
.skip(skip)
|
.skip(skip)
|
||||||
.limit(pageSize)
|
.limit(pageSize)
|
||||||
.maxTimeMS(5000)
|
.maxTimeMS(5000)
|
||||||
.exec();
|
.exec();
|
||||||
|
|
||||||
const totalBrews = await HomebrewModel.countDocuments(titleQuery).maxTimeMS(5000);
|
const totalBrews = await HomebrewModel.countDocuments(
|
||||||
|
titleQuery
|
||||||
const totalPages = Math.ceil(totalBrews / pageSize);
|
).maxTimeMS(5000);
|
||||||
//console.log('Total brews: ', totalBrews);
|
|
||||||
//console.log('Total pages: ', totalPages);
|
const totalPages = Math.ceil(totalBrews / pageSize);
|
||||||
return res.json({ brews, page, totalPages, totalBrews});
|
console.log('Total brews: ', totalBrews);
|
||||||
} catch (error) {
|
console.log('Total pages: ', totalPages);
|
||||||
console.error(error);
|
return res.json({ brews, page, totalPages, totalBrews });
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
if (error.response && error.response.status === 503) {
|
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 {
|
} 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));
|
router.get('/api/archive', asyncHandler(archive.findBrews));
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
Reference in New Issue
Block a user