Exemplo n.º 1
0
        public IActionResult DeductCashAmount(double amount)
        {
            var userName = HttpContext.Session.GetString("CurrentUserName");
            var pin      = HttpContext.Session.GetInt32("CurrentUserPin");

            if (pin == null)
            {
                throw new InvalidOperationException("Wrong user pin.");
            }
            var encodedPin = new Encoder().Encode((int)pin);
            var user       = _bankService.GetUserByNameAndPin(userName, encodedPin);

            if (user == null)
            {
                throw new InvalidOperationException("Wrong user credits.");
            }

            if (user.Balance >= amount && amount > 0)
            {
                user.Balance -= amount;
                _bankService.UpdateUser(user);
                var transaction = new Transaction(user.UserName, -amount, DateTime.Now);
                _bankService.AddTransaction(transaction);
                _bankService.DatabaseSaveChanges();
                return(RedirectToAction("DeductSuccess", "Cash"));
            }

            return(RedirectToAction("CashRequestMenu", "Cash"));
        }