Skip to content

Instantly share code, notes, and snippets.

@wertlex
Last active December 20, 2015 06:49
Show Gist options
  • Save wertlex/6088484 to your computer and use it in GitHub Desktop.
Save wertlex/6088484 to your computer and use it in GitHub Desktop.
object UserController extends Controller with RESTResponses {
def create = Action.typedJson(UserCreateForm.reads, User.writes) { request =>
val user = request.body.create()
rest.async.Created(dao.save(user))
}
def get(id: String) = Action.httpToTypedJson(User.writes) { request =>
rest.async.Ok(dao.get(Id(id)))
}
def update(id: String) = TODO
def remove(id: String) = Action { request =>
dao.remove(Id(id))
Ok
}
def find = Action.httpToTypedJson(EntityList.writes[User]) { implicit request =>
BindForm(data.userFindForm).fold(
err => BadRequest(None),
form => {
rest.async.Ok(dao.find(form.q, form.offset, form.limit).map( Option(_) ))
}
)
}
}
object Router {
val routes: PartialFunction[Route, Action] = {
case Route(POST, "users" :: Nil) => UserController.create
case Route(GET, "users" :: Nil) => UserController.find
case Route(GET, "users" :: id :: Nil) => UserController.get(id)
case Route(PUT, "users" :: id :: Nil) => UserController.update(id)
case Route(DELETE, "users" :: id :: Nil) => UserController.remove(id)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment