Putting things together

You now have all the required knowledge to define HTTP endpoints for the list, create, and get entry points of the Shop service so that your HTTP endpoints can read both HTML form data and JSON data, and can return either HTML or JSON content according to the client's preferences.

For instance, here is the code for how the create endpoint can look like:

 val create = Action { implicit request => createItemFormModel.bindFromRequest().fold( formWithErrors => render { case Accepts.Html() => BadRequest(views.html.createForm(formWithErrors)) case Accepts.Json() => BadRequest(formWithErrors.errorsAsJson) }, createItem => { shop.create(createItem.name, createItem.price) match { case Some(item) => render { case Accepts.Html() => ...

Get Play Framework Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.