public Task <InquiryViewModel> Handle(InquiryByAccountNumberQuery request, CancellationToken cancellationToken)
        {
            var account = _accountRepository.GetAccountByAccountNumber(request.AccountNumber);

            if (account == null)
            {
                throw new InquiryFailException($"Account Number - {request.AccountNumber} could not be found.");
            }

            var currentBalance = _accountRepository.GetCurrentBalanceByAccountId(account);

            _bankingSystemDbContext.Transactions.Add(new AccountTransaction()
            {
                AccountId           = account.Id,
                Amount              = currentBalance,
                Action              = ActionCode.Inquiry,
                TransactionDatetime = _machineDateTime.Now
            });

            if (_bankingSystemDbContext.SaveChanges() <= 0)
            {
                throw new InquiryFailException($"Inquiry fail with account number - {request.AccountNumber}.");
            }

            return(Task.FromResult(new InquiryViewModel()
            {
                AccountNumber = account.AccountNumber,
                Currency = account.Currency,
                Balance = currentBalance
            }));
        }
        public async Task <Unit> Handle(WithdrawCommand request, CancellationToken cancellationToken)
        {
            var account = _accountRepository.GetAccountByAccountNumber(request.AccountNumber);

            if (account == null)
            {
                throw new WithdrawFailException($"No Account was found by account numbe - {request.AccountNumber}");
            }

            decimal rates = 1;

            if (request.Currency != account.Currency)
            {
                rates = await _currencyService.GetRates(account.Currency, request.Currency);

                if (rates <= 0)
                {
                    throw new WithdrawFailException($"Invalid Currency {request.Currency}.");
                }
            }

            var currentBalance = _accountRepository.GetCurrentBalanceByAccountId(account);
            var actualAmout    = (request.Amount * rates);

            if (actualAmout <= 0)
            {
                throw new WithdrawFailException($"Invalid actual amount after exchanged ({actualAmout}).");
            }

            if (actualAmout > currentBalance)
            {
                throw new WithdrawFailException("The current balance not enough to withdraw.");
            }

            _bankingSystemDbContext.Transactions.Add(new AccountTransaction()
            {
                AccountId           = account.Id,
                Amount              = actualAmout,
                Action              = ActionCode.Withdraw,
                TransactionDatetime = _machineDateTime.Now
            });

            account.LastActivityDate = _machineDateTime.Now;

            if (_bankingSystemDbContext.SaveChanges() <= 0)
            {
                throw new WithdrawFailException($"Withdraw fail with account number - {request.AccountNumber}.");
            }

            return(Unit.Value);
        }
        public async Task <Unit> Handle(DepositCommand request, CancellationToken cancellationToken)
        {
            var account = _accountRepository.GetAccountByAccountNumber(request.AccountNumber);

            if (account == null)
            {
                throw new DepositFailException("Account Number - {request.AccountNumber} could not be found.");
            }

            decimal rates = 1;

            if (request.Currency != account.Currency)
            {
                rates = await _currencyService.GetRates(account.Currency, request.Currency);

                if (rates <= 0)
                {
                    throw new DepositFailException($"Invalid Currency {request.Currency}");
                }
            }

            var actualAmout = (request.Amount * rates);

            if (actualAmout <= 0)
            {
                throw new DepositFailException($"Invalid actual amount {actualAmout}");
            }

            _bankingSystemDbContext.Transactions.Add(new AccountTransaction()
            {
                AccountId           = account.Id,
                Amount              = actualAmout,
                Action              = ActionCode.Deposit,
                TransactionDatetime = _machineDateTime.Now
            });

            account.LastActivityDate = _machineDateTime.Now;

            if (_bankingSystemDbContext.SaveChanges() <= 0)
            {
                throw new WithdrawFailException($"Withdraw fail with account number - {request.AccountNumber}.");
            }

            return(Unit.Value);
        }
Beispiel #4
0
        public static BankingSystemDbContext Create()
        {
            var options = new DbContextOptionsBuilder <BankingSystemDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var context = new BankingSystemDbContext(options);

            context.Database.EnsureCreated();


            int a1 = 1,
                a2 = 2,
                a3 = 3;

            context.BankAccounts.AddRange(new[] {
                new BankAccount()
                {
                    Id = a1, AccountNumber = "1", IsActive = true, Currency = "THB"
                },
                new BankAccount()
                {
                    Id = a2, AccountNumber = "2", IsActive = true, Currency = "THB"
                },
                new BankAccount()
                {
                    Id = a3, AccountNumber = "3", IsActive = true, Currency = "THB"
                }
            });

            context.Transactions.AddRange(
                new AccountTransaction()
            {
                AccountId = a1, Id = 10001, Action = ActionCode.Deposit, Amount = 100, Note = "This is the initial deposit.", TransactionDatetime = new DateTime(2018, 12, 5, 22, 30, 0, 0)
            },
                new AccountTransaction()
            {
                AccountId = a2, Id = 10002, Action = ActionCode.Deposit, Amount = 200, Note = "This is the initial deposit.", TransactionDatetime = new DateTime(2018, 12, 5, 22, 30, 0, 0)
            },
                new AccountTransaction()
            {
                AccountId = a3, Id = 10003, Action = ActionCode.Deposit, Amount = 300, Note = "This is the initial deposit.", TransactionDatetime = new DateTime(2018, 12, 5, 22, 30, 0, 0)
            }
                );

            context.Statements.AddRange(
                new AccountStatement()
            {
                AccountId = a1, ClosingBalance = 0, Id = 1, StatementDetails = "This statement on Dec, 2018"
            },
                new AccountStatement()
            {
                AccountId = a2, ClosingBalance = 0, Id = 2, StatementDetails = "This statement on Dec, 2018"
            },
                new AccountStatement()
            {
                AccountId = a3, ClosingBalance = 0, Id = 3, StatementDetails = "This statement on Dec, 2018"
            }
                );

            context.SaveChanges();

            return(context);
        }