I am new to Scala, Slick and Play but I am trying to do some little service using this technology. I have a problem with a proper way how to check existence of item in DB.
Play action -- simple to see output in Browser:
val id = 5
val name = "xx"
def show(): Action async {
dao.isExist(id,name).map(c => Ok(c.toString)
}
Dao
User = TableQuery[UserRow]
def isExist(id:Int, name:String) = {
val res = db.run(User.filter(i => (i.id === id || i.name === name)).result)}
// I would like to do something like
if (res.length > 0) true else false
// or since action is async to return future.
res match {
case 0 => Future(true)
case _ => Future(false)
}
// but this doesnt compile. I came up with
val trueRes = Await.result(res, Duratin.Inf)
// which in not async Action do what I want.
I think that I should avoid using Await, but in this case I need to make some action based on what DB will return. Could you advice what would be the best pattern to address this case?