Browse Source

Made it more idiomatic

Nilanjan Raychaudhuri 12 years ago
parent
commit
7355ec154b

+ 9 - 6
play-slick/app/controllers/Application.scala

@@ -29,6 +29,9 @@ object Application extends Controller {
     new NamedThreadFactory("dbEc"))
   private val dbEc = ExecutionContext.fromExecutorService(tpe)
 
+  private val worldsTable = new Worlds
+  private val fortunesTable = new Fortunes
+
   // A predicate for checking our ability to service database requests is determined by ensuring that the request
   // queue doesn't fill up beyond a certain threshold. For convenience we use the max number of connections * the max
   // # of db requests per web request to determine this threshold. It is a rough check as we don't know how many
@@ -41,12 +44,12 @@ object Application extends Controller {
       Async {
         val random = ThreadLocalRandom.current()
 
-        val worlds = Future.sequence((for {
+        val _worlds = Future.sequence((for {
           _ <- 1 to queries
-        } yield Future(Worlds.findById(random.nextInt(TestDatabaseRows) + 1))(dbEc)
+        } yield Future(worldsTable.findById(random.nextInt(TestDatabaseRows) + 1))(dbEc)
           ).toList)
 
-        worlds map {
+        _worlds map {
           w => Ok(Json.toJson(w))
         }
       }
@@ -56,7 +59,7 @@ object Application extends Controller {
   def fortunes() = PredicatedAction(isDbAvailable, ServiceUnavailable) {
     Action {
       Async {
-        Future(Fortunes.getAll())(dbEc).map { fs =>
+        Future(fortunesTable.getAll())(dbEc).map { fs =>
           val fortunes =  Fortune(-1, "Additional fortune added at request time.") +: fs
           Ok(views.html.fortune(fortunes))
         }
@@ -79,10 +82,10 @@ object Application extends Controller {
           _ <- 1 to boundsCheckedQueries
         } yield Future {
             for {
-              world <- Worlds.findById(random.nextInt(TestDatabaseRows) + 1) 
+              world <- worldsTable.findById(random.nextInt(TestDatabaseRows) + 1) 
             } yield {
               val updatedWorld = world.copy(randomNumber = random.nextInt(TestDatabaseRows) + 1)
-              Worlds.updateRandom(updatedWorld)
+              worldsTable.updateRandom(updatedWorld)
               updatedWorld
             }
           }(dbEc)

+ 2 - 3
play-slick/app/models/Fortune.scala

@@ -5,7 +5,7 @@ import play.api.db.slick.Config.driver.simple._
 import play.api.db.slick.DB
 
 
-object Fortunes extends Table[Fortune]("Fortune") {
+class Fortunes extends Table[Fortune]("Fortune") {
   def id = column[Long]("id", O.PrimaryKey)
   def message = column[String]("message")
   def * = id ~ message <> (Fortune.apply _, Fortune.unapply _)
@@ -13,8 +13,7 @@ object Fortunes extends Table[Fortune]("Fortune") {
   val byId = createFinderBy(_.id)
 
   def getAll(): List[Fortune] = DB.withSession { implicit session =>
-    val q = for(f <- Fortunes) yield f
-    q.list
+    Query(this).list
   }
 }
 

+ 3 - 3
play-slick/app/models/World.scala

@@ -6,7 +6,7 @@ import play.api.db.slick.DB
 import play.api.libs.json._
 import play.api.libs.functional.syntax._
 
-object Worlds extends Table[World]("World") {
+class Worlds extends Table[World]("World") {
   def id = column[Int]("id", O.PrimaryKey)
   def randomNumber = column[Long]("randomNumber")
   def * = id ~ randomNumber <> (World.apply _, World.unapply _)
@@ -14,12 +14,12 @@ object Worlds extends Table[World]("World") {
   val byId = createFinderBy(_.id)
 
   def findById(id: Int): Option[World] = DB.withSession { implicit session =>
-      Worlds.byId(id).firstOption
+      byId(id).firstOption
   }
 
   def updateRandom(world: World) {
     DB.withSession { implicit session =>
-      Worlds.where(_.id === world.id).update(world)
+      this.where(_.id === world.id.bind).update(world)
     }
   }
 

+ 1 - 1
play-slick/project/build.properties

@@ -1 +1 @@
-sbt.version=0.12.2
+sbt.version=0.12.2