Chapter 2. Defining Actions

If you're reading this, you've either survived the first chapter or skipped it. Either way, I am assuming you know the structure of a simple Play application. A controller in Play generates Action values and, to do so, it uses several objects and methods internally. In this chapter, we will see what goes on behind the scenes and how we can leverage these actions when we build our application.

In this chapter, we will be covering the following topics:

  • Defining Actions
  • Request body parsers
  • Action composition and troubleshooting

A dummy Artist model

In the following sections, we will give make reference to an artist model. It is a simple class with a companion object, defined as follows:

case class Artist(name: String, country: String)

object Artist {
  val availableArtist = Seq(Artist("Wolfgang Amadeus Mozart", "Austria"), 
    Artist("Ludwig van Beethoven", "Germany"), 
    Artist("Johann Sebastian Bach", "Germany"), 
    Artist("Frédéric François Chopin", "Poland"), 
    Artist("Joseph Haydn", "Austria"), 
    Artist("Antonio Lucio Vivaldi", "Italy"), 
    Artist("Franz Peter Schubert", "Austria"), 
    Artist("Franz Liszt", "Austria"), 
    Artist("Giuseppe Fortunino Francesco Verdi", "Austria")) 

  def fetch: Seq[Artist] = {
    availableArtist 
  } 

  def fetchByName(name: String): Seq[Artist] = {
    availableArtist.filter(a => a.name.contains(name)) 
  } 

  def fetchByCountry(country: String): Seq[Artist] = {
    availableArtist.filter(a => a.country == country) 
  } 

  def fetchByNameOrCountry(name: String, country: String): Seq[Artist] = { 
    availableArtist.filter(a => a.name.contains(name) || a.country == country) 
  } 

  def fetchByNameAndCountry(name: String, country: String): Seq[Artist] = {
    availableArtist.filter(a => a.name.contains(name) && a.country == country)
  } 
} 

The Artist model has a method to fetch all these artists and a few methods to filter the artist, based on different parameters.

Note

In real applications, the model interacts with the database but to keep things simple, we have hardcoded the data as Seq[Artist].

We also have a view of home.scala.html, which displays information about the artist in a table:

@(artists: Seq[Artist])
<!DOCTYPE html> 

<html>
    <head>
        <title>Action App</title>
    </head>
    <body>
        <table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Country</th>
                    </tr>
            </thead>
            <tbody>
            @artists.map { artist => 
                <tr>
                    <td>@artist.name</td>
                    <td>@artist.country</td>
                </tr>
            } 
            </tbody>
        </table>
    </body>
</html>

This is a twirl, template which requires a Seq[Artist]. It is similar to the view of the TaskTracker application we built in the previous chapter.

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

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