app2.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 22 Oct 2012 12:55:01 +0100
changeset 51 7762c11f37d5
parent 7 bd2b16f82601
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
object Application extends Controller {
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
7
bd2b16f82601 new slides
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
     3
  def gt_cookie(c: Option[Cookie]) : Int = c.map(_.value) match {
bd2b16f82601 new slides
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
     4
    case Some(s) if (s.forall(_.isDigit)) => s.toInt 
6
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
    case _ => 0
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
  }
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  def mk_cookie(i: Int) : Cookie = {
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
    Cookie("visits", i.toString)
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  }
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  // GET request: read cookie data first
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  def index = Action { request =>
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
    
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
    val visits_cookie = request.cookies.get("visits")
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
    val visits = gt_cookie(visits_cookie)
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
    val msg1 = "You are a valued customer who has visited this site %d times."               
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
    val msg2 = "You have visited this site %d times."
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
    val msg = 
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
      if (visits >= 10) msg1.format(visits) else msg2.format(visits)
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
    
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
    //send with new cookie
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
    Ok(msg).as(HTML).withCookies(mk_cookie(visits + 1))
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  }
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
}
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30