コード例 #1
0
        public async Task <CreateAccountResponse> CreateAccountAsync(CreateAccountRequest request)
        {
            CreateAccountResponse response = new CreateAccountResponse();

            // E-mail validation
            Email            email          = new Email(request.Email);
            EmailValidator   emailValidator = new EmailValidator();
            ValidationResult emailResult    = await emailValidator.ValidateAsync(email);

            if (!emailResult.IsValid)
            {
                response.AddErrorValidationResult(emailResult);
                return(response);
            }

            bool emailExiste = await _unitOfWork.UserRepository.IsEmailExistsAsync(request.Email);

            if (emailExiste)
            {
                response.SetError(new ErrorResponse("Email", Msg.EmailExists));
                return(response);
            }

            // User validation
            User             user          = new User(request.Name, email, request.Password);
            UserValidator    userValidator = new UserValidator();
            ValidationResult userResult    = await userValidator.ValidateAsync(user);

            if (!userResult.IsValid)
            {
                response.AddErrorValidationResult(userResult);
                return(response);
            }

            // Crypt
            user.CryptPassword(user.Password);

            // Persistence
            await _unitOfWork.UserRepository.CreateAsync(user);

            await _unitOfWork.SaveAsync();

            // Send mail
            //emailService.SendEmailNewUser(user.Email.EmailAddress, user.UserSecurity.TokenCreateConfirmed, user.FisrtName);

            response.IdUser = user.UserId;
            return(response);
        }