Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Building Applications with Scala
Source

Creating our models

For our product model, we have a simple Scala case class in Product.scala as follows:

package models case class Product ( var id:Option[Long], var name:String, var details:String, var price:BigDecimal )

{

override def toString:String =

{

"Product { id: " + id.getOrElse(0) + ",name: " + name + ", details: "+ details + ", price: " + price + "}"

}

}

A product can have an optional ID, a name, details, and a price. We also override the toString method just for the sake of simplicity for logging. We also need to define models for image and review. The following is the review model from Review.scala:

package models

case class Review (var id:Option[Long],

var productId:Option[Long], var author:String, var comment:String)

{

override def toString:String = {

"Review { id: " + id + " ,productId: " +

productId.getOrElse(0) + ",author: " + author + ",comment:

" + comment + " }"

}

}

For a review model, we have an optional ID, an optional productId, one author, and a comment. Validations will be done on the views. Now let's go for the image model. The image model can be found in Image.scala as follows:

package models

case class Image (var id:Option[Long],

var productId:Option[Long], var url:String){

override def toString:String = {

"Image { productId: " + productId.getOrElse(0) + ",url: "

+ url + "}"

}

}

For an image model, we have an optional ID, an optional productId, and the image URL.

The Play framework does the routing, and we need to define the routes at ReactiveWebStore/conf/routes. Keep in mind that the Play framework will validate all the routes, so you need to specify valid packages and classes. Play also creates something called reverse controller, which we will use later in the chapter. For now, let's define the routes. Reverse controller is generated by the Play framework with an action method which is the same as that of the original controller with the same signature, but it returns play.api.mvc.Call instead of play.api.mvc.Action.

 
Source
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Mathematics
Political science
Philosophy
Psychology
Religion
Sociology
Travel