Cart endpoints

Now, let's document the cart section with the list of products in the cart:

  @ApiOperation(value = "List the product in the cart", consumes = 
"text/plain") @ApiResponses(Array(new ApiResponse(code = 200, message = "Product
added"), new ApiResponse(code = 401, message = "unauthorized, please login
before to proceed"), new ApiResponse(code = 500, message = "Internal server error,
database error"))) def listCartProducts() = Action.async { request =>

If we call listCartProducts, we receive an empty array. To test it with some products, complete the declaration of addCartProduct with the following:

  @ApiOperation(value = "Add a product in the cart", consumes = 
"text/plain") @ApiResponses(Array(new ApiResponse(code = 200, message = "Product
added in the cart"),
new ApiResponse(code = 400, message = "Cannot insert duplicates in
the database"), new ApiResponse(code = 401, message = "unauthorized, please login
before to proceed"), new ApiResponse(code = 500, message = "Internal server error,
database error"))) def addCartProduct( @ApiParam(name = "id", value = "The product code", required =
true) id: String, @ApiParam(name = "quantity", value= "The quantity to add",
required = true) quantity: String) = Action.async { request
=>

In Swagger, we can now add a new product to the cart, as follows:

Then, the list of products will return the following:

After that, we can try to update a product. Add the following annotations to updateCartProduct:

  @ApiOperation(value = "Update a product quantity in the cart", 
consumes = "text/plain") @ApiResponses(Array(new ApiResponse(code = 200, message = "Product
added in the cart"), new ApiResponse(code = 401, message = "unauthorized, please login
before to proceed"), new ApiResponse(code = 500, message = "Internal server error,
database error"))) def updateCartProduct(@ApiParam(name = "id", value = "The product
code", required = true, example = "ALD1") id: String,
@ApiParam(name = "quantity", value= "The quantity to update",
required = true) quantity: String) = Action.async { request =>

Then, use Swagger to update the quantity, as follows:

After the update, the list of products returns, as follows:

Perfect; the last operation to document is deleteCartProduct:

  @ApiOperation(value = "Delete a product from the cart", consumes = 
"text/plain") @ApiResponses(Array(new ApiResponse(code = 200, message = "Product
delete from the cart"), new ApiResponse(code = 401, message = "unauthorized, please login
before to proceed"), new ApiResponse(code = 500, message = "Internal server error,
database error"))) def deleteCartProduct(@ApiParam(name = "id", value = "The product
code", required = true) id: String) = Action.async { request =>

We now have complete Swagger documentation for our API, and users can test it directly from their browsers.

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

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