Created
September 12, 2012 03:39
-
-
Save imeredith/3704141 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
case class SomeModel(field1: String, field2) | |
def js[A:Reads](field: String)(implicit request: Request[JsValue]): Option[A] = (request.body \ field).asOpt[A] | |
def index = Action(parse.json) { implicit request => | |
val field1 = js[String]("field1") | |
val field2 = js[String]("field2") | |
if(!field1.isDefined){ | |
BadRequest(Json.toJson(Map("error" -> "Field 'field1' is not defined"))) | |
} else if(!field2.isDefined){ | |
BadRequest(Json.toJson(Map("error" -> "Field 'field2' is not defined"))) | |
} else { | |
val model = SomeModel(field1.get, field2.get) //Should be ok because of the if checks above | |
// ... | |
// ... | |
Ok(Json.toJson(Map("message"->"everything was ok"))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment