Attic/scala/app2.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 06 Nov 2017 14:22:04 +0000
changeset 562 69580b23c480
parent 198 2ce98ee39990
permissions -rw-r--r--
updated
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
98
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     3
  def gt_cookie(c: Cookie) : Int = c.value match {
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     4
    case 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
98
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     8
  def mk_cookie(i: Int) : Cookie = Cookie("visits", i.toString)
6
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  // GET request: read cookie data first
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  def index = Action { request =>
98
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    12
 
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    13
    //reads the cookie and extracts the visits counter   
6
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
    val visits_cookie = request.cookies.get("visits")
98
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    15
    val visits = visits_cookie.map(gt_cookie).getOrElse(0)
6
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
98
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    17
    //printing a message according to value of visits counter
6
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
    val msg = 
98
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    19
      if (visits >= 10)
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    20
        s"You are a valued customer who has visited this site $visits times."               
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    21
      else s"You have visited this site $visits times."
6
4c40d4b2d01c new slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
    
98
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    23
    //send message with new cookie
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    24
    Ok(msg).withCookies(mk_cookie(visits + 1))
6
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