Attic/scala/app2.scala
changeset 198 2ce98ee39990
parent 197 9c968d0de9a0
equal deleted inserted replaced
197:9c968d0de9a0 198:2ce98ee39990
       
     1 object Application extends Controller {
       
     2 
       
     3   def gt_cookie(c: Cookie) : Int = c.value match {
       
     4     case s if (s.forall(_.isDigit)) => s.toInt 
       
     5     case _ => 0
       
     6   }
       
     7 
       
     8   def mk_cookie(i: Int) : Cookie = Cookie("visits", i.toString)
       
     9   
       
    10   // GET request: read cookie data first
       
    11   def index = Action { request =>
       
    12  
       
    13     //reads the cookie and extracts the visits counter   
       
    14     val visits_cookie = request.cookies.get("visits")
       
    15     val visits = visits_cookie.map(gt_cookie).getOrElse(0)
       
    16 
       
    17     //printing a message according to value of visits counter
       
    18     val msg = 
       
    19       if (visits >= 10)
       
    20         s"You are a valued customer who has visited this site $visits times."               
       
    21       else s"You have visited this site $visits times."
       
    22     
       
    23     //send message with new cookie
       
    24     Ok(msg).withCookies(mk_cookie(visits + 1))
       
    25   }
       
    26 }
       
    27 
       
    28 
       
    29 
       
    30