Esempio n. 1
0
        public WithdrawData Withdraw(long ccNumber, string pinCode, int sum)
        {
            CreditCard creditCard = GetCreditCard(ccNumber);

            CheckPinCodeInternal(creditCard, pinCode);

            if (creditCard.Balance - sum < 0)
                throw new InsufficientFundsException();

            DateTime now = DateTime.Now;

            using (TransactionScope transaction = new TransactionScope())
            {
                creditCard.Balance -= sum;

                Operation operation = new Operation
                {
                    CreditCardId = creditCard.Id,
                    Date = now,
                    Type = OperationType.Withdraw
                };

                operation.SetMetadata(new WithdrawMetadata
                {
                    Balance = creditCard.Balance,
                    Withdraw = sum
                });

                Context.Operations.Add(operation);

                Context.SaveChanges();

                transaction.Complete();
            }

            return new WithdrawData
            {
                Balance = creditCard.Balance,
                Date = now,
                CreditCardNumber = creditCard.CreditCardNumber,
                Withdraw = sum
            };
        }