mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-24 18:32:41 +00:00
search by author
This commit is contained in:
@@ -1,33 +1,30 @@
|
||||
const HomebrewModel = require('./homebrew.model.js').model;
|
||||
const router = require('express').Router();
|
||||
const express = require('express');
|
||||
const asyncHandler = require('express-async-handler');
|
||||
const HomebrewModel = require('./homebrew.model.js').model;
|
||||
|
||||
const buildTitleConditions = (inputString) => {
|
||||
return [
|
||||
{
|
||||
$text: {
|
||||
$search: inputString,
|
||||
$caseSensitive: false,
|
||||
},
|
||||
const router = express.Router();
|
||||
|
||||
const buildTitleConditions = (title) => {
|
||||
if (!title) return {};
|
||||
return {
|
||||
$text: {
|
||||
$search: title,
|
||||
$caseSensitive: false,
|
||||
},
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
const buildAuthorConditions = (author, owner) => {
|
||||
if (!author) return {};
|
||||
return owner ? { 'authors.0': author } : { authors: author };
|
||||
};
|
||||
|
||||
const handleErrorResponse = (res, error, functionName) => {
|
||||
let status;
|
||||
let message;
|
||||
const status = error.response?.status || 500;
|
||||
const message =
|
||||
status === 503 ? 'Service Unavailable' : 'Internal Server Error';
|
||||
|
||||
if (error.response && error.response.status) {
|
||||
status = error.response.status;
|
||||
} else {
|
||||
status = 500;
|
||||
}
|
||||
|
||||
if (status === 503) {
|
||||
message = 'Service Unavailable';
|
||||
} else {
|
||||
message = 'Internal Server Error';
|
||||
}
|
||||
console.error(`Error in ${functionName}:`, error);
|
||||
|
||||
return res.status(status).json({
|
||||
errorCode: status.toString(),
|
||||
@@ -37,43 +34,35 @@ const handleErrorResponse = (res, error, functionName) => {
|
||||
|
||||
const buildBrewsQuery = (legacy, v3) => {
|
||||
const renderers = [];
|
||||
if (legacy === 'true') renderers.push('legacy');
|
||||
if (v3 === 'true') renderers.push('V3');
|
||||
|
||||
if (legacy === 'true') {
|
||||
renderers.push('legacy');
|
||||
}
|
||||
|
||||
if (v3 === 'true') {
|
||||
renderers.push('V3');
|
||||
}
|
||||
|
||||
const brewsQuery = {
|
||||
published: true,
|
||||
};
|
||||
|
||||
if (renderers.length > 0) {
|
||||
brewsQuery.renderer = { $in: renderers };
|
||||
}
|
||||
const brewsQuery = { published: true };
|
||||
if (renderers.length > 0) brewsQuery.renderer = { $in: renderers };
|
||||
|
||||
return brewsQuery;
|
||||
};
|
||||
|
||||
const vault = {
|
||||
findBrews: async (req, res, next) => {
|
||||
findBrews: async (req, res) => {
|
||||
try {
|
||||
console.log(`Query as received in vault api for findBrews:`);
|
||||
console.table(req.query);
|
||||
|
||||
const title = req.query.title || '';
|
||||
const author = req.query.author || '';
|
||||
const owner = req.query.owner === 'true';
|
||||
const page = Math.max(parseInt(req.query.page) || 1, 1);
|
||||
const mincount = 10;
|
||||
const count = Math.max(parseInt(req.query.count) || 20, mincount);
|
||||
const skip = (page - 1) * count;
|
||||
|
||||
const brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3);
|
||||
const titleConditionsArray = buildTitleConditions(title);
|
||||
const titleConditions = buildTitleConditions(title);
|
||||
const authorConditions = buildAuthorConditions(author, owner);
|
||||
|
||||
const titleQuery = {
|
||||
$and: [brewsQuery, ...titleConditionsArray],
|
||||
const combinedQuery = {
|
||||
$and: [brewsQuery, titleConditions, authorConditions]
|
||||
};
|
||||
|
||||
const projection = {
|
||||
@@ -82,35 +71,51 @@ const vault = {
|
||||
text: 0,
|
||||
textBin: 0,
|
||||
};
|
||||
const brews = await HomebrewModel.find(titleQuery, projection)
|
||||
const brews = await HomebrewModel.find(combinedQuery, projection)
|
||||
.skip(skip)
|
||||
.limit(count)
|
||||
.maxTimeMS(5000)
|
||||
.exec();
|
||||
|
||||
console.log('query', JSON.stringify(combinedQuery, null, 2));
|
||||
return res.json({ brews, page });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return handleErrorResponse(res, error, 'findBrews');
|
||||
}
|
||||
},
|
||||
|
||||
findTotal: async (req, res) => {
|
||||
console.log(`Query as received in vault api for totalBrews:`);
|
||||
console.table(req.query);
|
||||
try {
|
||||
const title = req.query.title || '';
|
||||
const author = req.query.author || '';
|
||||
const owner = req.query.owner === 'true';
|
||||
|
||||
const brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3);
|
||||
const titleConditionsArray = buildTitleConditions(title);
|
||||
const titleConditions = buildTitleConditions(title);
|
||||
const authorConditions = buildAuthorConditions(author, owner);
|
||||
|
||||
const titleQuery = {
|
||||
$and: [brewsQuery, ...titleConditionsArray],
|
||||
const combinedQuery = {
|
||||
$and: [brewsQuery, titleConditions, authorConditions]
|
||||
};
|
||||
const totalBrews = await HomebrewModel.countDocuments(titleQuery);
|
||||
console.log('when returning, totalbrews is ', totalBrews, 'for the query ',JSON.stringify(titleQuery));
|
||||
return res.json({ totalBrews });
|
||||
|
||||
console.log(
|
||||
'Combined Query:',
|
||||
JSON.stringify(combinedQuery, null, 2)
|
||||
);
|
||||
|
||||
const totalBrews = await HomebrewModel.countDocuments(
|
||||
combinedQuery
|
||||
);
|
||||
console.log(
|
||||
'when returning, totalbrews is ',
|
||||
totalBrews,
|
||||
'for the query',
|
||||
JSON.stringify(combinedQuery)
|
||||
);
|
||||
return res.json({ totalBrews });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return handleErrorResponse(res, error, 'findTotal');
|
||||
|
||||
Reference in New Issue
Block a user