Ejemplo n.º 1
0
        public static bool WithdrawMoney(int pin, int cardNumber, decimal moneyToWithdraw, ApplicationDbContext context)
        {
            var transactionScope = new TransactionScope(
               TransactionScopeOption.RequiresNew,
               new TransactionOptions()
               {
                   IsolationLevel = (System.Transactions.IsolationLevel) IsolationLevel.RepeatableRead
               });

            using (transactionScope)
            {
                var card = context.CardAccounts.FirstOrDefault(c => c.CardNumber == cardNumber);

                var isPinValid = card != null && card.CardPin == pin;
                var isAmountValid = card.CardCash >= moneyToWithdraw;

                if (isAmountValid && isPinValid )
                {
                    card.CardCash -= moneyToWithdraw;
                    transactionScope.Complete();
                }
                else
                {
                    return false;
                }
            }

            AddTransactionToHistory(cardNumber, moneyToWithdraw, context);

            context.SaveChanges();
            return true;
        }