public void SaveTransaction(IEnumerable <Transaction> transactions)
 {
     try
     {
         foreach (var tran in transactions)
         {
             _context.Add(tran);
             _context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         throw;
     }
 }
예제 #2
0
        public ActionResult Create([Bind(Include = "TransactionID,AccountFromID,AccountToID,TransactionAmount,TransactionDate")] TransactionModel transactionmodel)
        {
            if (ModelState.IsValid)
            {
                var clientID1 = from a in db2.Accounts
                                where a.AccountID.Equals(transactionmodel.AccountFromID)
                                select a.AccountClientID;

                var clientID2 = from a in db2.Accounts
                                where a.AccountID.Equals(transactionmodel.AccountToID)
                                select a.AccountClientID;


                if (clientID1 != null && clientID2 != null && transactionmodel.AccountFromID != transactionmodel.AccountToID)
                {
                    var AccBalance1 = from a in db2.Accounts
                                      where a.AccountID.Equals(transactionmodel.AccountFromID)
                                      select a.AccountBalance;

                    var AccBalance2 = from a in db2.Accounts
                                      where a.AccountID.Equals(transactionmodel.AccountToID)
                                      select a.AccountBalance;


                    if (AccBalance1.FirstOrDefault() - transactionmodel.TransactionAmount >= 0)
                    {
                        var accountToUpdateFrom = db2.Accounts.Where(o => o.AccountID == transactionmodel.AccountFromID);
                        var accountToUpdateTo   = db2.Accounts.Where(o => o.AccountID == transactionmodel.AccountToID);

                        // update LastName for all Persons in personsToUpdate
                        foreach (BankAccountModel p in accountToUpdateFrom)
                        {
                            p.AccountBalance = AccBalance1.FirstOrDefault() - transactionmodel.TransactionAmount;
                        }

                        foreach (BankAccountModel p in accountToUpdateTo)
                        {
                            p.AccountBalance = AccBalance2.FirstOrDefault() + transactionmodel.TransactionAmount;
                        }
                        db.Transactions.Add(transactionmodel);
                        db.SaveChanges();
                        db2.SaveChanges();
                        String currentUser = User.Identity.Name;
                        db3.Loggers.Add(
                            new LoggerModel {
                            userNameLog    = currentUser,
                            dateLog        = DateTime.Now,
                            transactionLog = "Transfer: " + transactionmodel.AccountFromID.ToString() + " to " + transactionmodel.AccountToID.ToString() + " amount: " + transactionmodel.TransactionAmount.ToString() + "\n"
                        }
                            );
                        db3.SaveChanges();
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        return(RedirectToAction("Error1"));
                    }
                }
                else
                {
                    return(RedirectToAction("Error2"));
                }
            }
            return(RedirectToAction("Error"));
        }