public async Task <Transaction> Handle(WithdrawCommand command)
        {
            Account account = await accountReadOnlyRepository.GetAccount(command.AccountId);

            if (account == null)
            {
                throw new AccountNotFoundException($"The account {command.AccountId} does not exists or is already closed.");
            }

            Transaction transaction = Debit.Create(command.CustomerId, Amount.Create(command.Amount));

            account.Withdraw(transaction);

            var domainEvents = account.GetEvents();
            await bus.Publish(domainEvents, command.Header);

            bool received = false;

            for (int i = 0; i < 5; i++)
            {
                received = await accountReadOnlyRepository.CheckTransactionReceived(account.Id, transaction.Id);

                if (received)
                {
                    break;
                }
            }

            //if (!received)
            //{
            //}

            return(transaction);
        }
Exemple #2
0
        public async Task Handle(CloseCommand command)
        {
            Account account = await accountReadOnlyRepository.GetAccount(command.AccountId);

            if (account == null)
            {
                throw new AccountNotFoundException($"The account {command.AccountId} does not exists or is already closed.");
            }

            account.Close();

            var domainEvents = account.GetEvents();
            await bus.Publish(domainEvents, command.Header);
        }
Exemple #3
0
        public async Task <UpdateAccountOutput> Run(Guid accountId, string accountName)
        {
            var account = await _accountReadOnlyRepository.GetAccount(accountId);

            if (account == null)
            {
                throw new AccountNotFoundException("Account not found.");
            }

            account.UpdateName(accountName);

            await _accountWriteOnlyRepository.Update(account);

            return(new UpdateAccountOutput(account.Id, account.Name));
        }
        public void Handle(ClosedDomainEvent domainEvent)
        {
            Account account = accountReadOnlyRepository.GetAccount(domainEvent.AggregateRootId).Result;

            if (account == null)
            {
                throw new AccountNotFoundException($"The account {domainEvent.AggregateRootId} does not exists or is already closed.");
            }

            if (account.Version != domainEvent.Version)
            {
                throw new TransactionConflictException(account, domainEvent);
            }

            accountWriteOnlyRepository.Delete(account).Wait();
        }
Exemple #5
0
        public async Task <Transaction> Handle(WithdrawCommand command)
        {
            Account account = await accountReadOnlyRepository.GetAccount(command.AccountId);

            if (account == null)
            {
                throw new AccountNotFoundException($"The account {command.AccountId} does not exists or is already closed.");
            }

            Transaction transaction = Debit.Create(command.CustomerId, Amount.Create(command.Amount));

            account.Withdraw(transaction);

            var domainEvents = account.GetEvents();
            await bus.Publish(domainEvents, command.Header);

            return(transaction);
        }
        public async Task <GetAccountOutput> Run(Guid accountId)
        {
            var account = await _accountReadOnlyRepository.GetAccount(accountId);

            return(new GetAccountOutput(account));
        }