0

Question: What is the best way to design a webservice that has optional parameters and different business logic for certain parameters?


Specific example:

The web service is intended to make it possible to search for houses.

A house resource has a structure like this:

{
    "houseId": 3123,
    "street": "Drake Street",
    "houseNo": "789",
    "city": "London",
    "zipCode: "EC2R 6AB",
    "country": "uk",
    "coordinates": "51.506885, -0.128107",
    "inventory": ["Veranda", "Baloncy", "Stove"]
}

The web service should perform a proximity search for a given address or coordinate and return houses that meet the given criteria.

Valid example:

/rest/v1/houses?city=London&street=Drake Street&inventory=Balcony&inventory=Veranda

Now it could also be possible to send requests as follows:

/rest/v1/houses?city=London&street=Drake Street&coordinates=51.506885, -0.128107

In this case, I would have to decide which geographical information has priority. Coordinates or Street+City+ZipCode.

In order to make things even more complicated, it should also be possible to pass the radius and the number of search results to the webservice. For performance reasons it should not be possible to get an unlimited number of search results if the country is missing.

Furthermore, it should not be possible to get an unlimited number of search results for an infinite radius. Therefore, one of the two values must be set to a finite number to trigger a search.

And so on... further logic...


Concluding:

It's not important to identify a particular house (resource) by id or anything like this. It is important to filter the existing houses by criteria.

When filtering, however, it must be ensured that there is different business logic for certain parameters.

How can I map such a problem elegantly in the backend? Are there any patterns that support such an approach?

DragonHawk
  • 39
  • 7
  • https://stackoverflow.com/questions/5020704/how-to-design-restful-search-filtering – Ashish Mathew Nov 20 '17 at 15:32
  • Any answers you might receive are primarily opinion based hence this question is a good candidate for being closed. Next, there are a lot of similar questions here at SO that provide pro's and con's of different approaches. Last but not least, your service is (with high certainty) not following REST principles anyway and thus should avoid the term [REST](https://devblast.com/b/calling-your-web-api-restful-youre-doing-it-wrong). – Roman Vottner Nov 20 '17 at 17:19

1 Answers1

1

Filtering can be a difficult thing to get right, so there is no right or wrong answer here. Here's how I would approach this though.

Firstly, let's say you have a RESTful URL for;

/rest/v1/search-houses/

Which can take in multiple parameters, one or more.

Based on this, you can then see what parameters you need to filter on. Here is where your business logic lies and this very much depends on the structure of your code base, database and more.

On the design pattern front, MVC in my opinion solves a lot of problems. In this instance, if you are always filtering data from a single table with basic lookups, i.e. where town = London or country = UK, then this is easy. Simply use the parameters that have been entered to build a String that will be there where clause part. Make sure to do this safely though using prepared statements.

If you are looking up in multiple tables or different places depending on what information has been entered, this gets more tricky. As such, in this instance I'd probably look to create multiple Model files that follow the same approach as outlined above but are tailored towards the specific database query.

Hope that helps you get going in the right direction. There is no single answer to this question unfortunately. As with many things, it depends...

Michael Cropper
  • 872
  • 1
  • 10
  • 28
  • Hi Michael, thanks for this answer.I have already developed a webservice that accepts a variety of parameters, exectues business logic based on the parameter and then construct a SQL statement (as you have already described). Unfortunately, more and more parameters and business logic are being added. It is thus becoming more and more complicated to maintain the webservice method and the filtering.The validation of the incoming parameters on the basis of the business logic and the resulting search parameter to use for the db query are the biggest problem. – DragonHawk Nov 20 '17 at 16:05
  • Yes these types of things can soon get very messy. I've yet to find a perfect solution to this. The best I've done is to put all DB queries in individual Model files which separates things out as much as possible, although depending on the individual requirements, this can be worse in some circumstances. – Michael Cropper Nov 20 '17 at 16:37
  • How does an `un-RESTful URL` look like? – Roman Vottner Nov 20 '17 at 17:20