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

archive 2.0

This commit is contained in:
Víctor Losada Hernández
2024-05-13 13:37:53 +02:00
parent 5eeac603db
commit a89b575b26
4 changed files with 183 additions and 85 deletions

View File

@@ -3,7 +3,6 @@ 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 {
@@ -17,9 +16,11 @@ const archive = {
*/
const bright = '\x1b[1m'; // Bright (bold) style
const yellow = '\x1b[93m'; // yellow color
const yellow = '\x1b[93m'; // yellow color
const reset = '\x1b[0m'; // Reset to default style
console.log(`Query as received in ${bright + yellow}archive api${reset}:`);
console.log(
`Query as received in ${bright + yellow}archive api${reset}:`
);
console.table(req.query);
const title = req.query.title || '';
@@ -84,18 +85,90 @@ const archive = {
.maxTimeMS(5000)
.exec();
const totalBrews = brews.length;
return res.json({ brews, page});
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) {
const status = error.response.status;
if (status === 500) {
return res.status(500).json({
errorCode: '500',
message: 'Internal Server Error',
});
} else if (status === 503) {
return res.status(503).json({
errorCode: '503',
message: 'Service Unavailable',
});
} else {
return res.status(status).json({
errorCode: status.toString(),
message: 'Internal Server Error',
});
}
} else {
return res.status(500).json({
errorCode: '500',
message: 'Internal Server Error',
});
}
}
},
findTotal: async (req, res) => {
try {
const title = req.query.title || '';
const brewsQuery = {
$or: [],
published: true,
};
if (req.query.legacy === 'true') {
brewsQuery.$or.push({ renderer: 'legacy' });
}
if (req.query.v3 === 'true') {
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],
};
console.table(req.query);
const totalBrews = await HomebrewModel.countDocuments(titleQuery);
return res.json({totalBrews});
} catch (error) {
console.error(error);
if (error.response && error.response.status) {
const status = error.response.status;
if (status === 500) {
return res.status(500).json({
errorCode: '500',
@@ -122,6 +195,7 @@ const archive = {
},
};
router.get('/api/archive/total', asyncHandler(archive.findTotal));
router.get('/api/archive', asyncHandler(archive.findBrews));
module.exports = router;