dimanche 18 juillet 2021

What is the correct way to express "select all when nothing is specified in parameter"?

Let's say we have an HTTP endpoint to get all elements by name?

GET /elements?name={name}

{name} can have a value of CSV or be absent

valid:

  GET /elements?name=Bill,Mary,Ann
  GET /elements?name=Mike
  GET /elements

invalid:
  GET /elements?name=

Somehow we find out in controller that name is not passed. We know that the contract implies to return all values for elements. Possible decisions on further actions (I've seen in different projects) are:

  • using a NULL or a "dummy" substitution like a secret char sequence "!@$@%#$" and juggling them in database while building a query
  • using if (present) { executeQueryA } else { executeQueryB } logic

I am not sure I like either of these approaches, because when there is more than one optional filter these designs become unmaintainable. Something makes me believe that there is a better way to handle the situation.

What would be a proper design on back-end and in database query to handle the case "select all" when nothing is given? Just a general idea and some pseudo-code will be much appreciated.

Aucun commentaire:

Enregistrer un commentaire