Esempio n. 1
0
 public WithdrawDeposit()
 {
     InitializeComponent();
     withdrawDepositViewModel = WithdrawDepositViewModel.Instance;
     DataContext = withdrawDepositViewModel;
     //cmbCustomerNo.ItemsSource = customerViewModel.Customers;
 }
Esempio n. 2
0
 public CreateAccount()
 {
     InitializeComponent();
     withdrawDepositViewModel = WithdrawDepositViewModel.Instance;
     DataContext = withdrawDepositViewModel;
     //cmbCustomers.ItemsSource = withdrawDepositViewModel.Customers;
 }
        public IActionResult WithdrawAndDeposit(string accountNumber = null, double amount = 0)
        {
            WithdrawDepositViewModel model = new WithdrawDepositViewModel()
            {
                AccountNumber = accountNumber, Amount = amount
            };

            return(View(model));
        }
Esempio n. 4
0
 public async Task <IActionResult> Deposit(WithdrawDepositViewModel vm)
 {
     if (ModelState.IsValid)
     {
         return(await WithdrawDeposit(vm, true));
     }
     ViewData["Accounts"]         = new SelectList(await _arepo.GetAll(UID()), "Id", "Name");
     ViewData["CheckingAccounts"] = new SelectList(await _bl.GetChecking(_context, UID()), "Id", "Name");
     return(View("Details", CreateDetailsVM(await _arepo.Get(UID(), vm.AccountId), vm)));
 }
Esempio n. 5
0
        public async Task <IActionResult> Withdraw()
        {
            var user = await _userManager.GetUserAsync(User);

            WithdrawDepositViewModel account = new WithdrawDepositViewModel()
            {
                AccountName   = user.AccountName,
                AccountNumber = user.AccountNumber,
                Balance       = user.Balance,
                RowVersion    = user.RowVersion
            };

            return(View(account));
        }
Esempio n. 6
0
        public async Task <Transaction> SaveFunds(TransactionType transactionType, WithdrawDepositViewModel model, ApplicationUser user)
        {
            Transaction transaction = AdjustBalance(transactionType, model.Amount, model.RowVersion, user);

            try
            {
                await _db.SaveChangesAsync();

                return(transaction);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                throw new Exception("Another transaction is ongoing with your account. Please try again.", ex);
            }
        }
        public async Task AddBalanceOnDeposit()
        {
            await BeginTestSqlite(async (service, db) =>
            {
                var user = await db.Users.FirstOrDefaultAsync();
                decimal amountToDeposit        = 100;
                decimal originalBalance        = user.Balance;
                decimal expectedBalance        = originalBalance + amountToDeposit;
                WithdrawDepositViewModel model = new WithdrawDepositViewModel {
                    Amount = amountToDeposit, RowVersion = user.RowVersion
                };

                await service.SaveFunds(TransactionType.Deposit, model, user);
                var result = await db.Users.FirstOrDefaultAsync(p => p.Id == user.Id);

                Assert.Equal(expectedBalance, result.Balance);
            });
        }
Esempio n. 8
0
        private DetailsAccountViewModel CreateDetailsVM(Accounts a, WithdrawDepositViewModel wdvm = null)
        {
            List <TransactionsViewModel> list = new List <TransactionsViewModel>();

            list.AddRange(a.OutgoingTransactions.AsQueryable().Select(t => TVM(t, a.Name)));
            list.AddRange(a.IncomingTransactions.AsQueryable().Select(t => TVM(t, a.Name)));
            var wdViewModel = (wdvm is null || wdvm.AccountId == 0) ? new WithdrawDepositViewModel()
            {
                AccountId = a.Id
            } : wdvm;
            var dvm = new DetailsAccountViewModel
            {
                Accounts        = a,
                TransactionsVMs = list.OrderByDescending(t => t.Timestamp),
                WDViewModel     = wdViewModel
            };

            return(dvm);
        }
