0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-25 14:02:40 +00:00

simplify logic

This commit is contained in:
Víctor Losada Hernández
2024-05-14 09:49:14 +02:00
parent 153812c6e5
commit 4b10686336

View File

@@ -2,8 +2,72 @@ const HomebrewModel = require('./homebrew.model.js').model;
const router = require('express').Router();
const asyncHandler = require('express-async-handler');
const buildTitleConditionsArray = (title) => {
if (title.startsWith('/') && title.endsWith('/')) {
return [
{
title: {
$regex: title.slice(1, -1),
$options: 'i',
},
},
];
} else {
return buildTitleConditions(title);
}
};
const buildTitleConditions = (inputString) => {
return [
{
$text: {
$search: inputString,
$caseSensitive: false,
},
},
];
};
const handleErrorResponse = (res, error, functionName) => {
let status;
let message;
if (error.response && error.response.status) {
status = error.response.status;
} else {
status = 500;
}
if (status === 503) {
message = 'Service Unavailable';
} else {
message = 'Internal Server Error';
}
return res.status(status).json({
errorCode: status.toString(),
message: `Error in function ${functionName}: ${message}`,
});
};
const buildBrewsQuery = (legacy, v3) => {
const brewsQuery = {
$or: [],
published: true,
};
if (legacy === 'true') {
brewsQuery.$or.push({ renderer: 'legacy' });
}
if (v3 === 'true') {
brewsQuery.$or.push({ renderer: 'V3' });
}
return brewsQuery;
};
const archive = {
/* Searches for matching title, also attempts to partial match */
findBrews: async (req, res, next) => {
try {
console.log(`Query as received in archive api:`);
@@ -12,48 +76,11 @@ const archive = {
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 pageSize = Math.max(parseInt(req.query.size) || 10, minPageSize);
const skip = (page - 1) * pageSize;
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 brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3);
const titleConditionsArray = buildTitleConditionsArray(title);
const titleQuery = {
$and: [brewsQuery, ...titleConditionsArray],
@@ -71,112 +98,31 @@ const archive = {
.maxTimeMS(5000)
.exec();
return res.json({ brews, page});
return res.json({ brews, page });
} 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',
});
}
return handleErrorResponse(res, error, 'findBrews');
}
},
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);
const brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3);
const titleConditionsArray = buildTitleConditionsArray(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});
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',
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',
});
}
return handleErrorResponse(res, error, 'findTotal');
}
},
};