-
-
Save ruud/10672131 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 controllers; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import models.Computer; | |
| import play.mvc.*; | |
| import play.data.Form; | |
| import java.util.Map; | |
| public class Application extends Controller { | |
| private static Form<Computer> form = Form.form(Computer.class); | |
| private static ObjectMapper mapper = new ObjectMapper(); | |
| public static Result create() { | |
| Form<Computer> computerForm = form.bindFromRequest(); | |
| if(computerForm.hasErrors()) { | |
| return badRequest(computerForm.errorsAsJson()); | |
| } else { | |
| return ok("computer created successfully"); | |
| } | |
| } | |
| public static Result edit() { | |
| Computer c = new Computer(); | |
| c.description = "foo"; | |
| c.name = "bar"; | |
| c.id = 101L; | |
| final Form<Computer> edit = editableForm(c); | |
| if(edit.hasErrors()) { | |
| return badRequest(edit.errorsAsJson()); | |
| } else { | |
| Computer edited = edit.get(); | |
| return ok("Success fully edited " + edited.name); | |
| } | |
| } | |
| private static Form<Computer> editableForm(final Computer obj) { | |
| Map<String,String> data = mapper.convertValue(obj, Map.class); | |
| Map<String, String> submittedData = form.bindFromRequest().data(); | |
| data.putAll(submittedData); | |
| return form.bind(data); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment