Last active
April 12, 2016 02:06
-
-
Save leandrob13/a0b23345c13af9346972c0278d734e29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package user.controllers | |
import genesis.user.persistence.repository.{UserFilter, UserRepository} | |
import play.api.libs.json._ | |
import play.api.mvc.{Action, Controller} | |
import scala.concurrent.Future | |
/** | |
* Created by leandrob13 on 10/9/15. | |
*/ | |
class UserController extends Controller { | |
val userRepository = UserRepository() | |
implicit val userWrites: Writes[ User ] = Json.writes[ UserRegister ] | |
/* | |
If you want to send a query string like /users?username=anakin.skywalker or /users?email=anakin@starwars.com | |
*/ | |
def findUser(username: Option[String], email: Option[String]) = Action.async { | |
val userFilter = UserFilter( username = username, email = email ) | |
userRepository.find(userFilter).map( u => Ok( Json.toJson( u ) ) ).recoverWith { | |
case ex => InternalServerError(s"Error: $ex") | |
} | |
} | |
/* | |
If you want to send a query string like /users?lastname=skywalker&isActive=true | |
or simply finding active users /users?isActive=true | |
*/ | |
def listUsers(name: Option[String], lastname: Option[String], isActive: Option[Boolean]) = Action.async { | |
val userFilter = UserFilter(name = name, lastname = lastname, active = isActive) | |
userRepository.list(userFilter).map( us => Ok( Json.toJson( us ) ) ).recoverWith { | |
case ex => InternalServerError(s"Error: $ex") | |
} | |
} | |
} | |
object UserController extends UserController |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment