app2.scala
changeset 93 82ac034dcc9d
parent 92 af1aeec504cb
child 94 caf08b02fa32
equal deleted inserted replaced
92:af1aeec504cb 93:82ac034dcc9d
     1 object Application extends Controller {
       
     2 
       
     3   def gt_cookie(c: Option[Cookie]) : Int = c.map(_.value) match {
       
     4     case Some(s) if (s.forall(_.isDigit)) => s.toInt 
       
     5     case _ => 0
       
     6   }
       
     7 
       
     8   def mk_cookie(i: Int) : Cookie = {
       
     9     Cookie("visits", i.toString)
       
    10   }
       
    11   
       
    12   // GET request: read cookie data first
       
    13   def index = Action { request =>
       
    14     
       
    15     val visits_cookie = request.cookies.get("visits")
       
    16     val visits = gt_cookie(visits_cookie)
       
    17 
       
    18     val msg1 = "You are a valued customer who has visited this site %d times."               
       
    19     val msg2 = "You have visited this site %d times."
       
    20     val msg = 
       
    21       if (visits >= 10) msg1.format(visits) else msg2.format(visits)
       
    22     
       
    23     //send with new cookie
       
    24     Ok(msg).as(HTML).withCookies(mk_cookie(visits + 1))
       
    25   }
       
    26 }
       
    27 
       
    28 
       
    29 
       
    30