示例#1
0
        public BaseWebServiceResponse Register(RegisterViewModel model)
        {
            var response = new BaseWebServiceResponse
            {
                Error = _userValidationService.ValidateUserDateOfBirth(
                    int.Parse(model.DateOfBirthDay),
                    int.Parse(model.DateOfBirthMonth),
                    int.Parse(model.DateOfBirthYear))
            };

            if (response.Error != null)
            {
                return(response);
            }

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

            response.Error = _userValidationService.ValidatePassword(model.Password, model.ConfirmPassword);

            if (response.Error != null)
            {
                return(response);
            }

            var newUser = _userService.Create(
                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)
            {
                //TODO: Create logging service at Service layer and log error
                _userService.TryRollbackUser(newUser);
                response.Error = new ErrorServiceViewModel()
                {
                    Name    = "User",
                    Message = "There was a problem creating your account. We have been notified of the error but please try again."
                };

                return(response);
            }

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