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-- |
6 | 1 |
object Application extends Controller { |
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 | 5 |
case _ => 0 |
6 |
} |
|
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 | 9 |
|
10 |
// GET request: read cookie data first |
|
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 | 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 | 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 | 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 | 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 | 25 |
} |
26 |
} |
|
27 |
||
28 |
||
29 |
||
30 |