コード例 #1
0
        public async Task <CreateAccountOutputDto> CreateAccount(CreateAccountInputDto request)
        {
            var businessExpection = new BusinessException();

            if ((await this.userRepository.GetAllByCriteria(UserSpecification.GetUserByEmail(request.Email))).Any())
            {
                businessExpection.AddError(new BusinessValidationFailure()
                {
                    ErrorMessage = "E-mail já cadastrado na base, por favor tente outro"
                });
            }

            if ((await this.userRepository.GetAllByCriteria(UserSpecification.GetUserByCPF(request.CPF))).Any())
            {
                businessExpection.AddError(new BusinessValidationFailure()
                {
                    ErrorMessage = "CPF já cadastrado na base, por favor tente outro"
                });
            }

            businessExpection.ValidateAndThrow();

            var user = this.mapper.Map <User>(request);

            user.ApplyStatus(Status.CONFIRMACAO_EMAIL);

            user.Validate();

            user.ApplyPassword();


            using (var transaction = await this.userRepository.CreateTransaction())
            {
                try
                {
                    var ms = ProxyUtils.DownloadFromUrl(String.Format(this.DefaultPhoto, Guid.NewGuid().ToString()));
                    user.Photo = await this.storage.SaveToStorage(AzureCloudImageDirectoryStorageEnum.USER, ms, String.Format("{0}.jpg", Guid.NewGuid().ToString().Replace("-", "")));

                    await this.userRepository.Save(user);

                    await transaction.CommitAsync();

                    var notification = new NotificationMessage(user.Email.Valor, new Message(new
                    {
                        Email    = user.Email.Valor,
                        FullName = user.Name,
                        Id       = user.Id
                    }));


                    CreatedAccountEvent accountEvent = new CreatedAccountEvent(CreatedAccountEvent.CREATE_ACCOUNT_EVENT, notification);

                    await this.EventPublisher.Publish <CreatedAccountEvent>(accountEvent);

                    return(this.mapper.Map <CreateAccountOutputDto>(user));
                }
                catch (Exception)
                {
                    await transaction.RollbackAsync();

                    throw;
                }
            }
        }