From 0329c7d8763395d1a11d6e2b95f6949dbb410e14 Mon Sep 17 00:00:00 2001 From: Athou Date: Sat, 9 Aug 2014 19:04:37 +0200 Subject: [PATCH] registration module --- src/main/app/js/controllers.js | 20 +++++++++++-- src/main/app/js/main.js | 3 +- src/main/app/templates/welcome.html | 29 +++++++++++++++---- .../com/commafeed/CommaFeedApplication.java | 2 -- .../model/request/RegistrationRequest.java | 10 +++++++ .../commafeed/frontend/resource/UserREST.java | 16 ++++++++-- 6 files changed, 65 insertions(+), 15 deletions(-) diff --git a/src/main/app/js/controllers.js b/src/main/app/js/controllers.js index 21b1b7eb..0a5aa002 100644 --- a/src/main/app/js/controllers.js +++ b/src/main/app/js/controllers.js @@ -1455,14 +1455,12 @@ module.controller('MetricsCtrl', ['$scope', 'AdminMetricsService', function($sco $scope.metrics = AdminMetricsService.get(); }]); -module.controller('WelcomeCtrl', ['$scope', '$location', 'SessionService', 'ServerService', function($scope, $location, SessionService, ServerService) { - $scope.ServerService = ServerService.get(); +module.controller('LoginCtrl', ['$scope', '$location', 'SessionService', function($scope, $location, SessionService) { $scope.model = {}; var login = function(model) { var success = function(data) { $location.path('/'); - $scope.$emit('emitReload'); }; var error = function(data) { $scope.message = data.data; @@ -1483,3 +1481,19 @@ module.controller('WelcomeCtrl', ['$scope', '$location', 'SessionService', 'Serv login($scope.model); }; }]); + +module.controller('RegisterCtrl', ['$scope', '$location', 'SessionService', 'ServerService', function($scope, $location, SessionService, ServerService) { + $scope.ServerService = ServerService.get(); + $scope.model = {}; + + $scope.register = function() { + var success = function(data) { + $location.path('/'); + $scope.$emit('emitReload'); + }; + var error = function(data) { + $scope.messages = data.data.errors; + }; + SessionService.register($scope.model, success, error); + } +}]); diff --git a/src/main/app/js/main.js b/src/main/app/js/main.js index 10a6ee4d..21dd5551 100644 --- a/src/main/app/js/main.js +++ b/src/main/app/js/main.js @@ -124,8 +124,7 @@ app.config(['$routeProvider', '$stateProvider', '$urlRouterProvider', '$httpProv $stateProvider.state('welcome', { url : '/welcome', - templateUrl : 'templates/welcome.html', - controller : 'WelcomeCtrl' + templateUrl : 'templates/welcome.html' }); $urlRouterProvider.when('/', '/feeds/view/category/all'); diff --git a/src/main/app/templates/welcome.html b/src/main/app/templates/welcome.html index aed0fe88..2eb068a7 100644 --- a/src/main/app/templates/welcome.html +++ b/src/main/app/templates/welcome.html @@ -20,7 +20,7 @@
-
+

Login

@@ -40,10 +40,29 @@
-
-
-

Register

- +
+
+
+

Register

+
{{message}}
+
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
diff --git a/src/main/java/com/commafeed/CommaFeedApplication.java b/src/main/java/com/commafeed/CommaFeedApplication.java index 93b562bb..e0623c52 100644 --- a/src/main/java/com/commafeed/CommaFeedApplication.java +++ b/src/main/java/com/commafeed/CommaFeedApplication.java @@ -201,9 +201,7 @@ public class CommaFeedApplication extends Application { environment.lifecycle().manage(feedWorker); environment.lifecycle().manage(feedUpdater); - // TODO user login + registration page // TODO translations - } public static void main(String[] args) throws Exception { diff --git a/src/main/java/com/commafeed/frontend/model/request/RegistrationRequest.java b/src/main/java/com/commafeed/frontend/model/request/RegistrationRequest.java index a307ee90..1c1bbd2a 100644 --- a/src/main/java/com/commafeed/frontend/model/request/RegistrationRequest.java +++ b/src/main/java/com/commafeed/frontend/model/request/RegistrationRequest.java @@ -4,6 +4,10 @@ import java.io.Serializable; import lombok.Data; +import org.hibernate.validator.constraints.Email; +import org.hibernate.validator.constraints.Length; +import org.hibernate.validator.constraints.NotEmpty; + import com.wordnik.swagger.annotations.ApiModel; import com.wordnik.swagger.annotations.ApiModelProperty; @@ -13,12 +17,18 @@ import com.wordnik.swagger.annotations.ApiModelProperty; public class RegistrationRequest implements Serializable { @ApiModelProperty(value = "username, between 3 and 32 characters", required = true) + @Length(min = 3, max = 32) + @NotEmpty private String name; @ApiModelProperty(value = "password, minimum 6 characters", required = true) + @Length(min = 6) + @NotEmpty private String password; @ApiModelProperty(value = "email address for password recovery", required = true) + @Email + @NotEmpty private String email; } diff --git a/src/main/java/com/commafeed/frontend/resource/UserREST.java b/src/main/java/com/commafeed/frontend/resource/UserREST.java index d0f55268..cca7b6b3 100644 --- a/src/main/java/com/commafeed/frontend/resource/UserREST.java +++ b/src/main/java/com/commafeed/frontend/resource/UserREST.java @@ -2,10 +2,14 @@ package com.commafeed.frontend.resource; import io.dropwizard.hibernate.UnitOfWork; import io.dropwizard.jersey.sessions.Session; +import io.dropwizard.jersey.validation.ValidationErrorMessage; import java.util.Arrays; +import java.util.Collections; import javax.servlet.http.HttpSession; +import javax.validation.ConstraintViolation; +import javax.validation.Valid; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; @@ -40,6 +44,7 @@ import com.commafeed.frontend.model.request.ProfileModificationRequest; import com.commafeed.frontend.model.request.RegistrationRequest; import com.google.common.base.Optional; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; import com.wordnik.swagger.annotations.Api; import com.wordnik.swagger.annotations.ApiOperation; import com.wordnik.swagger.annotations.ApiParam; @@ -200,12 +205,17 @@ public class UserREST { @POST @UnitOfWork @ApiOperation(value = "Register a new account") - public Response register(@ApiParam(required = true) RegistrationRequest req) { + public Response register(@Valid @ApiParam(required = true) RegistrationRequest req) { try { userService.register(req.getName(), req.getPassword(), req.getEmail(), Arrays.asList(Role.USER)); return Response.ok().build(); - } catch (Exception e) { - return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); + } catch (final IllegalArgumentException e) { + return Response.status(422).entity(new ValidationErrorMessage(Collections.> emptySet()) { + @Override + public ImmutableList getErrors() { + return ImmutableList.of(e.getMessage()); + } + }).build(); } }