Book Image

Play Framework essentials

By : Julien Richard-Foy
Book Image

Play Framework essentials

By: Julien Richard-Foy

Overview of this book

Table of Contents (14 chapters)
Play Framework Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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...