Reputation: 4878
I have a controller, that have some functions.
My problem is that i wish to do:
GET /v1.0/products @controllers.ProductController.getProductsByCategory(lang: String ?="en_US", t:String, cat:String, start: Int ?=0, maxresults:Int ?=100, sort:String ?="rank", order:String ?="asc")
GET /v1.0/products @controllers.ProductController.getProducts(lang: String ?="en_US", t: String, ids: String)
I have the 2 functions in the controller:
def getProducts(lang: String, t: String, ids: String) = Action { ... code.. }
and
def getProductsByCategory(lang: String, t: String, cat:String, start: Int, maxResults:Int, sort:String, order:String) = Action { ... Code ...}
This does not work. I must define the route like this:
GET /v1.0/products/bycategory @controllers.ProductController.getProductsByCategory(lang: String ?="en_US", t:String, cat:String, start: Int ?=0, maxresults:Int ?=100, sort:String ?="rank", order:String ?="asc")
GET /v1.0/products @controllers.ProductController.getProducts(lang: String ?="en_US", t: String, ids: String)
Is there a way to achieve this without adding "bycategory" in the path ?
Thanks
Upvotes: 1
Views: 114
Reputation: 55798
You can't use two routes with the same path an type (credits goes to Haris, who wrote that already), but you can make better usage of router, I'd use is like (pseudo code)
GET /products getAllProducts()
GET /products/:catId getProductsByCat(catId)
GET /products/:catId/:id getSingleProductWithinCat(catId, id)
So you'll have i.e. /products
> /products/toys
> /products/toys/rc-plane
Of course you can still add your optional params to it:
GET /products/toys?start=10&maxresults=250
Remember that these are GET
routes = and it's normal that normal user will try to modify it manually to fasten the search, so if /products/toys/rc-plane
won't satisfy him, he'll try first to go level up to /products/toys
again
Upvotes: 3
Reputation: 1285
One way to do this is having one method, for fetching products, e.g.
GET /v1.0/products @controllers.ProductController.getProducts(lang: String ?="en_US", t:String, cat:String ?= "", start: Int ?=0, maxresults:Int ?=100, sort:String ?="rank", order:String ?="asc")
where you set that the default category is none/null. In cases you need all products, you leave it as the default, e.g.
http://myhost/v1.0/products
and in cases you need the category
http://myhost/v1.0/products?category=hotsauces
Upvotes: 2