equal
deleted
inserted
replaced
|
1 package controllers |
|
2 |
|
3 import play.api.mvc._ |
|
4 |
|
5 /* |
|
6 * Application sets a cookie in plain ASCII on the |
|
7 * client's browser recording the number of visits |
|
8 * of a page. |
|
9 */ |
|
10 |
|
11 object Application extends Controller { |
|
12 |
|
13 //no or invalid cookie results in the counter being 0 |
|
14 def gt_cookie(c: Cookie) : Int = c.value match { |
|
15 case s if (s.forall(_.isDigit)) => s.toInt |
|
16 case _ => 0 |
|
17 } |
|
18 |
|
19 def mk_cookie(i: Int) : Cookie = { |
|
20 Cookie("visits", i.toString) |
|
21 } |
|
22 |
|
23 // GET request: read cookie data first |
|
24 def index = Action { request => |
|
25 |
|
26 //reads the cookie and extracts the visits counter |
|
27 val visits_cookie = request.cookies.get("visits") |
|
28 val visits = visits_cookie.map(gt_cookie).getOrElse(0) |
|
29 |
|
30 //printing a message according to value of visits counter |
|
31 val msg = |
|
32 if (visits >= 10) |
|
33 s"You are a valued customer who has visited this site $visits times." |
|
34 else s"You have visited this site $visits times." |
|
35 |
|
36 //send message with new cookie |
|
37 Ok(msg).withCookies(mk_cookie(visits + 1)) |
|
38 } |
|
39 } |
|
40 |
|
41 |
|
42 |
|
43 |