protected override async Task <FundsModel> HandleCore(CreateTransferCommand request)
        {
            var receivingAccountId = new AccountId(request.ReceivingAccountId);
            var sendingAccountId   = new AccountId(request.SendingAccountId);

            var funds = new Funds(request.Funds.Currency, request.Funds.Amount);

            var receivingAccount = await repo.FindAsync(new GetById(receivingAccountId));

            var sendingAccount = await repo.FindAsync(new GetById(sendingAccountId));

            if (receivingAccount == null)
            {
                throw new BadRequestException("Account was not found");
            }

            if (sendingAccount == null)
            {
                throw new BadRequestException("Account was not found");
            }

            var transferService = new TransferFundsService();

            transferService.Transfer(sendingAccount, receivingAccount, funds);

            if (sendingAccount.Balance.IsNegative)
            {
                throw new BadRequestException("Insufficent Funds");
            }

            await repo.UnitOfWork.CommitAsync();

            return(request.Funds);
        }
Example #2
0
 public void Throw_ArgumentNullException_On_Null_From_Account(
     Account to,
     Funds funds,
     TransferFundsService sut)
 {
     Assert.Throws <ArgumentNullException>(() => { sut.Transfer(null, to, funds); });
 }
Example #3
0
 public void Throw_ArgumentNullException_On_Null_Funds(
     Account to,
     Account from,
     TransferFundsService sut)
 {
     Assert.Throws <ArgumentNullException>(() => { sut.Transfer(from, to, null); });
 }
Example #4
0
            public void Succeed_With_Valid_Request(
                Account from,
                Account to,
                Funds funds,
                TransferFundsService sut)
            {
                from.Credit(funds);

                var initialFrom = from.Balance.Amount;
                var initialTo   = to.Balance.Amount;

                sut.Transfer(from, to, funds);

                Assert.True(initialTo + funds.Amount == to.Balance.Amount);
                Assert.True(initialFrom - funds.Amount == from.Balance.Amount);
            }