I have a Play controller that takes in a Model object that comes in from the user interface. This model object is a User that I'm trying to insert into the database. But before I insert this User in the table, I check if there is a duplicate user that already exist for the given EMail address. If yes, I reject the request and if not I insert. I'm using Slick and Play framework and here is my controller code:
def registerNewUser(user: User) = {
dbService.registerNewUser(User.toUserRow(user))
.map(userID => Ok(Json.obj("status" -> "ok", "userId" -> userID)))
.recover { case ex => InternalServerError(Json.obj("error" -> s"${ex.getMessage}")) }
}
def createUser() = Action.async(parse.tolerantJson) { request =>
request.body.validate[User].fold(
errors => Future.successful {
BadRequest(Json.obj("status" -> "error", "message" -> JsError.toJson(errors)))
},
user => {
val userExists: Future[Boolean] = dbService.userExists(user.email)
userExists.map(value => {
if (value) UnprocessableEntity(Json.obj("status" -> "error", "message" -> s"user with email ${user.email.toString} already exists"))
else registerNewUser(user)
}).recover {
case ex => InternalServerError(Json.obj("error" -> s"${ex.getMessage}"))
}
} // compilation failure on this line [[ Line X ]]
)
}
I do not see anything wrong with this approach, but my compiler is not happy. It complaints at Line X as:
Expression of type Future[Object] does not confirm to the expected type _X
What is actually the problem here?