2
I have this question about endpoints express bred.
In this first section, the endpoint /anuncios/:search
must search in the fields descricao
and nome
by the text reported. Therefore, a call /anuncios/carro
will return all results with carro
in the name or description.
app.get('/anuncios/:search', function (req, res) {
let search = req.params.search;
if (!search) {
return res.status(400).send({message: 'Nada encontrado para ' +search });
}
else
{
search = '%' +search.replace(' ', '%') +'%';
dbConn.query(
'SELECT * FROM anuncios where (descricao LIKE ?) '
+'OR (nome LIKE ?)',
[search, search], function(err, result){
res.send(result)
});
}
});
Already this second endpoint, it must search for the informed ID. This way, when giving a GET /anuncios/id/2
, the API returns the ID 2 ad.
app.get('/anuncios/id/:id', function (req, res) {
let id = req.params.id;
if (!id) {
return res.status(400).send({message: 'Nenhum ID informado'});
}
else
{
dbConn.query(
'SELECT * FROM anuncios where id = ?', [id],
function(err, result){
res.send(result)
}
);
}
});
My problem is when I make a call with an empty ID (GET /anuncios/id/
). In this case, the API is doing a query in the first endpoint, searching for names and descriptions with the ID provided in the text.
How should I make that call without this problem?
Thanks, Luiz! It was very clear the idea now!
– Cleber Griff