There is a library called api-query-params , with which it is possible to perform filters and pagination on a single method, at first it is working perfectly to perform pagination, however I am not having the same result with the filters, look at the following URL below:
http://localhost:3000/menus?skip=1&limit=4
With the settings that were made in the method it is possible to perform pageings as in the URL above that it is listing the first four records in the first tab.
http://localhost:3000/menus?skip=2&limit=3
In this next example it is listing the three records in the second tab, and so on.
I'm having problems with the filters.
I have an entity called Menu, where it has the price and description attributes, and what I need and query through these two attributes of the Menu entity
In the method I created, the filters work only as they are below;
http://localhost:3000/menus?price=2.5
And the description is like this;
http://localhost:3000/menus?description=/cidade/i
The i
f modifier determines that the search will ignore the case of your string.
What I need is a unique way to perform the filter, because with two different ways of filtering, it is very difficult to implement this in Angular Front End, mainly because I have little experience.
The way I need to do the filters would be this way for both the price and the description;
http://localhost:3000/menus?price=2.5
And so for description;
http://localhost:3000/menus?description=cidade
The method that does paging and filters is this;
exports.lista_all_paginacao = async (req, res) => {
const { filter, skip, limit, sort, projection } = aqp(req.query);
Menus
.find(filter)
.skip(skip)
.limit(limit)
.sort(sort)
.select(projection)
.exec(async (err, result) => {
if (err) {
return res.status(500).jsonp({
message:"There was an internal error listing all the providers " + err});
}
let count = await Menus.find().count()
res.status(200).jsonp({
limit: limit,
skip: skip,
total: count,
data: result
});
});
};
This method below can filter the way I need it;
const pesquisaEspecialMenus = async (req, res) => {
try {
const { price, description } = req.query;
let where = {};
if (price) {
where.price = price;
}
if (description) {
where.description = { $regex: description, $options: 'i' };
}
const menu = await Menus.findOne(where).exec();
// Caso não encontre nenhum registro para a busca especificada
if (!menu){
const message = 'Não existe nenhum Reviews nesse registro';
console.error(message);
res.status(404).send({ message });
return;
}
// Se tudo correr bem
res.status(200).send(menu);
} catch(e) {
const message = 'Erro na solicitação.';
console.error(e);
res.status(500).send({ message });
}
}
I ask for help adapting the pagination you have in the pagination_list method with the SpecialMenus search
I tried that way and I did not succeed.
exports.list_all_dataProviders = async (req, res) => {
const { price, description } = req.query;
let where = {};
if (price) {
where.price = price;
}
if (description) {
where.description = { $regex: description, $options: 'i' };
}
const { filter, skip, limit, sort, projection } = aqp(req.query);
Menus
.findOnde(filter(where))
.skip(skip)
.limit(limit)
.sort(sort)
.select(projection)
.exec(async (err, result) => {
if (err) {
return res.status(500).jsonp({message:"There was an internal error listing all the providers " + err});
}
let count = await Menus.find().count()
res.status(200).jsonp({
limit: limit,
skip: skip,
total: count,
data: result
});
});
};
What this method needs to do is query all records, need to perform paging, and also filter by price and description.