Application.scala 3.1 KB

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