public async Task Execute(WithdrawInput input)
        {
            IAccount account = await _accountRepository.Get(input.AccountId);

            if (account == null)
            {
                _outputHandler.Error($"The account {input.AccountId} does not exist or is already closed.");
                return;
            }

            IDebit debit = account.Withdraw(_entityFactory, input.Amount);

            if (debit == null)
            {
                _outputHandler.Error($"The account {input.AccountId} does not have enough funds to withdraw {input.Amount}.");
                return;
            }

            await _accountRepository.Update(account, debit);

            // Publish the event to the enterprice service bus
            await _serviceBus.PublishEventAsync(new Shared.Events.WithdrawCompleted()
            {
                AccountId = input.AccountId, Amount = input.Amount.ToMoney().ToDecimal()
            });

            await _unitOfWork.Save();

            WithdrawOutput output = new WithdrawOutput(
                debit,
                account.GetCurrentBalance()
                );

            _outputHandler.Default(output);
        }
示例#2
0
        public async Task <IActionResult> Withdraw([FromBody] WithdrawRequest request)
        {
            WithdrawOutput output = await _withdrawUseCase.Execute(request.AccountId, request.Amount);

            _presenter.Populate(output);
            return(_presenter.ViewModel);
        }
        public async Task Execute(WithdrawInput input)
        {
            IAccount account = await _accountRepository.Get(input.AccountId);

            if (account == null)
            {
                _outputHandler.Error($"The account {input.AccountId} does not exist or is already closed.");
                return;
            }

            IDebit debit = account.Withdraw(_entityFactory, input.Amount);

            if (debit == null)
            {
                _outputHandler.Error($"The account {input.AccountId} does not have enough funds to withdraw {input.Amount}.");
                return;
            }

            await _accountRepository.Update(account, debit);

            await _unitOfWork.Save();

            WithdrawOutput output = new WithdrawOutput(
                debit,
                account.GetCurrentBalance()
                );

            _outputHandler.Default(output);
        }
        private void BuildOutput(IDebit debit, IAccount account)
        {
            var output = new WithdrawOutput(
                debit,
                account.GetCurrentBalance());

            _outputPort.Standard(output);
        }
 public void Default(WithdrawOutput output)
 {
     ViewModel = new ObjectResult(new WithdrawResponse(
                                      output.Transaction.Amount,
                                      output.Transaction.Description,
                                      output.Transaction.TransactionDate,
                                      output.UpdatedBalance
                                      ));
 }
        /// <summary>
        /// </summary>
        /// <param name="output"></param>
        public void Standard(WithdrawOutput output)
        {
            var debitModel       = new DebitModel((Debit)output.Transaction);
            var withdrawResponse = new WithdrawResponse(
                debitModel,
                output.UpdatedBalance.ToDecimal());

            this.ViewModel = new ObjectResult(withdrawResponse);
        }
        public void Standard(WithdrawOutput withdrawOutput)
        {
            var withdrawResponse = new WithdrawResponse(
                withdrawOutput.Transaction.Amount.ToMoney().ToDecimal(),
                withdrawOutput.Transaction.Description,
                withdrawOutput.Transaction.TransactionDate,
                withdrawOutput.UpdatedBalance.ToDecimal());

            ViewModel = new ObjectResult(withdrawResponse);
        }
        public void Default(WithdrawOutput withdrawOutput)
        {
            var withdrawResponse = new WithdrawResponse(
                withdrawOutput.Transaction.Amount,
                withdrawOutput.Transaction.Description,
                withdrawOutput.Transaction.TransactionDate,
                withdrawOutput.UpdatedBalance
                );

            ViewModel = new ObjectResult(withdrawResponse);
        }
        public void Populate(WithdrawOutput output)
        {
            if (output == null)
            {
                ViewModel = new NoContentResult();
                return;
            }

            ViewModel = new ObjectResult(new {
                Amount          = output.Transaction.Amount,
                Description     = output.Transaction.Description,
                TransactionDate = output.Transaction.TransactionDate,
                UpdatedBalance  = output.UpdatedBalance,
            });
        }
示例#10
0
        public async Task <IActionResult> Withdraw([FromRoute] Guid accountId, WithdrawRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            WithdrawOutput output = await _withdrawUseCase.Execute(
                request.AccountId,
                request.Amount);

            _presenter.Populate(output, this);

            return(_presenter.ViewModel);
        }
示例#11
0
        public async Task Withdraw_ValidValues_ShouldCompleteTask()
        {
            //ARRANGE
            var account = BuildAccount(10);
            var amount  = new Amount(10);

            _accountReadOnlyRepository.Setup(m => m.Get(account.Id)).Returns(Task.FromResult(account));
            _accountWriteOnlyRepository.Setup(m => m.Update(account, It.IsAny <Debit>())).Returns(Task.CompletedTask);

            //ACT
            WithdrawOutput outPut = await withdrawUseCase.Execute(account.Id, amount);

            //ASSERT
            _accountReadOnlyRepository.Verify(v => v.Get(account.Id), Times.Once());
            _accountWriteOnlyRepository.Verify(v => v.Update(account, It.IsAny <Debit>()), Times.Once());
            Assert.Equal(0, outPut.UpdatedBalance);
            Assert.NotNull(outPut.Transaction);
        }
示例#12
0
        public async void Withdraw_Valid_Amount(string accountId, decimal amount)
        {
            var mockAccountReadOnlyRepository  = new Mock <IAccountReadOnlyRepository>();
            var mockAccountWriteOnlyRepository = new Mock <IAccountWriteOnlyRepository>();
            TransactionCollection transactions = new TransactionCollection();

            transactions.Add(new Credit(Guid.Empty, 4000));

            Account account = Account.LoadFromDetails(Guid.Parse(accountId), Guid.Empty, transactions);

            mockAccountReadOnlyRepository.SetupSequence(e => e.Get(It.IsAny <Guid>()))
            .ReturnsAsync(account);

            WithdrawUseCase sut = new WithdrawUseCase(
                mockAccountReadOnlyRepository.Object,
                mockAccountWriteOnlyRepository.Object
                );

            WithdrawOutput output = await sut.Execute(
                Guid.Parse(accountId),
                amount);

            Assert.Equal(3900, output.UpdatedBalance);
        }
 public void Default(WithdrawOutput output)
 {
     Withdrawals.Add(output);
 }
示例#14
0
 public void Populate(WithdrawOutput response, Controller controller)
 {
     ViewModel = controller.View();
 }
 public void Standard(WithdrawOutput output)
 {
     Withdrawals.Add(output);
 }
示例#16
0
 public void Standard(WithdrawOutput output) => this.StandardOutput = output;
 public void Standard(WithdrawOutput output) => this.Withdrawals.Add(output);