public bool TransferCheckingToSaving(long checkingAccountNumber, long savingAccountNumber, decimal amount, decimal transactionFee)
        {
            bool success = false;

            using (var dbContextTransaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    // Taking out money from checking account
                    var checkingAccountRow = (from row in _dbContext.CheckingAccounts
                                              where row.CheckingAccountNumber == checkingAccountNumber
                                              select row).FirstOrDefault <CheckingAccounts>();
                    if (checkingAccountRow != null)
                    {
                        checkingAccountRow.Balance -= amount;
                    }
                    _dbContext.SaveChanges();

                    // Checking if after taking account money, our checking balance is good

                    var checkingBalance = (from row in _dbContext.CheckingAccounts
                                           where row.CheckingAccountNumber == checkingAccountNumber
                                           select row.Balance).FirstOrDefault <decimal>();
                    if (checkingBalance < 0)
                    {
                        throw new Exception("Insufficient funds in checking account");
                    }

                    var savingAccountRow = (from row in _dbContext.SavingAccounts
                                            where row.SavingAccountNumber == savingAccountNumber
                                            select row).FirstOrDefault <SavingAccounts>();
                    if (savingAccountRow != null)
                    {
                        savingAccountRow.Balance += amount;
                    }

                    _dbContext.SaveChanges();

                    TransactionHistories transactions = new TransactionHistories();
                    transactions.CheckingAccountNumber = checkingAccountNumber;
                    transactions.SavingAccountNumber   = savingAccountNumber;
                    transactions.Amount            = amount;
                    transactions.TransactionTypeId = 100;
                    transactions.TransactionFee    = transactionFee;
                    transactions.TransactionDate   = DateTime.Now;
                    _dbContext.TransactionHistories.Add(transactions);
                    _dbContext.SaveChanges();

                    dbContextTransaction.Commit();
                    success = true;
                }
                catch (Exception)
                {
                    dbContextTransaction.Rollback();
                    throw;
                }
            }
            return(success);
        }
Beispiel #2
0
        public bool TransferCheckingToSaving(long checkingAccountNum, long savingAccountNum, decimal amount, decimal transactionFee)
        {
            bool ret = false;

            using (var dbContextTransaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    var reccheck = (from r in _dbContext.CheckingAccounts
                                    where r.CheckingAccountNumber == checkingAccountNum
                                    select r).FirstOrDefault <CheckingAccounts>();
                    if (reccheck != null)
                    {
                        reccheck.Balance = reccheck.Balance - amount;
                    }
                    _dbContext.SaveChanges();
                    var bal = (from r in _dbContext.CheckingAccounts
                               where r.CheckingAccountNumber == checkingAccountNum
                               select r.Balance).FirstOrDefault <decimal>();
                    if (bal < 0)
                    {
                        throw new Exception("insufficient amount in Checking Account..");
                    }
                    var recsav = (from r in _dbContext.SavingAccounts
                                  where r.SavingAccountNumber == savingAccountNum
                                  select r).FirstOrDefault <SavingAccounts>();
                    if (recsav != null)
                    {
                        recsav.Balance = recsav.Balance + amount;
                    }
                    _dbContext.SaveChanges();
                    TransactionHistories th = new TransactionHistories();
                    th.CheckingAccountNumber = checkingAccountNum;
                    th.SavingAccountNumber   = savingAccountNum;
                    th.Amount            = amount;
                    th.TransactionTypeId = 100;
                    th.TransactionFee    = transactionFee;
                    th.TransactionDate   = DateTime.Now;
                    _dbContext.TransactionHistories.Add(th);
                    _dbContext.SaveChanges();
                    dbContextTransaction.Commit();
                    ret = true;
                }
                catch (Exception)
                {
                    dbContextTransaction.Rollback();
                    throw;
                }
            }
            return(ret);
        }