In my Play web-app I am using val resultRack = Await.result(futureList, Duration.Inf)
to get the result from a Future. Is there another better way (using best practices) to get the result from the DB? If I use onComplete
or onSuccess
my Controller finishes execution and the result is not in the val
yet. Below is my method of the Controller. Everything is working, but I need to follow more best practices in Scala.
Edited: I am already using Action.async
on other methods. But in this one I cannot use, basically because of the either.fold
. I guess I need a map
surrounding all the code of the method before to validate the json.
def addRack = Action(parse.json) { request =>
val either = request.body.validate[Rack]
either.fold(
errors => BadRequest("invalid json Rack.\n"),
rack => {
val f: Future[Option[RackRow]] = rackRepository.getById(rack.id)
val result = Await.result(f, Duration.Inf)
result match {
case Some(r) =>
// If the Rack already exists we update the produced and currentTime properties
val fGpu: Future[Seq[GpuRow]] = gpuRepository.getByRack(r.id)
// val total = fGpu.map(_.map(_.produced).sum)
val resultGpu = Await.result(fGpu, Duration.Inf)
val total = resultGpu.map(_.produced).sum
rackRepository.update(r.id, Some(total), Some(System.currentTimeMillis))
Ok("Rack already exists! Updated produced and currentTime.\n")
case None =>
// If the Rack does not exist we create it.
val rackRow = RackRow(rack.id, rack.produced, System.currentTimeMillis)
rackRepository.insert(rackRow)
Ok
}
}
)
}
New method using flatMap and map. My problem is that I am creating and filling the seq rackSeq
inside the Controller. The gpuSeq
that I am using to create this object is not evaluated because it is from the Future. How should I do to evaluate this Future gpuSeq
? On my results I only can see the rackSeq
, but the list of gpuSeq
is always empty.
Also, if the code Util.toTime(at)
throws an error I cannot catch this with recover
. As I understood by the answers I could do this....
def getRacks(at: String) = Action.async { implicit request: Request[AnyContent] =>
var rackSeq: Seq[Rack] = Seq.empty
var gpuSeq: Seq[Gpu] = Seq.empty
rackRepository.get(Util.toTime(at)).flatMap { resultRack: Seq[RackRow] =>
resultRack.map { r: RackRow =>
gpuRepository.getByRack(r.id).map { result: Seq[GpuRow] =>
result.map { gpuRow: GpuRow =>
gpuSeq = gpuSeq :+ Gpu(gpuRow.id, gpuRow.rackId, gpuRow.produced, Util.toDate(gpuRow.installedAt))
println(gpuRow)
}
}
val rack = Rack(r.id, r.produced, Util.toDate(r.currentHour), gpuSeq)
rackSeq = rackSeq :+ rack
}
// val result = Await.result(listGpu, Duration.Inf)
// result.foreach { gpuRow =>
// gpuSeq = gpuSeq :+ Gpu(gpuRow.id, gpuRow.rackId, gpuRow.produced, Util.toDate(gpuRow.installedAt))
// }
Future.successful(Ok(Json.toJson(rackSeq)).as(JSON))
}.recover {
case pe: ParseException => BadRequest(Json.toJson("Error on parse String to time."))
case e: Exception => BadRequest(Json.toJson("Error to get racks."))
case _ => BadRequest(Json.toJson("Unknow error to get racks."))
}
}