Environment: Play! 2.2.3, ReactiveMongo 0.10.0-SNAPSHOT
Suppose I have a page with a list of documents (let's say "projects") and a button that pops up a modal dialogue with fields to be filled in. Upon pressing the OK button the page sends a request with JSON body to the backend:
{
name: "Awesome Project",
url: "https://github.com/ab/cd",
repository: "git@github.com/ab/cd.git",
script: "empty"
}
The backend routes the request to the Action
defined like this:
def projectsCollection: JSONCollection = db.collection[JSONCollection]("projects")
def create = Action.async(parse.json) { request =>
projectsCollection.insert(request.body) map {
case LastError(true,_,_,_,Some(doc),_,_) => Created(JsObject(List(
"result" -> JsString("OK") ,
"doc" -> BSONFormats.toJSON(doc)
)))
case LastError(false, err, code, msg, _, _, _) => NotAcceptable(JsObject(List(
"result" -> JsString("ERROR"),
"error" -> JsString(err.getOrElse("unknown")),
"code" -> JsNumber(code.getOrElse[Int](0)),
"msg" -> JsString(msg.getOrElse("no messsage"))
)))
}
}
The LastError
case class has a parameter originalDocument: Option[BSONDocument]
which is returned in the request response body, but it isn't the document I expected. I want the document with the BSONObjectID
filled or at least the _id
itself.
Trying to retrieve the freshly created document led me into a dead end, because everything is wrapped into Future
.
How to write elegant code that does the task?