/// <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); } }
/// <summary> /// Initialize the list of objects and save them into database table and do the transactions process. /// </summary> public static async Task SaveDataAsync(CancellationToken cancellationToken = default) { try { // Initialize the list of objects and save them into database table. string filePath = "\\Data\\customers.json"; var customersDTO = IOHelpers.ReadFromFile <Customer, CustomerDTO>(filePath); var repositoryCustomer = new RepositoryCustomer(); var customers = IOHelpers.EntitiesList <Customer, CustomerDTO>(customersDTO, repositoryCustomer.BusinessToDomainObjectPropertyMap()); using (var dbContext = new BankAccountContext()) { var repository = new RepositoryBaseEF <Customer>(dbContext); await repository.CreateEntities(customers).ConfigureAwait(false); await repository.SaveAsync().ConfigureAwait(false); } filePath = "\\Data\\accounts.json"; var accountsDTO = IOHelpers.ReadFromFile <Account, AccountDTO>(filePath); var repositoryAccount = new RepositoryAccount(); var accounts = IOHelpers.EntitiesList <Account, AccountDTO>(accountsDTO, repositoryAccount.BusinessToDomainObjectPropertyMap()); using (var dbContext = new BankAccountContext()) { var repository = new RepositoryBaseEF <Account>(dbContext); await repository.CreateEntities(accounts).ConfigureAwait(false); await repository.SaveAsync().ConfigureAwait(false); } if (accountsDTO.Count > 0) { await repositoryCustomer.CreateCustomerAccounts(accountsDTO); } filePath = "\\Data\\phones.json"; var phonesDTO = IOHelpers.ReadFromFile <PhoneNumber, PhoneNumberDTO>(filePath); var repositoryPhone = new RepositoryPhone(); var phones = IOHelpers.EntitiesList <PhoneNumber, PhoneNumberDTO>(phonesDTO, repositoryPhone.BusinessToDomainObjectPropertyMap()); if (phonesDTO.Count > 0 && customersDTO.Count > 0) { repositoryPhone.UpdateCustomerIds(customersDTO, phones, phonesDTO); } using (var dbContext = new BankAccountContext()) { var repository = new RepositoryBaseEF <PhoneNumber>(dbContext); await repository.CreateEntities(phones).ConfigureAwait(false); await repository.SaveAsync().ConfigureAwait(false); } } catch (DbException ex) { Console.WriteLine(ex.Message); } catch (FileNotFoundException ex) { Console.WriteLine(ex.Message); } }