Defining the routes

We define all of the URLs of our API in the config/routes file. In this file, we define the mapping between the URL and the code. In our example, the file looks as follows:

# Product API
GET    /v1/products                         W.listProduct
POST   /v1/products/add                     W.addProduct

# Cart API
GET    /v1/cart/products                    W.listCartProducts()
DELETE /v1/cart/products/:id                W.deleteCartProduct(id)
POST   /v1/cart/products/:id/quantity/:qty  W.addCartProduct(id,qty)
PUT    /v1/cart/products/:id/quantity/:qty  W.updateCartProduct(id,qty)

For more clarity, we squeezed the controllers.WebServices package to W, to fit the page width.

For each line, if it starts with #, the line is a comment; otherwise, the first column defines the HTTP action to perform, followed by the context URL. Finally, the last column is the method to call with the parameters, if any.

At the URL level, you can use the wildcard, :, to define a variable in the path; this variable can be used in the method call. For example, the id variable is defined in the cart/products/:id path, and then used in the controllers.Cart.deleteProduct(id) method call.

We have now defined the route that Play is going to create; the next step is to define the methods defined in this routing file.

To do so, create a new file, named WebServices, in the controllers folder. In this file, the implementation is as follows:

@Singleton
class WebServices  @Inject()(cc: ControllerComponents, productDao: ProductsDao) extends AbstractController(cc) {

  // *********** CART Controler ******** //
  def listCartProducts() = play.mvc.Results.TODO

  def deleteCartProduct(id: String) = play.mvc.Results.TODO

  def addCartProduct(id: String, quantity: String) =     
play.mvc.Results.TODO def updateCartProduct(id: String, quantity: String) =
play.mvc.Results.TODO // *********** Product Controler ******** // def listProduct() = play.mvc.Results.TODO def addProduct() = play.mvc.Results.TODO }

We have defined all of the methods, but instead of coding the details of all of the implementations, we will set it to play.mvc.Results.TODO. At that point, we can try to run the test, to make sure that we do not have any compilation errors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset