2017-04-14 7 views
0

私はユーザー登録モジュールを作成しています。投稿時(JSONを使用)、JSONが正しく解析されているかどうかを確認したい。 JSONに問題がある場合は、エラーを返します。 JSONが正しい場合は、ユーザーが既に存在するかどうかを確認したい(firstnameを参照)。データはMongoDBにあります。 ReactiveMongoPlugin 0.10を使用しています。私はFuture [Option [BSONDocument]]を返す 'one'メソッドを使用します。アクションが完了する前に、この未来が終了するのをどのように待つのですか?今後の対応方法[再生時のオプション[BSONDocument]アクション

アプローチ1 - アクションを使用し、自分自身の結果を処理しようとします。コードはコンパイルして、私は今、結果を待って、[OK]を(Json.toJson(ACK))またはBadRequest(Json.toJson(どちらかを返すようにしたいどのように未来

def registrationRequest = Action(parse.json) { request => { 
    Logger.debug("received message:" + request) 
    Logger.debug("received message:" + request.body) 
    val jr:JsResult[User2] = request.body.validate[User2] 
    Logger.debug("jr is "+jr) 

    jr match { 
     case s:JsSuccess[User2] => { 

     val user = s.get 
     Logger.debug("opening database connection") 

     val driver = new MongoDriver() 
     val connection = driver.connection(List("localhost")) 
     val db = connection.db("website-db") 
     val collection = db.collection[BSONCollection]("users") 

     // the data from client is a JSON of type {user:{firstname:"name"}}. I have created code to parse the JSON 
     val query = BSONDocument("user"-> BSONDocument("firstname"->user.firstname)) 
     Logger.debug("query is:"+query) 


     val result = collection.find(query).one 

を終了するのを待つことを知らないしませんack))。それ、どうやったら出来るの?私は次のコードを書いたが、私は2つのポイントで立ち往生している。(a)コードは未来が完了するのを待つだろう。(b)onCompleteはユニットを返すが、Playのアクションはplay.api.mvc.Resultを必要とする。それ、どうやったら出来るの?

//I guess data would be Success or Failure 
result onComplete (data => 
     data match { 
      //If Success, value would be Some or None 
      case Success(value) => { 
      value match { 
       case None => { //no record. Can add 
       Logger.debug("No record from mongo: Can add") 
       val ack = Acknowledgment (1, "Welcome " + user.firstName + " " + user.lastName) 
       Logger.debug ("success ack Json:" + Json.toJson (ack)) 
       Ok (Json.toJson (ack)) 

       } 
       case Some(x) => { //duplicae record 
       Logger.debug("error from Mongo. Duplicate:"+x) 
       val ack = Acknowledgment(0,"duplicate: "+x.toString()) 
       Logger.debug("fail ack:"+Json.toJson(ack)) 
       BadRequest(Json.toJson(ack)) 
       } 

      } 
      } 
      case Failure (e)=> { 
      Logger.debug("error from Mongo."+e) 
      val ack = Acknowledgment(0,"MongoDB Error: "+e.toString()) 
      Logger.debug("fail ack:"+Json.toJson(ack)) 
      BadRequest(Json.toJson(ack)) 
      } 
     }) //onComplete returns Unit. Action needs play.api.mvc.Result 
    case f:JsError => { 
    Logger.debug("error: "+JsError.toFlatJson(f)) 
    val ack = Acknowledgment(0,JsError.toFlatJson(f).toString()) 
    Logger.debug("fail ack:"+Json.toJson(ack)) 
    BadRequest(Json.toJson(ack)) 
    } 
} 

}

アプローチ2 - 私はAction.asyncを使用する必要があることを読んで、私は一緒に作品をフィットすることができません。それは期待するので、私は続いて第2のアプローチはAction.Asyncを使用することであったが、コードはコンパイルdidntの[SimpleResult]

def registrationRequest = Action.async(parse.json) { request => { 
    Logger.debug("received message:" + request) 
    Logger.debug("received message:" + request.body) 
    val jr:JsResult[User2] = request.body.validate[User2] 
    Logger.debug("jr is "+jr) 

    jr match { 
     case s:JsSuccess[User2] => { 

     val user = s.get 
     Logger.debug("opening database connection") 
     val driver = new MongoDriver() 
     val connection = driver.connection(List("localhost")) 
     val db = connection.db("website-db") 
     val collection = db.collection[BSONCollection]("users") 

     val query = BSONDocument("user"-> BSONDocument("firstname"->user.firstName)) 
     Logger.debug("query is:"+query) 

     //result is of type Future[Option[BSONDocument]] 
     val result = collection.find(query).one 

     result.map(option => option match { 
      case None => { 
      //no record. Can add 
      Logger.debug("No record from mongo: Can add") 
      val ack = Acknowledgment(1, "Welcome " + user.firstName + " " + user.lastName) 
      Logger.debug("success ack Json:" + Json.toJson(ack)) 
      Ok(Json.toJson(ack)) 
      } 
      case Some(x) => { 
      //duplicae record 
      Logger.debug("error from Mongo. Duplicate:" + x) 
      val ack = Acknowledgment(0, "duplicate: " + x.toString()) 
      Logger.debug("fail ack:" + Json.toJson(ack)) 
      BadRequest(Json.toJson(ack)) 
      } 
     } 
     ) 
     } 
     case f:JsError => { 
     Logger.debug("error: "+JsError.toFlatJson(f)) 
     val ack = Acknowledgment(0,JsError.toFlatJson(f).toString()) 
     Logger.debug("fail ack:"+Json.toJson(ack)) 
     BadRequest(Json.toJson(ack)) //Action.async expect scala.concurrent.Future[play.api.mvc.SimpleResult] 
    } 
} 


    } 

答えて

1

未来溶液が将来[SimpleResult]を返すAction.asyncを使用することです。コード内では、FutureのMapMapとFlatMapを使用してFuture [SimpleResult]を返します。

+0

テスト(テストでは除く)を待つのではなく、 – cchantep

関連する問題