Attic/programs/Application2.scala
author cu
Sat, 28 Oct 2017 00:05:02 +0100
changeset 560 85521d542da4
parent 198 2ce98ee39990
permissions -rw-r--r--
updated cw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
package controllers
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import play.api._
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
import play.api.mvc._
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import play.api.data._
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
import play.api.data.Forms._
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
/*
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
 * Application sets a cookie in plain ASCII on the
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
 * clients browser recording the visits of a page.
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
 */
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
object Application extends Controller {
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  //no or invalid cookie results in the counter being 0
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  def gt_cookie(c: Option[Cookie]) : Int = c.map(_.value) match {
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
    case Some(s) if (s.forall(_.isDigit)) => s.toInt 
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
    case _ => 0
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  }
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  def mk_cookie(i: Int) : Cookie = {
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
    Cookie("visits", i.toString)
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  }
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  // GET request: read cookie data first
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  def index = Action { request =>
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
 
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
    //reads the cookie and extracts the visits counter   
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
    val visits_cookie = request.cookies.get("visits")
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
    val visits = gt_cookie(visits_cookie)
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
    //printing a message according to value of visits counter
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
    val msg1 = "You are a valued customer who has visited this site %d times."               
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
    val msg2 = "You have visited this site %d times."
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
    val msg = 
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
      if (visits >= 10) msg1.format(visits) else msg2.format(visits)
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
    
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
    //send message with new cookie
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
    Ok(msg).as(HTML).withCookies(mk_cookie(visits + 1))
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  }
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
}
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45