Browse Source

Added the fortunes testcase to play-scala

Skamander 12 years ago
parent
commit
303e76e84e

+ 11 - 1
play-scala/app/controllers/Application.scala

@@ -5,7 +5,7 @@ import play.api.mvc._
 import play.api.libs.json.Json
 import play.api.libs.json.Json
 import java.util.concurrent._
 import java.util.concurrent._
 import scala.concurrent._
 import scala.concurrent._
-import models._
+import models.{World, Fortune}
 import utils._
 import utils._
 import scala.concurrent.Future
 import scala.concurrent.Future
 
 
@@ -58,4 +58,14 @@ object Application extends Controller {
     }
     }
   }
   }
 
 
+  def fortunes() = PredicatedAction(isDbAvailable, ServiceUnavailable) {
+    Action {
+      Async {
+        Future(Fortune.getAll())(dbEc).map { fs =>
+          val fortunes =  fs :+ Fortune(anorm.NotAssigned, "Additional fortune added at request time.")
+          Ok(views.html.fortune(fortunes))
+        }
+      }
+    }
+  }
 }
 }

+ 24 - 0
play-scala/app/models/Fortune.scala

@@ -0,0 +1,24 @@
+package models
+
+import play.api.db._
+import play.api.Play.current
+import anorm._
+import anorm.SqlParser._
+
+case class Fortune(id: Pk[Long], message: String)
+
+object Fortune {
+
+  val simpleRowParser = {
+    get[Pk[Long]]("fortune.id") ~
+    get[String]("fortune.message") map {
+      case id~message => Fortune(id, message)
+    }
+  }
+
+  def getAll(): List[Fortune] = {
+    DB.withConnection { implicit connection =>
+      SQL("SELECT * FROM Fortune").as(Fortune.simpleRowParser *)
+    }
+  }
+}

+ 1 - 1
play-scala/app/models/World.scala

@@ -37,7 +37,7 @@ object World {
    */
    */
     def findById(id: Long): World = {
     def findById(id: Long): World = {
         DB.withConnection { implicit connection =>
         DB.withConnection { implicit connection =>
-            SQL("SELECT * FROM world WHERE id = {id}").on(
+            SQL("SELECT * FROM World WHERE id = {id}").on(
                 "id" -> id
                 "id" -> id
             ).as(World.simpleRowParser.single)
             ).as(World.simpleRowParser.single)
         }
         }

+ 18 - 0
play-scala/app/views/fortune.scala.html

@@ -0,0 +1,18 @@
+@(fortunes: List[Fortune])
+
+@main("Testcase 4 :: Fortune :: FrameworkBenchmark") {
+    <table>
+        <tr>
+            <th>id</th>
+            <th>message</th>
+        </tr>
+
+        @fortunes.sortBy(_.message).map { case Fortune(id, message) =>
+            <tr>
+                <td>@id</td>
+                <td>@message</td>
+            </tr>
+        }
+
+    </table>
+}

+ 13 - 0
play-scala/app/views/main.scala.html

@@ -0,0 +1,13 @@
+@(title: String)(content: Html)
+
+<!DOCTYPE html>
+
+<html>
+<head>
+    <title>@title</title>
+    <meta charset=utf-8>
+</head>
+<body>
+    @content
+</body>
+</html>

+ 1 - 0
play-scala/benchmark_config

@@ -6,6 +6,7 @@
       "json_url": "/json",
       "json_url": "/json",
       "db_url": "/db",
       "db_url": "/db",
       "query_url": "/db?queries=",
       "query_url": "/db?queries=",
+      "fortune_url": "/fortunes",
       "port": 9000,
       "port": 9000,
       "sort": 32
       "sort": 32
     }
     }

+ 1 - 0
play-scala/conf/routes

@@ -5,6 +5,7 @@
 # Home page
 # Home page
 GET     /json                           controllers.Application.json
 GET     /json                           controllers.Application.json
 GET     /db                             controllers.Application.db(queries: Int ?= 1)
 GET     /db                             controllers.Application.db(queries: Int ?= 1)
+GET     /fortunes                       controllers.Application.fortunes
 
 
 # Map static resources from the /public folder to the /assets URL path
 # Map static resources from the /public folder to the /assets URL path
 GET     /assets/*file                   controllers.Assets.at(path="/public", file)
 GET     /assets/*file                   controllers.Assets.at(path="/public", file)