progs/app2.scala
changeset 98 3d585e603927
parent 93 82ac034dcc9d
--- a/progs/app2.scala	Mon Sep 23 17:39:31 2013 +0100
+++ b/progs/app2.scala	Mon Sep 23 22:23:55 2013 +0100
@@ -1,27 +1,27 @@
 object Application extends Controller {
 
-  def gt_cookie(c: Option[Cookie]) : Int = c.map(_.value) match {
-    case Some(s) if (s.forall(_.isDigit)) => s.toInt 
+  def gt_cookie(c: Cookie) : Int = c.value match {
+    case s if (s.forall(_.isDigit)) => s.toInt 
     case _ => 0
   }
 
-  def mk_cookie(i: Int) : Cookie = {
-    Cookie("visits", i.toString)
-  }
+  def mk_cookie(i: Int) : Cookie = Cookie("visits", i.toString)
   
   // GET request: read cookie data first
   def index = Action { request =>
-    
+ 
+    //reads the cookie and extracts the visits counter   
     val visits_cookie = request.cookies.get("visits")
-    val visits = gt_cookie(visits_cookie)
+    val visits = visits_cookie.map(gt_cookie).getOrElse(0)
 
-    val msg1 = "You are a valued customer who has visited this site %d times."               
-    val msg2 = "You have visited this site %d times."
+    //printing a message according to value of visits counter
     val msg = 
-      if (visits >= 10) msg1.format(visits) else msg2.format(visits)
+      if (visits >= 10)
+        s"You are a valued customer who has visited this site $visits times."               
+      else s"You have visited this site $visits times."
     
-    //send with new cookie
-    Ok(msg).as(HTML).withCookies(mk_cookie(visits + 1))
+    //send message with new cookie
+    Ok(msg).withCookies(mk_cookie(visits + 1))
   }
 }