Esempio n. 9
0
        // TODO Move to business logic
        private async Task <IActionResult> WithdrawDeposit(WithdrawDepositViewModel vm, bool Deposit)
        {
            Accounts a;

            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    a = await _arepo.Get(UID(), vm.AccountId);

                    var newBal = a.Balance + (Deposit ? vm.Amount : -vm.Amount);
                    if (newBal < 0 && !a.Business)
                    {
                        ModelState.AddModelError("Amount", "Insufficient funds. Negative balance prohibited.");
                        ViewData["Accounts"]         = new SelectList(await _arepo.GetAll(UID()), "Id", "Name");
                        ViewData["CheckingAccounts"] = new SelectList(await _bl.GetChecking(_context, UID()), "Id", "Name");
                        return(View("Details", CreateDetailsVM(await _arepo.Get(UID(), vm.AccountId))));
                    }
                    a.Balance = newBal;
                    _trepo.Add(Deposit ? "Deposit" : "Withdrawal", vm.Amount, null, vm.AccountId);
                    _context.Update(a);
                    await _context.SaveChangesAsync();

                    await transaction.CommitAsync();
                }
                catch (Exception e)
                {
                    _logger.LogError("Transfer failed.", e);
                    throw e; // TODO Not this
                }
            }
            if (a is null)
            {
                return(View("Index"));
            }
            else
            {
                ViewData["Accounts"]         = new SelectList(await _arepo.GetAll(UID()), "Id", "Name");
                ViewData["CheckingAccounts"] = new SelectList(await _bl.GetChecking(_context, UID()), "Id", "Name");
                return(View("Details", CreateDetailsVM(await _arepo.Get(UID(), vm.AccountId))));
            }
        }
Esempio n. 10
0
        public async Task <IActionResult> Deposit(WithdrawDepositViewModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            try
            {
                var transaction = await _transactionManager.SaveFunds(TransactionType.Deposit, model, user);

                return(View("TransactionSummary", transaction));
            }
            catch (Exception ex)
            {
                return(RedirectToAction("Error", new { message = ex.Message }));
            }
        }
Esempio n. 11
0
        public async Task ThrowExceptionOnConcurrencyConflict(TransactionType tranType)
        {
            await BeginTestSql(async (service, db) =>
            {
                var userId     = (await db.Users.FirstOrDefaultAsync()).Id;
                var chromeUser = await db.Users.FirstOrDefaultAsync(p => p.Id == userId);
                var edgeUser   = await db.Users.FirstOrDefaultAsync(p => p.Id == userId);
                WithdrawDepositViewModel modelOnChrome = new WithdrawDepositViewModel {
                    Amount = 100.00m, RowVersion = chromeUser.RowVersion
                };
                WithdrawDepositViewModel modelOnEdge = new WithdrawDepositViewModel {
                    Amount = 250.00m, RowVersion = edgeUser.RowVersion
                };

                // The first user will try to withdraw or deposit
                await service.SaveFunds(TransactionType.Deposit, modelOnChrome, chromeUser);
                // The second user will also try but will throw a Concurrency exception.
                Exception result = await Assert.ThrowsAsync <Exception>(() => service.SaveFunds(TransactionType.Deposit, modelOnEdge, edgeUser));

                Assert.IsType <DbUpdateConcurrencyException>(result.InnerException);
            });
        }
Esempio n. 12
0
        public async Task HaveTransactionOnWithdraw()
        {
            await BeginTestSqlite(async (service, db) =>
            {
                var user = await db.Users.FirstOrDefaultAsync();
                decimal amountToWithdraw       = 100;
                decimal originalBalance        = user.Balance;
                decimal expectedBalance        = originalBalance - amountToWithdraw;
                WithdrawDepositViewModel model = new WithdrawDepositViewModel {
                    Amount = amountToWithdraw, RowVersion = user.RowVersion
                };

                await service.SaveFunds(TransactionType.Withdraw, model, user);
                var result = await db.Transactions
                             .Where(p => p.UserId == user.Id)
                             .OrderByDescending(p => p.TransactionDate)
                             .FirstOrDefaultAsync();

                Assert.Equal(originalBalance, result.BalanceBefore);
                Assert.Equal(expectedBalance, result.BalanceAfter);
                Assert.Equal(amountToWithdraw, result.Amount);
                Assert.Equal(TransactionType.Withdraw, result.TransactionType);
            });
        }
Esempio n. 13
0
 public CreateCustomer()
 {
     InitializeComponent();
     withdrawDepositViewModel = WithdrawDepositViewModel.Instance;
     DataContext = withdrawDepositViewModel;
 }