Would it be possible to add an option for the api user to filter the wine query by year? However, if no year is specified, mongoose should not return an empty array. The same applies to the price property.
For example, http://localhost:1234/api/wine?year=2010 should return wines from 2010.
http://localhost:1234/api/wine should return all wines (limited to 10).
I have successfully implemented other filters as shown below. Is this the most efficient way to achieve this?
Thank you
controller
getWines: async (req, res) => {
try {
const types = ['red', 'white'];
let {
limit = 10,
page = 1,
// sort = 'asc',
search = '',
type = 'all',
year = undefined,
} = req.query;
if (page === '0') {
return res.json({ error: 'Invalid page' });
}
type === 'all' ? (type = [...types]) : (type = [req.query.type]);
const response = await Wine.find({
name: { $regex: search, $options: 'i' },
})
.where('type')
.in(type)
// .where('year')
// .equals(parseInt(year))
// .sort(sort)
.limit(limit)
.skip((parseInt(page) - 1) * limit);
res.json(response);
} catch (error) {
console.error(error);
}
},
documents sample
[{
"_id": "63952372129acf895c427240",
"name": "Chateau Leoville Barton",
"year": 2010,
"type": "red",
"domain": "Saint-Julien",
"quantity": 750,
"price": 169,
"quality": 100,
"image": <<<<LONG_URL>>>>
},
{
"_id": "639523e7129acf895c42c238",
"name": "Chateau La Mission Haut Brion",
"year": 2014,
"type": "red",
"domain": "Pessac-Leognan",
"quantity": 750,
"price": 219,
"quality": 94,
"image": <<<<LONG_URL>>>>
}]