Application.scala 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package controllers
  2. import javax.inject.{Inject, Singleton}
  3. import play.api.mvc._
  4. import play.api.libs.json.Json
  5. import java.util.concurrent._
  6. import models.{WorldDAO, FortunesDAO, World, Fortune}
  7. import utils.DbOperation
  8. import scala.concurrent.Future
  9. import play.api.libs.concurrent.Execution.Implicits._
  10. @Singleton()
  11. class Application @Inject() (fortunesDAO: FortunesDAO, worldDAO: WorldDAO, dbOperation: DbOperation) extends Controller {
  12. // Anorm code
  13. def getRandomWorlds(n: Int): Future[Seq[World]] = dbOperation.asyncDbOp { implicit connection =>
  14. for (_ <- 1 to n) yield {
  15. worldDAO.findById(getNextRandom)
  16. }
  17. }
  18. def getFortunes: Future[Seq[Fortune]] = dbOperation.asyncDbOp { implicit connection =>
  19. fortunesDAO.getAll
  20. }
  21. def updateWorlds(n: Int): Future[Seq[World]] = dbOperation.asyncDbOp { implicit connection =>
  22. for(_ <- 1 to n) yield {
  23. val world = worldDAO.findById(getNextRandom)
  24. val updatedWorld = world.copy(randomNumber = getNextRandom)
  25. worldDAO.updateRandom(updatedWorld)
  26. updatedWorld
  27. }
  28. }
  29. def getNextRandom: Int = {
  30. ThreadLocalRandom.current().nextInt(TestDatabaseRows) + 1
  31. }
  32. // Test seems picky about headers. Doesn't like character set being there for JSON. Always wants Server header set.
  33. // There is a Filter which adds the Server header for all types. Below I set Content-Type as needed to get rid of
  34. // warnings.
  35. // Easy ones
  36. case class HelloWorld(message: String)
  37. def getJsonMessage = Action {
  38. val helloWorld = HelloWorld(message = "Hello, World!")
  39. Ok(Json.toJson(helloWorld)(Json.writes[HelloWorld])).withHeaders(CONTENT_TYPE -> "application/json")
  40. }
  41. val plaintext = Action {
  42. // default headers are correct according to docs: charset included.
  43. // BUT the test harness has a WARN state and says we don't need it.
  44. Ok("Hello, World!").withHeaders(CONTENT_TYPE -> "text/plain")
  45. }
  46. // Common code between Scala database code
  47. protected val TestDatabaseRows = 10000
  48. import models.WorldJsonHelpers.toJson
  49. def db = Action.async {
  50. getRandomWorlds(1).map { worlds =>
  51. Ok(Json.toJson(worlds.head)).withHeaders(CONTENT_TYPE -> "application/json")
  52. }
  53. }
  54. def queries(countString: String) = Action.async {
  55. val n = parseCount(countString)
  56. getRandomWorlds(n).map { worlds =>
  57. Ok(Json.toJson(worlds)).withHeaders(CONTENT_TYPE -> "application/json")
  58. }
  59. }
  60. def fortunes() = Action.async {
  61. getFortunes.map { dbFortunes =>
  62. val appendedFortunes = Fortune(0, "Additional fortune added at request time.") :: dbFortunes.to[List]
  63. Ok(views.html.fortune(appendedFortunes)).withHeaders(CONTENT_TYPE -> "text/html")
  64. }
  65. }
  66. def update(queries: String) = Action.async {
  67. val n = parseCount(queries)
  68. updateWorlds(n).map { worlds =>
  69. Ok(Json.toJson(worlds)).withHeaders(CONTENT_TYPE -> "application/json")
  70. }
  71. }
  72. private def parseCount(s: String): Int = {
  73. try {
  74. val parsed = java.lang.Integer.parseInt(s, 10)
  75. parsed match {
  76. case i if i < 1 => 1
  77. case i if i > 500 => 500
  78. case i => i
  79. }
  80. } catch {
  81. case _: NumberFormatException => 1
  82. }
  83. }
  84. }