Last active
December 12, 2015 03:28
-
-
Save dsugden/4706996 to your computer and use it in GitHub Desktop.
Implicit conversion for JSON
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
implicit val ArticleCreate = Json.reads[ArticleForCreation] | |
def create = IsAuthenticated { username => | |
implicit request => | |
userIdOpt.map { userId => | |
request.body.asJson.map { json => | |
json.validate[ArticleForCreation].fold( | |
invalid => { | |
BadRequest(Json.toJson(Map("error" -> invalid.head.toString))) | |
}, | |
valid => { | |
Articles.create(userId, valid) match { | |
case Success(a) => { | |
Ok(Json.toJson(a)) | |
} | |
case Failure(e) => BadRequest(Json.toJson(Map("error" -> e.getMessage()))) | |
} | |
}) | |
}.getOrElse(BadRequest(Json.toJson( | |
Map("status" -> "error request.body", "message" -> "Not Json")))) | |
}.getOrElse(BadRequest(Json.toJson( | |
Map("status" -> "error userID", "message" -> "Not Json")))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment