/// <summary>
        /// Update the list of phones with the customer Ids.
        /// </summary>
        /// <param name="phones">The phones to be inserted.</param>
        /// <param name="phonesDTO">The phonesDTO to be inserted.</param>
        /// <param name="customersDTO">The customersDTO to be inserted.</param>
        public void UpdateCustomerIds(List <CustomerDTO> customersDTO, List <PhoneNumber> phones, List <PhoneNumberDTO> phonesDTO)
        {
            if (phones == null)
            {
                throw new ArgumentNullException("The phones can not be null.");
            }

            if (phonesDTO == null)
            {
                throw new ArgumentNullException("The phonesDTO can not be null.");
            }

            if (customersDTO == null)
            {
                throw new ArgumentNullException("The customersDTO can not be null.");
            }

            try
            {
                var customers = new List <Customer>();

                using (var dbContext = new BankAccountContext())
                {
                    var repositoryCustomer = new Repositories.RepositoryBaseEF <Customer>(dbContext);

                    customers = repositoryCustomer.Get();
                    if (customers.Count <= 0)
                    {
                        throw new ArgumentNullException("The customers can not be null.");
                    }
                }

                for (int j = 0; j < phonesDTO.Count; j++)
                {
                    for (int i = 0; i < customers.Count; i++)
                    {
                        if (phonesDTO[j].CustomerNo.Equals(customersDTO[i].CustomerNo))
                        {
                            phones[j].CustomerId = customers[i].Id;
                            break;
                        }
                    }
                }
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// Delete all data from the database tables.
        /// </summary>
        public static async Task DeleteData(CancellationToken cancellationToken = default)
        {
            try
            {
                using (var dbContext = new BankAccountContext())
                {
                    var repositoryAccount = new Repositories.RepositoryBaseEF <Account>(dbContext);
                    var accounts          = repositoryAccount.Get();
                    repositoryAccount.DeleteEntities(accounts);
                    await repositoryAccount.SaveAsync().ConfigureAwait(false);

                    var repositoryCustomer = new Repositories.RepositoryBaseEF <Customer>(dbContext);
                    var customers          = repositoryCustomer.Get();
                    repositoryCustomer.DeleteEntities(customers);
                    await repositoryCustomer.SaveAsync().ConfigureAwait(false);

                    var repositoryCustomerAccount = new Repositories.RepositoryBaseEF <CustomerAccount>(dbContext);
                    var customerAccounts          = repositoryCustomerAccount.Get();
                    repositoryCustomerAccount.DeleteEntities(customerAccounts);
                    await repositoryCustomerAccount.SaveAsync().ConfigureAwait(false);

                    var repositoryPhone = new Repositories.RepositoryBaseEF <PhoneNumber>(dbContext);
                    var phones          = repositoryPhone.Get();
                    repositoryPhone.DeleteEntities(phones);
                    await repositoryPhone.SaveAsync().ConfigureAwait(false);

                    var repositoryTransaction = new Repositories.RepositoryBaseEF <Transaction>(dbContext);
                    var transactions          = repositoryTransaction.Get();
                    repositoryTransaction.DeleteEntities(transactions);
                    await repositoryTransaction.SaveAsync().ConfigureAwait(false);
                }
            }
            catch (DbException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Insert a list of customrAccounts in the database table.
        /// </summary>
        /// <param name="accountsDTO">The accountsDTO to be inserted.</param>
        public async Task CreateCustomerAccounts(List <AccountDTO> accountsDTO)
        {
            if (accountsDTO == null)
            {
                throw new ArgumentNullException("The accountsDTO can not be null.");
            }

            var accounts  = new List <Account>();
            var customers = new List <Customer>();

            using (var dbContext = new BankAccountContext())
            {
                var repositoryAccount  = new Repositories.RepositoryBaseEF <Account>(dbContext);
                var repositoryCustomer = new Repositories.RepositoryBaseEF <Customer>(dbContext);

                accounts = repositoryAccount.Get();
                if (accounts.Count <= 0)
                {
                    throw new ArgumentNullException("The accounts can not be null.");
                }

                customers = repositoryCustomer.Get();
                if (customers.Count <= 0)
                {
                    throw new ArgumentNullException("The customers can not be null.");
                }
            }

            try
            {
                using (var dbContext = new BankAccountContext())
                {
                    var repository = new RepositoryBaseEF <CustomerAccount>(dbContext);

                    var customerAccounts = new List <CustomerAccount>();

                    for (int j = 0; j < accountsDTO.Count; j++)
                    {
                        for (int i = 0; i < customers.Count; i++)
                        {
                            if (accountsDTO[j].CustomerNo.Equals(customers[i].CustomerNo))
                            {
                                var customerAccount = new CustomerAccount();
                                customerAccount.CustomerId  = customers[i].Id;
                                customerAccount.AccountId   = accounts[j].Id;
                                customerAccount.CreatedDate = DateTime.Now.ToString();
                                customerAccount.CreatedBy   = "Aziz Azimi";
                                customerAccounts.Add(customerAccount);
                                break;
                            }
                        }
                    }

                    try
                    {
                        await repository.CreateEntities(customerAccounts);

                        await repository.SaveAsync();
                    }
                    catch (DbException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// Update the list of transactions with the customer and account Ids.
        /// </summary>
        /// <param name="transactions">The transactions to be inserted.</param>
        /// <param name="transactionDTOs">The transactionDTOs to be inserted.</param>
        public async Task UpdateIds(List <Transaction> transactions, List <TransactionDTO> transactionDTOs)
        {
            if (transactions == null)
            {
                throw new ArgumentNullException("The transactions can not be null.");
            }

            if (transactionDTOs == null)
            {
                throw new ArgumentNullException("The transactionDTOs can not be null.");
            }

            try
            {
                var customers        = new List <Customer>();
                var accounts         = new List <Account>();
                var customerAccounts = new List <CustomerAccount>();

                using (var dbContext = new BankAccountContext())
                {
                    var repositoryCustomer        = new Repositories.RepositoryBaseEF <Customer>(dbContext);
                    var repositoryAccount         = new Repositories.RepositoryBaseEF <Account>(dbContext);
                    var repositoryCustomerAccount = new Repositories.RepositoryBaseEF <CustomerAccount>(dbContext);

                    customers = await Task.FromResult(repositoryCustomer.Get());

                    accounts         = repositoryAccount.Get();
                    customerAccounts = repositoryCustomerAccount.Get();
                }

                if (accounts.Count <= 0)
                {
                    throw new ArgumentNullException("The accounts can not be null.");
                }

                if (customers.Count <= 0)
                {
                    throw new ArgumentNullException("The customers can not be null.");
                }

                if (customerAccounts.Count <= 0)
                {
                    throw new ArgumentNullException("The customerAccounts can not be null.");
                }

                for (int j = 0; j < transactionDTOs.Count; j++)
                {
                    for (int i = 0; i < customers.Count; i++)
                    {
                        if (transactionDTOs[j].CustomerNo.Equals(customers[i].CustomerNo))
                        {
                            transactions[j].CustomerId = customers[i].Id;
                            transactions[j].AccountId  = -1;
                            if (customers[i].CustomerAccounts != null)
                            {
                                for (int k = 0; k < accounts.Count; k++)
                                {
                                    if (transactionDTOs[j].AccountNo.Equals(accounts[k].AccountNo))
                                    {
                                        transactions[j].AccountId = accounts[k].Id;
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }