0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-15 12:42:45 +00:00

limit the search

This commit is contained in:
Víctor Losada Hernández
2024-01-24 21:15:00 +01:00
parent c50042c1e7
commit 0c167d803c

View File

@@ -2,29 +2,38 @@ 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 archive = { const archive = {
archiveApi : router, archiveApi: router,
/* Searches for matching title, also attempts to partial match */ /* Searches for matching title, also attempts to partial match */
findBrews : async (req, res, next) => { findBrews: async (req, res, next) => {
try { try {
const brews = await HomebrewModel.find({ const limit = 3;
title: { $regex: req.params.query, $options: 'i' }, const brews = await HomebrewModel.find({
published: true title: { $regex: req.params.query, $options: 'i' },
}).exec(); published: true
})
if (!brews || brews.length === 0) { .limit(limit)
// No published documents found with the given title .exec();
return res.status(404).json({ error: 'Published documents not found' });
} if (!brews || brews.length === 0) {
return res.json(brews); // No published documents found with the given title
return res.status(404).json({ error: 'Published documents not found' });
}
let message = '';
if (brews.length === limit) {
// If the limit has been reached, include a message in the response
message = `You've reached the limit of ${limit} documents, you can try being more specific in your search.`;
}
return res.json({ brews, message });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return res.status(500).json({ error: 'Internal Server Error' }); return res.status(500).json({ error: 'Internal Server Error' });
} }
} }
} }
router.get('/archive/:query', asyncHandler(archive.findBrews)); router.get('/archive/:query', asyncHandler(archive.findBrews));
module.exports = router;
module.exports = router;