3

I downloaded the typesafe application "modern-web-template", which implements a crud application with play + scala + reactivemongo

I was trying to add a new functionality. I want to be able by calling a URL with two params like this

localhost:9000/users?dni&30000000

first I added this route to the routes file

GET     /users                      @controllers.Users.findUsersParams(tipoDocumento: String ?= "", numeroDocumento:  String ?= "")

Then I added this method to the controller

def findUsersParams(tipoDocumento: String, numeroDocumento: String) = Action.async {
// let's do our query
val cursor: Cursor[User] = collection.
  // find all
  find(Json.obj("tipoDocumento" -> tipoDocumento, "numeroDocumento" -> numeroDocumento)).
  // sort them by creation date
  sort(Json.obj("created" -> -1)).
  // perform the query and get a cursor of JsObject
  cursor[User]

// gather all the JsObjects in a list
val futureUsersList: Future[List[User]] = cursor.collect[List]()

// transform the list into a JsArray
val futurePersonsJsonArray: Future[JsArray] = futureUsersList.map { users =>
  Json.arr(users)
}
// everything's ok! Let's reply with the array
futurePersonsJsonArray.map {
  users =>
    Ok(users(0))
}
}

I am not able to return the expected result that should be one user, instead, I am getting all the users inside the collection

agusgambina
  • 6,229
  • 14
  • 54
  • 94

1 Answers1

1

There is an error in your Query, it should be

http://localhost:9000/users?tipoDocumento=dni&numeroDocumento=30000000

Other than that code seems fine, and should be working.

mavarazy
  • 7,562
  • 1
  • 34
  • 60
  • thanks for your answers. The right URL is the one you posted, but still not working, keeps me retrieving all the users. – agusgambina Oct 26 '14 at 00:14
  • 1
    Maybe you have overlapping mapping, check in debug, that you actually reach this method. – mavarazy Oct 26 '14 at 03:28
  • Thank you @mavarazy was that, routes were overlapping mapping. I changed one of the routes name and worked perfectly. – agusgambina Oct 26 '14 at 04:09