/// <summary> /// Read the list of objects from a file. /// </summary> public static List <T2> ReadFromFile <T1, T2>(string filePath, bool inputData = false) where T1 : EntityBase where T2 : EntityBase { using (var dbContext = new BankAccountContext()) { var repository = new RepositoryBaseEF <T1>(dbContext); string json = string.Empty; if (repository.Get().Count <= 0 || inputData) { // Get the current WORKING directory (i.e. \bin\Debug) string workingDirectory = Directory.GetCurrentDirectory(); // Get the current PROJECT directory string projectDirectory = Directory.GetParent(workingDirectory).Parent.Parent.FullName; filePath = projectDirectory + filePath; json = File.ReadAllText(filePath); } return(json == string.Empty ? new List <T2>() : Newtonsoft.Json.JsonConvert.DeserializeObject <List <T2> >(json)); } }
public void AccountData() { var dataFixture = new DataFixture(); dataFixture.Fixture().ConfigureAwait(false).GetAwaiter().GetResult(); // Initialize the list of objects and save them into database table. var filePath = "\\Data\\accounts.json"; var accountsDTO = IOHelpers.ReadFromFile <Account, AccountDTO>(filePath, true); var repositoryAccount = new RepositoryAccount(); var accounts = IOHelpers.EntitiesList <Account, AccountDTO>(accountsDTO, repositoryAccount.BusinessToDomainObjectPropertyMap()); var accountsNew = new List <Account>(); using (var dbContext = new BankAccountContext()) { var repository = new RepositoryBaseEF <Account>(dbContext); accountsNew = repository.Get(); } dataFixture.Dispose().ConfigureAwait(false).GetAwaiter().GetResult(); Assert.NotNull(accountsNew); var count = accounts.Count; Assert.Equal(accounts[count - 1].AccountNo, accountsNew[count - 1].AccountNo); }
public void CustomerData() { var dataFixture = new DataFixture(); dataFixture.Fixture().ConfigureAwait(false).GetAwaiter().GetResult(); // Initialize the list of objects and save them into database table. string filePath = "\\Data\\customers.json"; var customersDTO = IOHelpers.ReadFromFile <Customer, CustomerDTO>(filePath, true); var repositoryCustomer = new RepositoryCustomer(); var customers = IOHelpers.EntitiesList <Customer, CustomerDTO>(customersDTO, repositoryCustomer.BusinessToDomainObjectPropertyMap()); var customersNew = new List <Customer>(); using (var dbContext = new BankAccountContext()) { var repository = new RepositoryBaseEF <Customer>(dbContext); customersNew = repository.Get(); } dataFixture.Dispose().ConfigureAwait(false).GetAwaiter().GetResult(); Assert.NotNull(customersNew); var count = customers.Count; Assert.Equal(customers[count - 1].BirthDate, customersNew[count - 1].BirthDate); }
/// <summary> /// Validate customer of an account. /// </summary> public static async Task <bool> CustomerValidation(int accountId, int customerId, string accountNo) { using (var dbContext = new BankAccountContext()) { var repositoryCustomer = new RepositoryBaseEF <Customer>(dbContext); var customer = await repositoryCustomer.FindById(customerId).ConfigureAwait(false); var repositoryAccount = new RepositoryBaseEF <Account>(dbContext); if (repositoryAccount.Get().Any(a => a.Id == accountId)) { return(true); } var message = string.Format("Customer ID: {0}, You do not allow to withdraw money from Account No.: {1}", customer.CustomerNo, accountNo); Console.WriteLine(message); return(false); } }
/// <summary> /// Read the list of objects from a file. /// </summary> public static async Task <Dictionary <string, decimal> > Transaction(string filePath) { var transactionsDTO = IOHelpers.ReadFromFile <Transaction, TransactionDTO>(filePath, true); var repositoryTransaction = new RepositoryTransaction(); var transactions = IOHelpers.EntitiesList <Transaction, TransactionDTO>(transactionsDTO, repositoryTransaction.BusinessToDomainObjectPropertyMap()); await repositoryTransaction.UpdateIds(transactions, transactionsDTO); Dictionary <string, decimal> balances = new Dictionary <string, decimal>(); using (var dbContext = new BankAccountContext()) { var repositoryAccountContext = new RepositoryBaseEF <Account>(dbContext); for (int i = 0; i < transactions.Count; i++) { var transactionDone = false; var account = await repositoryAccountContext.FindById(transactions[i].AccountId); repositoryTransaction = new RepositoryTransaction(account); var message = string.Empty; message = string.Format("Ouput {0}:", (i + 1).ToString()); repositoryTransaction.report(message); var validation = true; if (transactions[i].TransactionType == TransactionTypes.WithdrawCash || transactions[i].TransactionType == TransactionTypes.WithdrawTrnasfer) { validation = await Validators.Validations.CustomerValidation(transactions[i].AccountId, transactions[i].CustomerId, transactionsDTO[i].AccountNo); } var accountDestination = new Account(); if (validation) { switch (transactions[i].TransactionType) { case TransactionTypes.DepositCash: repositoryTransaction.DepositCash(transactions[i]); message = string.Format("Account Number: {0} Balance: ${1} CAD", account.AccountNo, account.Balance.ToString()); repositoryTransaction.report(message); transactionDone = true; break; case TransactionTypes.WithdrawCash: message = repositoryTransaction.WithdrawCash(transactions[i]); if (message.Equals(string.Empty)) { message = string.Format("Account Number: {0} Balance: ${1} CAD", account.AccountNo, account.Balance.ToString()); repositoryTransaction.report(message); transactionDone = true; } else { repositoryTransaction.report(message); transactionDone = false; } break; case TransactionTypes.WithdrawTrnasfer: foreach (Account ac in repositoryAccountContext.Get()) { if (ac.AccountNo.Equals(transactions[i].TransactionAccountNo)) { accountDestination = ac; break; } } message = repositoryTransaction.WithdrawTrnasfer(transactions[i], accountDestination); if (message.Equals(string.Empty)) { message = string.Format("Account Number: {0} Balance: ${1} CAD Account Number: {2} Balance: ${3} CAD", account.AccountNo, account.Balance, accountDestination.AccountNo, accountDestination.Balance); repositoryTransaction.report(message); transactionDone = true; } else { repositoryTransaction.report(message); transactionDone = false; } break; } } if (transactionDone) { var repositoryTransactionContext = new RepositoryBaseEF <Transaction>(dbContext); await repositoryTransactionContext.Create(transactions[i]).ConfigureAwait(false); await repositoryTransactionContext.SaveAsync().ConfigureAwait(false); await repositoryAccountContext.Update(account).ConfigureAwait(false); await repositoryAccountContext.SaveAsync().ConfigureAwait(false); if (accountDestination != null && transactions[i].TransactionType == TransactionTypes.WithdrawTrnasfer) { await repositoryAccountContext.Update(accountDestination).ConfigureAwait(false); await repositoryAccountContext.SaveAsync().ConfigureAwait(false); } } if (account != null && account.AccountNo != null && account.AccountNo != string.Empty) { var key = account.AccountNo; if (balances.ContainsKey(key)) { balances[key] = account.Balance; } else { balances.Add(key, account.Balance); } } if (accountDestination != null && accountDestination.AccountNo != null && accountDestination.AccountNo != string.Empty) { var key = accountDestination.AccountNo; if (balances.ContainsKey(key)) { balances[key] = accountDestination.Balance; } else { balances.Add(key, accountDestination.Balance); } } } } return(balances); }
/// <summary> /// Read the list of transactions from file and do them. /// </summary> public static async Task TransactionProcessAsync(CancellationToken cancellationToken = default) { try { string filePath = "\\Data\\transactions.json"; var transactionsDTO = IOHelpers.ReadFromFile <Transaction, TransactionDTO>(filePath); var repositoryTransaction = new RepositoryTransaction(); var transactions = IOHelpers.EntitiesList <Transaction, TransactionDTO>(transactionsDTO, repositoryTransaction.BusinessToDomainObjectPropertyMap()); await repositoryTransaction.UpdateIds(transactions, transactionsDTO); using (var dbContext = new BankAccountContext()) { var repositoryAccountContext = new RepositoryBaseEF <Account>(dbContext); for (int i = 0; i < transactions.Count; i++) { var transactionDone = false; var account = await repositoryAccountContext.FindById(transactions[i].AccountId); repositoryTransaction = new RepositoryTransaction(account); var message = string.Empty; message = string.Format("Ouput {0}:", (i + 1).ToString()); repositoryTransaction.report(message); var validation = true; if (transactions[i].TransactionType == Models.Enums.TransactionTypes.WithdrawCash || transactions[i].TransactionType == Models.Enums.TransactionTypes.WithdrawTrnasfer) { validation = await Validators.Validations.CustomerValidation(transactions[i].AccountId, transactions[i].CustomerId, transactionsDTO[i].AccountNo); } var accountDestination = new Account(); if (validation) { switch (transactions[i].TransactionType) { case Models.Enums.TransactionTypes.DepositCash: repositoryTransaction.DepositCash(transactions[i]); message = string.Format("Account Number: {0} Balance: ${1} CAD", account.AccountNo, account.Balance.ToString()); repositoryTransaction.report(message); transactionDone = true; break; case Models.Enums.TransactionTypes.WithdrawCash: message = repositoryTransaction.WithdrawCash(transactions[i]); if (message.Equals(string.Empty)) { message = string.Format("Account Number: {0} Balance: ${1} CAD", account.AccountNo, account.Balance.ToString()); repositoryTransaction.report(message); transactionDone = true; } else { repositoryTransaction.report(message); transactionDone = false; } break; case Models.Enums.TransactionTypes.WithdrawTrnasfer: foreach (Account ac in repositoryAccountContext.Get()) { if (ac.AccountNo.Equals(transactions[i].TransactionAccountNo)) { accountDestination = ac; break; } } message = repositoryTransaction.WithdrawTrnasfer(transactions[i], accountDestination); if (message.Equals(string.Empty)) { message = string.Format("Account Number: {0} Balance: ${1} CAD Account Number: {2} Balance: ${3} CAD", account.AccountNo, account.Balance, accountDestination.AccountNo, accountDestination.Balance); repositoryTransaction.report(message); transactionDone = true; } else { repositoryTransaction.report(message); transactionDone = false; } break; } } if (transactionDone) { var repositoryTransactionContext = new RepositoryBaseEF <Transaction>(dbContext); await repositoryTransactionContext.Create(transactions[i]).ConfigureAwait(false); await repositoryTransactionContext.SaveAsync().ConfigureAwait(false); await repositoryAccountContext.Update(account).ConfigureAwait(false); await repositoryAccountContext.SaveAsync().ConfigureAwait(false); if (accountDestination != null && transactions[i].TransactionType == Models.Enums.TransactionTypes.WithdrawTrnasfer) { await repositoryAccountContext.Update(accountDestination).ConfigureAwait(false); await repositoryAccountContext.SaveAsync().ConfigureAwait(false); } } } } } catch (DbException ex) { Console.WriteLine(ex.Message); } catch (FileNotFoundException ex) { Console.WriteLine(ex.Message); } }