コード例 #1
0
        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);
        }
コード例 #2
0
        /// <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));
            }
        }
コード例 #3
0
        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);
        }
コード例 #4
0
ファイル: Validations.cs プロジェクト: aazimi3731/BankAccount
        /// <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);
            }
        }
コード例 #5
0
        /// <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);
        }
コード例 #6
0
        /// <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);
            }
        }
コード例 #7
0
        /// <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);
            }
        }