Advertisement
Guest User

Untitled

a guest
May 31st, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. package controllers;
  2.  
  3. import com.fasterxml.jackson.databind.node.ObjectNode;
  4. import models.User;
  5. import play.data.Form;
  6. import play.data.validation.Constraints;
  7. import play.libs.Json;
  8. import play.mvc.Controller;
  9. import play.mvc.Result;
  10.  
  11. /**
  12. * Created by lukasz on 2016-05-31.
  13. */
  14. public class Application extends Controller {
  15.  
  16. public static Result signup() {
  17. Form<SignUp> signUpForm = Form.form(SignUp.class).bindFromRequest();
  18.  
  19. if ( signUpForm.hasErrors()) {
  20. return badRequest(signUpForm.errorsAsJson());
  21. }
  22. SignUp newUser = signUpForm.get();
  23. User existingUser = User.findByEmail(newUser.email);
  24. if(existingUser != null) {
  25. return badRequest(buildJsonResponse("error", "User exists"));
  26. } else {
  27. User user = new User();
  28. user.setEmail(newUser.email);
  29. user.setPassword(newUser.password);
  30. user.save();
  31. session().clear();
  32. session("username", newUser.email);
  33.  
  34. return ok(buildJsonResponse("success", "User created successfully"));
  35. }
  36. }
  37.  
  38. public static class UserForm {
  39. @Constraints.Required
  40. @Constraints.Email
  41. public String email;
  42. }
  43.  
  44. public static class SignUp extends UserForm {
  45. @Constraints.Required
  46. @Constraints.MinLength(6)
  47. public String password;
  48. }
  49.  
  50. private static ObjectNode buildJsonResponse(String type, String message) {
  51. ObjectNode wrapper = Json.newObject();
  52. ObjectNode msg = Json.newObject();
  53. msg.put("message", message);
  54. wrapper.put(type, msg);
  55. return wrapper;
  56. }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement