Exemplo n.º 1
0
        public ActionResult CcPurchase(int accountId)
        {
            using (var _context = new ApplicationDbContext()) {
                var ccAccount = _context.Accounts.Single(a => a.Id == accountId);

                var model = new CcPurchaseViewModel {
                    Cc = (CreditCard)ccAccount
                };

                return(View("CcPurchase", model));
            }
        }
Exemplo n.º 2
0
        public ActionResult ProcessCcPurchase(CcPurchaseViewModel model)
        {
            using (var _context = new ApplicationDbContext()) {
                CreditCard account = _context.Accounts.Single(a => a.Id == model.Cc.Id) as CreditCard;

                //Create viewmodel to return
                var backModel = model;

                //validation
                if (!ModelState.IsValid)
                {
                    return(View("CcPurchase", backModel));
                }

                //check available balance
                if (model.Transaction.Amount > (account.Limit - (-account.Balance)))
                {
                    return(View("CcPurchase", backModel));
                }

                //process purchase
                account.Balance -= model.Transaction.Amount;

                //create transaction
                var transaction = new Transaction {
                    Amount               = model.Transaction.Amount,
                    Description          = model.Transaction.Description,
                    FromCurrentBalance   = model.Cc.Balance,
                    TransactionTimeStamp = DateTime.Now,
                    BankAccountBaseId    = model.Cc.Id
                };

                //update db
                _context.Transactions.Add(transaction);
                _context.SaveChanges();
            }

            return(RedirectToAction("Index", "LoggedIn"));
        }