0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-16 03:52:42 +00:00

all sugggested changes

This commit is contained in:
Víctor Losada Hernández
2024-08-31 22:28:29 +02:00
parent 19102db23a
commit fb8c4c5c44

View File

@@ -19,25 +19,8 @@ const buildAuthorConditions = (author) => {
return { authors: author }; return { authors: author };
}; };
//"$and": [ {"published": true}, {"$text": { "$search": "titleString", "$caseSensitive": false } }, { "authors" : "authorString"}] const buildRendererConditions = (legacy, v3) => {
//is a good example of a query constructed with this function const brewsQuery = {};
const handleErrorResponse = (res, error, functionName) => {
const status = error.response?.status || 500;
const message =
status === 503 ? 'Service Unavailable' : 'Internal Server Error';
console.error(`Error in ${functionName}:`, error);
return res.status(status).json({
errorCode: status.toString(),
message: `Error in function ${functionName}: ${message}`,
});
};
const buildBrewsQuery = (legacy, v3) => {
const brewsQuery = { published: true };
if (legacy === 'true' && v3 === 'true') return { published: true };
if (legacy === 'true' && v3 !== 'true') { if (legacy === 'true' && v3 !== 'true') {
brewsQuery.renderer = 'legacy'; brewsQuery.renderer = 'legacy';
@@ -48,81 +31,96 @@ const buildBrewsQuery = (legacy, v3) => {
return brewsQuery; return brewsQuery;
}; };
const vault = { // Function to find brews
findBrews: async (req, res) => { const findBrews = async (req, res) => {
try { const title = req.query.title || '';
const title = req.query.title || ''; const author = req.query.author || '';
const author = req.query.author || ''; const page = Math.max(parseInt(req.query.page) || 1, 1);
const page = Math.max(parseInt(req.query.page) || 1, 1); const mincount = 10;
const mincount = 10; const count = Math.max(parseInt(req.query.count) || 20, mincount);
const count = Math.max(parseInt(req.query.count) || 20, mincount); const skip = (page - 1) * count;
const skip = (page - 1) * count;
const brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3); const brewsQuery = buildRendererConditions(req.query.legacy, req.query.v3);
const titleConditions = buildTitleConditions(title); const titleConditions = buildTitleConditions(title);
const authorConditions = buildAuthorConditions(author); const authorConditions = buildAuthorConditions(author);
const combinedQuery = { const combinedQuery = {
$and: [brewsQuery, titleConditions, authorConditions], $and: [
}; { published: true },
brewsQuery,
titleConditions,
authorConditions,
],
};
const projection = { const projection = {
editId: 0, editId: 0,
googleId: 0, googleId: 0,
text: 0, text: 0,
textBin: 0, textBin: 0,
version: 0, version: 0,
thumbnail: 0, thumbnail: 0,
}; };
const brews = await HomebrewModel.find(combinedQuery, projection)
.skip(skip)
.limit(count)
.maxTimeMS(5000)
.exec();
await HomebrewModel.find(combinedQuery, projection)
.skip(skip)
.limit(count)
.maxTimeMS(5000)
.exec()
.then((brews) => {
console.log( console.log(
'Query in findBrews: ', 'Query in findBrews: ',
JSON.stringify(combinedQuery, null, 2) JSON.stringify(combinedQuery, null, 2)
); );
return res.json({ brews, page }); res.json({ brews, page });
} catch (error) { })
.catch((error) => {
console.error(error); console.error(error);
return handleErrorResponse(res, error, 'findBrews'); res.status(500).json({
} error: 'Error finding brews in Vault search',
}, HBErrorCode: '99',
});
});
};
findTotal: async (req, res) => { // Function to find total brews
try { const findTotal = async (req, res) => {
const title = req.query.title || ''; const title = req.query.title || '';
const author = req.query.author || ''; const author = req.query.author || '';
const brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3); const brewsQuery = buildRendererConditions(req.query.legacy, req.query.v3);
const titleConditions = buildTitleConditions(title); const titleConditions = buildTitleConditions(title);
const authorConditions = buildAuthorConditions(author); const authorConditions = buildAuthorConditions(author);
const combinedQuery = { const combinedQuery = {
$and: [brewsQuery, titleConditions, authorConditions], $and: [
}; { published: true },
brewsQuery,
titleConditions,
authorConditions,
],
};
const totalBrews = await HomebrewModel.countDocuments( await HomebrewModel.countDocuments(combinedQuery)
combinedQuery .then((totalBrews) => {
);
console.log( console.log(
'when returning, the total of brews is ', 'when returning, the total of brews is ',
totalBrews, totalBrews,
'for the query', 'for the query',
JSON.stringify(combinedQuery) JSON.stringify(combinedQuery)
); );
return res.json({ totalBrews }); res.json({ totalBrews });
} catch (error) { })
.catch((error) => {
console.error(error); console.error(error);
return handleErrorResponse(res, error, 'findTotal'); res.status(500).json({
} error: 'Error finding brews in Vault search find total search',
}, HBErrorCode: '99',
});
});
}; };
router.get('/api/vault/total', asyncHandler(vault.findTotal)); router.get('/api/vault/total', asyncHandler(findTotal));
router.get('/api/vault', asyncHandler(vault.findBrews)); router.get('/api/vault', asyncHandler(findBrews));
module.exports = router; module.exports = router;