Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playframework routes with different query parameters

I have two methods in my search controller

searchBoundingBox - does bounding box search and requires the bounding box latitude and longitude values. search - does distance search (does distance based search from the center point).

I have defined my two routes in route file as below.

#Search
#bounding box
GET     /search                             controllers.Search.searchBoundingBox(swLatitude:java.lang.String,swLongitude:java.lang.String, neLatitude:java.lang.String,neLongitude:java.lang.String)

# distance based
GET     /search                             controllers.Search.search(latitude:java.lang.String,longitude:java.lang.String, offset:java.lang.Integer?=0,distance:java.lang.Integer?=50, limit:java.lang.Integer?=10)

But when I create a query in the 2nd route (distance based) it is not resolved.

Any workarounds !!!( Note if I change the order of the routes then the bounding box searches fails)

like image 458
rOrlig Avatar asked Feb 19 '23 09:02

rOrlig


1 Answers

If you are forced to use query params you can use one action for both searches as a workaround:

GET     /search   controllers.Search.search

and fetch the query parameters yourself:

Scala:

def search = Action { request =>
     val latitude = request.queryString.getOrElse("latitude", Seq()).headOption.getOrElse("default")

Java:

public static Result search() {
        String latitude = request().queryString().get("latitude")[0];//of course check if param is in map

So you can split the action:

if(latitude != null)
   searchWithoutBoundingBox()
else
   searchBoundingBox()

You see this is not a nice and typesafe API so it's better to follow Julien's and biesior's approach.

like image 106
Schleichardt Avatar answered Apr 27 '23 20:04

Schleichardt