DragonHawk
DragonHawk

Reputation: 39

Webservice with optional parameter and different business logic

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?

Upvotes: 0

Views: 100

Answers (1)

Michael Cropper
Michael Cropper

Reputation: 1032

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...

Upvotes: 1

Related Questions