I've built a REST endpoint for listing items, /api/items
, and it can be passed a few options via query params for filtering the result.
For example, GET /api/items?minPrice=30&maxPrice=100&minRating=4&onSale=true
How can I translate this into my SQL queries?
If I get a request with only maxPrice=50
, then what about the other parameters? Let's just assume the following is my query -
select * from items
where minPrice=(???) and maxPrice=50 and minRating=(???) and onSale=(???)
How do I "ignore" a filter if nothing was requested for it, and only filter on maxPrice=50
? Is there a programmatic way to do this?
In my case, I am using postgres, so I have paramterized query with &1
, &2
, etc. with the values for each one.