private static void RecordTransaction( ATMEntities context, string cardNumber, decimal amount) { using (var tran = context.Database.BeginTransaction(System.Data.IsolationLevel.RepeatableRead)) { try { var transactionRecord = new TransactionHistory(); transactionRecord.CardNumber = cardNumber; transactionRecord.TransactionDate = DateTime.Now; transactionRecord.Amount = amount; context.TransactionHistories.Add(transactionRecord); context.SaveChanges(); tran.Commit(); //Console.WriteLine(accounts.First().CardCash -= amount); } catch (Exception e) { tran.Rollback(); throw (e); } } }
private static void SaveTransactionHistory(string cardNumber, decimal amount) { var db = new ATMEntities(); var newTransaction = new TransactionHistory() { CardNumber = cardNumber, TransactionDate = DateTime.Now, Amount = amount }; db.TransactionHistories.Add(newTransaction); db.SaveChanges(); }
private static void ATMWithdrawWithHystory(ATMEntities context, int userId) { using (var dbContextTransaction = context.Database.BeginTransaction()) { var account = context.CardAccounts.Find(userId); try { var givenAccount = context.CardAccounts .Where(a => a.CardNumber == account.CardNumber && a.CardPIN == account.CardPIN); foreach (var acc in givenAccount) { Console.Write("Enter card number: "); string card = Console.ReadLine(); Console.Write("Enter PIN: "); string pin = Console.ReadLine(); if (card != acc.CardNumber || pin != acc.CardPIN) { throw new Exception("Invalid card number or PIN"); } Console.Write("Enter sum: "); decimal sum = decimal.Parse(Console.ReadLine()); if (acc.CardCash < sum) { throw new Exception("Available cash is not enough to commit transaction"); } acc.CardCash -= sum; var note = new TransactionHistory() { CardNumber = card, TransactionDate = DateTime.Now, Amount = sum }; context.TransactionHistories.Add(note); } context.SaveChanges(); dbContextTransaction.Commit(); Console.WriteLine("Transaction has been commited"); } catch (Exception ex) { dbContextTransaction.Rollback(); Console.WriteLine("Error: " + ex.Message); } } }