public TransactionDto Pay(CreditCardDto from, BankAccountDto to, double amountDollar) { if (!IsValid(from)) { return(null); } var builder = new TransactionBuilder(); Transaction transaction = builder .From(from.CardNumber) .To(to.Id) .AmountDollar(amountDollar) .TimeUtcNow() .Build(); using (var db = new BankSystemContext()) { Transaction saved = db.Transactions.Add(transaction); BankAccountDetails fromBank = db.BankAccounts.FirstOrDefault(bank => bank.CreditCards.Any(card => card.CardNumber == from.CardNumber)); if (fromBank == null) { return(null); } fromBank.Balance -= transaction.AmountDollar; BankAccountDetails toBank = db.BankAccounts.Find(to.Id); toBank.Balance += transaction.AmountDollar; db.SaveChanges(); return(mapper.Map <TransactionDto>(transaction)); } }
public bool IsValid(CreditCardDto details) { return(mapper.Map <CreditCardDetails>(details).IsValid()); }