Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- case class Book(
- title: String,
- startDate: Option[DateTime],
- endDate: Option[DateTime],
- creationDate: Option[DateTime],
- updateDate: Option[DateTime],
- bookType: String,
- active: Boolean,
- editions: List[Edition])
- case class Edition(
- editionType: String,
- publisher: String,
- startDate: Option[DateTime],
- endDate: Option[DateTime],
- creationDate: Option[DateTime],
- updateDate: Option[DateTime],
- active: Boolean
- )
- object BookFormats {
- import play.api.libs.json.Json
- // Generates Writes and Reads for Feed and User thanks to Json Macros
- implicit val editionFormat = Json.format[Edition]
- implicit val bookFormat = Json.format[Book]
- }
- play.api.Application$$anon$1: Execution exception[[RuntimeException: JsError(List((/editions,List(ValidationError(error.path.missing,WrappedArray())))))]]
- @Singleton
- class Books @Inject() (uuidGenerator: UUIDGenerator) extends Controller with MongoController {
- private final val logger: Logger = LoggerFactory.getLogger(classOf[Books])
- /*
- * Get a JSONCollection (a Collection implementation that is designed to work
- * with JsObject, Reads and Writes.)
- * Note that the `collection` is not a `val`, but a `def`. We do _not_ store
- * the collection reference to avoid potential problems in development with
- * Play hot-reloading.
- */
- def collection: JSONCollection = db.collection[JSONCollection]("books")
- // ------------------------------------------ //
- // Using case classes + Json Writes and Reads //
- // ------------------------------------------ //
- import models._
- import models.BookFormats._
- import java.util.UUID
- def createBook = Action.async(parse.json) {
- request =>
- request.body.validate[Book].map {
- book =>
- // `book` is an instance of the case class `models.Book`
- collection.insert(book.copy(creationDate = Some(new DateTime()), updateDate = Some(new DateTime()))).map {
- lastError =>
- logger.debug(s"Successfully inserted with LastError: $lastError")
- Created(s"Book Created")
- }
- }.getOrElse(Future.successful(BadRequest("invalid json")))
- }
- def updateBook(title: String) = Action.async(parse.json) {
- request =>
- request.body.validate[Book].map {
- book =>
- // find our book by title
- val nameSelector = Json.obj("title" -> title)
- collection.update(nameSelector, book.copy(updateDate = Some(new DateTime()))).map {
- lastError =>
- logger.debug(s"Successfully inserted with LastError: $lastError")
- Created(s"Book Created")
- }
- }.getOrElse(Future.successful(BadRequest("invalid json")))
- }
- def findBooks = Action.async {
- // let's do our query
- val cursor: Cursor[Book] = collection.
- // find all
- find(Json.obj("active" -> true)).
- // sort them by creation date
- sort(Json.obj("created" -> -1)).
- // perform the query and get a cursor of JsObject
- cursor[Book]
- // gather all the JsObjects in a list
- val futureBooksList: Future[List[Book]] = cursor.collect[List]()
- // transform the list into a JsArray
- val futureBooksJsonArray: Future[JsArray] = futureBooksList.map { books =>
- Json.arr(books)
- }
- // everything's ok! Let's reply with the array
- futureBooksJsonArray.map {
- books =>
- Ok(books(0))
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement