Exemplo n.º 1
0
        public BaseUserWebServiceResponse CreateUser(RegisterViewModel model)
        {
            var response = new BaseUserWebServiceResponse();

            if (!_userValidationService.IsDateOfBirthValid(model.DateOfBirthDay, model.DateOfBirthMonth, model.DateOfBirthYear))
            {
                response.Error = new ErrorServiceResponseModel()
                {
                    Name    = "Date of Birth",
                    Message = "Date of Birth is not valid."
                };
                return(response);
            }

            if (!_userValidationService.IsDateOfBirthOver18(int.Parse(model.DateOfBirthDay), int.Parse(model.DateOfBirthMonth), int.Parse(model.DateOfBirthYear)))
            {
                response.Error = new ErrorServiceResponseModel()
                {
                    Name    = "Date of Birth",
                    Message = "Date of Birth must be over 18 years old."
                };
                return(response);
            }

            if (!_userValidationService.IsDateOfBirthWithinHumanLivingYears(int.Parse(model.DateOfBirthYear)))
            {
                response.Error = new ErrorServiceResponseModel()
                {
                    Name    = "Date of Birth",
                    Message = "Date of Birth must be over 18 years old."
                };
                return(response);
            }

            if (_userValidationService.IsEmailInUse(model.Email))
            {
                response.Error = new ErrorServiceResponseModel()
                {
                    Name    = "Email",
                    Message = "Email is already in use."
                };
                return(response);
            }

            if (!_userValidationService.IsPasswordValid(model.Password))
            {
                response.Error = new ErrorServiceResponseModel()
                {
                    Name    = "Password",
                    Message = "Password does not meet requirements; at least one number, one special character, one upper and lower case and 8 characters minimum."
                };
                return(response);
            }

            if (!_userValidationService.DoPasswordsMatch(model.Password, model.ConfirmPassword))
            {
                response.Error = new ErrorServiceResponseModel()
                {
                    Name    = "Password",
                    Message = "Password and Confirm Password do not match."
                };
                return(response);
            }

            var newUser = _userService.CreateUser(
                new UserDTO()
            {
                Email       = model.Email,
                FirstName   = model.FirstName,
                LastName    = model.LastName,
                DateOfBirth = new DateTime(
                    Convert.ToInt32(model.DateOfBirthYear),
                    Convert.ToInt32(model.DateOfBirthMonth),
                    Convert.ToInt32(model.DateOfBirthDay)),
                IsSubscribed = model.IsSubscribed,
                Password     = model.Password
            });

            if (newUser == null || newUser.Id == 0)
            {
                _userService.TryRollbackUser(newUser);
                response.Error = new ErrorServiceResponseModel()
                {
                    Name    = "User",
                    Message = "There was a problem creating your account. We have been notified of the error but please try again."
                };
                //TODO: Create logging service at Service layer and log error
                return(response);
            }

            response.JsonResponseObject = JsonConvert.SerializeObject(newUser);
            response.ActionSuccessful   = true;
            return(response);
        }