From 4b10686336e4b2f2963c8cb1b306f201b689c047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Losada=20Hern=C3=A1ndez?= Date: Tue, 14 May 2024 09:49:14 +0200 Subject: [PATCH] simplify logic --- server/archive.api.js | 202 ++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 128 deletions(-) diff --git a/server/archive.api.js b/server/archive.api.js index 36aa93429..e9e118152 100644 --- a/server/archive.api.js +++ b/server/archive.api.js @@ -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'); } }, };