progs/app2.scala
changeset 93 82ac034dcc9d
parent 7 bd2b16f82601
child 98 3d585e603927
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/app2.scala	Sun Sep 22 15:35:50 2013 +0100
@@ -0,0 +1,30 @@
+object Application extends Controller {
+
+  def gt_cookie(c: Option[Cookie]) : Int = c.map(_.value) match {
+    case Some(s) if (s.forall(_.isDigit)) => s.toInt 
+    case _ => 0
+  }
+
+  def mk_cookie(i: Int) : Cookie = {
+    Cookie("visits", i.toString)
+  }
+  
+  // GET request: read cookie data first
+  def index = Action { request =>
+    
+    val visits_cookie = request.cookies.get("visits")
+    val visits = gt_cookie(visits_cookie)
+
+    val msg1 = "You are a valued customer who has visited this site %d times."               
+    val msg2 = "You have visited this site %d times."
+    val msg = 
+      if (visits >= 10) msg1.format(visits) else msg2.format(visits)
+    
+    //send with new cookie
+    Ok(msg).as(HTML).withCookies(mk_cookie(visits + 1))
+  }
+}
+
+
+
+