Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. case class Book(
  2. title: String,
  3. startDate: Option[DateTime],
  4. endDate: Option[DateTime],
  5. creationDate: Option[DateTime],
  6. updateDate: Option[DateTime],
  7. bookType: String,
  8. active: Boolean,
  9. editions: List[Edition])
  10.  
  11. case class Edition(
  12. editionType: String,
  13. publisher: String,
  14. startDate: Option[DateTime],
  15. endDate: Option[DateTime],
  16. creationDate: Option[DateTime],
  17. updateDate: Option[DateTime],
  18. active: Boolean
  19. )
  20.  
  21. object BookFormats {
  22. import play.api.libs.json.Json
  23.  
  24. // Generates Writes and Reads for Feed and User thanks to Json Macros
  25. implicit val editionFormat = Json.format[Edition]
  26. implicit val bookFormat = Json.format[Book]
  27.  
  28. }
  29.  
  30. play.api.Application$$anon$1: Execution exception[[RuntimeException: JsError(List((/editions,List(ValidationError(error.path.missing,WrappedArray())))))]]
  31.  
  32. @Singleton
  33. class Books @Inject() (uuidGenerator: UUIDGenerator) extends Controller with MongoController {
  34.  
  35. private final val logger: Logger = LoggerFactory.getLogger(classOf[Books])
  36.  
  37. /*
  38. * Get a JSONCollection (a Collection implementation that is designed to work
  39. * with JsObject, Reads and Writes.)
  40. * Note that the `collection` is not a `val`, but a `def`. We do _not_ store
  41. * the collection reference to avoid potential problems in development with
  42. * Play hot-reloading.
  43. */
  44. def collection: JSONCollection = db.collection[JSONCollection]("books")
  45.  
  46. // ------------------------------------------ //
  47. // Using case classes + Json Writes and Reads //
  48. // ------------------------------------------ //
  49.  
  50. import models._
  51. import models.BookFormats._
  52. import java.util.UUID
  53.  
  54. def createBook = Action.async(parse.json) {
  55. request =>
  56.  
  57. request.body.validate[Book].map {
  58. book =>
  59. // `book` is an instance of the case class `models.Book`
  60. collection.insert(book.copy(creationDate = Some(new DateTime()), updateDate = Some(new DateTime()))).map {
  61. lastError =>
  62. logger.debug(s"Successfully inserted with LastError: $lastError")
  63. Created(s"Book Created")
  64. }
  65. }.getOrElse(Future.successful(BadRequest("invalid json")))
  66. }
  67.  
  68. def updateBook(title: String) = Action.async(parse.json) {
  69. request =>
  70. request.body.validate[Book].map {
  71. book =>
  72. // find our book by title
  73. val nameSelector = Json.obj("title" -> title)
  74. collection.update(nameSelector, book.copy(updateDate = Some(new DateTime()))).map {
  75. lastError =>
  76. logger.debug(s"Successfully inserted with LastError: $lastError")
  77. Created(s"Book Created")
  78. }
  79. }.getOrElse(Future.successful(BadRequest("invalid json")))
  80. }
  81.  
  82. def findBooks = Action.async {
  83. // let's do our query
  84. val cursor: Cursor[Book] = collection.
  85. // find all
  86. find(Json.obj("active" -> true)).
  87. // sort them by creation date
  88. sort(Json.obj("created" -> -1)).
  89. // perform the query and get a cursor of JsObject
  90. cursor[Book]
  91.  
  92. // gather all the JsObjects in a list
  93. val futureBooksList: Future[List[Book]] = cursor.collect[List]()
  94.  
  95. // transform the list into a JsArray
  96. val futureBooksJsonArray: Future[JsArray] = futureBooksList.map { books =>
  97. Json.arr(books)
  98. }
  99. // everything's ok! Let's reply with the array
  100. futureBooksJsonArray.map {
  101. books =>
  102. Ok(books(0))
  103. }
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement