mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-07 16:22:42 +00:00
simplify logic
This commit is contained in:
@@ -2,8 +2,72 @@ const HomebrewModel = require('./homebrew.model.js').model;
|
|||||||
const router = require('express').Router();
|
const router = require('express').Router();
|
||||||
const asyncHandler = require('express-async-handler');
|
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 = {
|
const archive = {
|
||||||
/* Searches for matching title, also attempts to partial match */
|
|
||||||
findBrews: async (req, res, next) => {
|
findBrews: async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
console.log(`Query as received in archive api:`);
|
console.log(`Query as received in archive api:`);
|
||||||
@@ -12,48 +76,11 @@ const archive = {
|
|||||||
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(
|
const pageSize = Math.max(parseInt(req.query.size) || 10, minPageSize);
|
||||||
parseInt(req.query.size) || 10,
|
|
||||||
minPageSize
|
|
||||||
);
|
|
||||||
const skip = (page - 1) * pageSize;
|
const skip = (page - 1) * pageSize;
|
||||||
|
|
||||||
const brewsQuery = {
|
const brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3);
|
||||||
$or: [],
|
const titleConditionsArray = buildTitleConditionsArray(title);
|
||||||
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 = {
|
const titleQuery = {
|
||||||
$and: [brewsQuery, ...titleConditionsArray],
|
$and: [brewsQuery, ...titleConditionsArray],
|
||||||
@@ -71,112 +98,31 @@ const archive = {
|
|||||||
.maxTimeMS(5000)
|
.maxTimeMS(5000)
|
||||||
.exec();
|
.exec();
|
||||||
|
|
||||||
return res.json({ brews, page});
|
return res.json({ brews, page });
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
return handleErrorResponse(res, error, 'findBrews');
|
||||||
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) => {
|
findTotal: async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const title = req.query.title || '';
|
const title = req.query.title || '';
|
||||||
|
|
||||||
const brewsQuery = {
|
const brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3);
|
||||||
$or: [],
|
const titleConditionsArray = buildTitleConditionsArray(title);
|
||||||
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 = {
|
const titleQuery = {
|
||||||
$and: [brewsQuery, ...titleConditionsArray],
|
$and: [brewsQuery, ...titleConditionsArray],
|
||||||
};
|
};
|
||||||
console.table(req.query);
|
|
||||||
|
|
||||||
const totalBrews = await HomebrewModel.countDocuments(titleQuery);
|
const totalBrews = await HomebrewModel.countDocuments(titleQuery);
|
||||||
|
|
||||||
return res.json({totalBrews});
|
return res.json({ totalBrews });
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
return handleErrorResponse(res, error, 'findTotal');
|
||||||
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',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user