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() => Redirect(routes.Items.details(item.id)) case Accepts.Json() => Ok(Json.toJson(item)) } case None => InternalServerError...