Priya R
Priya R

Reputation: 451

How to achieve double asynchronous return from Play Controller?

I am trying to write an async REST method as below.

def createUser = Action.async(parse.json) { request =>
    val b = request.body.validate[User]
    b.fold(
      errors => {
        Future {
          BadRequest(Json.obj("status" -> "OK", "message" -> JsError.toJson(errors)))
        }
      },
      user => {
        // I am stuck here! Looking the below you would
        // understand what I need to do
        // How to achieve this?
        Future {
          UserServiceImpl.createUser(user) onComplete  {
            case user => Ok
            case _ => InternalServerError
          }
      }
    )
  }

The problem is the Slick dbConfig.db.run method is also async. So the UserServiceImpl.createUser has to be async.

Now if the UserServiceImpl.createUser throws an exception I need to return InternalServerError. And if it is successful I want to return Ok.

How can I do this?

Below is my model:

object User {
  case class User(id: Long,
                  firstName: String,
                  lastName: String,
                  mobile: Long,
                  email: String)

  implicit val userWrites = Json.writes[User]
  implicit val userReads = Json.reads[User]
}

class UserTable(tag: Tag)
  extends Table[User.User](tag, "user") {

  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def firstName = column[String]("first_name")
  def lastName = column[String]("last_name")
  def mobile = column[Long]("mobile")
  def email = column[String]("email")

  override def * =
    (id, firstName, lastName, mobile, email) <> (User.User.tupled, User.User.unapply)
}

Below is my Service:

trait UserService {

    def createUser(user: User): Future[User]

}

object UserServiceImpl extends UserService {
    val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
    val users = TableQuery[UserTable]

    override def createUser(user: User): Future[User] = {
      dbConfig.db.run(users += user).map(res => user)
    }

}

Upvotes: 2

Views: 98

Answers (1)

Priya R
Priya R

Reputation: 451

Found solution myself:

def createUser = Action.async(parse.json) { request =>
    val b = request.body.validate[User]
    b.fold(
      errors => {
        Future {
          BadRequest(Json.obj("status" -> "OK", "message" -> JsError.toJson(errors)))
        }
      },
      user => {
        UserServiceImpl.createUser(user).map(res => Ok) recover {
          case ex: Exception => InternalServerError
        }
      }
    )
  }

Upvotes: 2

Related Questions