Exemplo n.º 1
0
        // For Updating categories
        private void UpdateContactCategory(string[] selectedOptions, Contact contactToUpdate)
        {
            if (selectedOptions == null) // Openning with a check
            {
                contactToUpdate.ContactCategories = new List <ContactCategories>();
                return;
            }
            var selectedOptionsHS   = new HashSet <string>(selectedOptions);
            var contactCategoriesHS = new HashSet <int>(contactToUpdate.ContactCategories.Select(c => c.CategoriesID)); // current category selected

            foreach (var option in _context.Categories)
            {
                if (selectedOptionsHS.Contains(option.ID.ToString()))
                {
                    if (!contactCategoriesHS.Contains(option.ID))
                    {
                        contactToUpdate.ContactCategories.Add(new ContactCategories {
                            ContactID = contactToUpdate.ContactID, CategoriesID = option.ID
                        });
                    }
                }
                else
                {
                    if (contactCategoriesHS.Contains(option.ID))
                    {
                        ContactCategories categoryToRemove = contactToUpdate.ContactCategories.SingleOrDefault(c => c.CategoriesID == option.ID);
                        _context.Remove(categoryToRemove);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void UpdateCompanyTypes(string companyTypes, Company companyToUpdate)
        {
            if (String.IsNullOrEmpty(companyTypes))
            {
                return;
            }
            if (companyTypes == ";;")
            {
                companyToUpdate.CompanyCustomers.Clear();
                companyToUpdate.CompanyContractors.Clear();
                companyToUpdate.CompanyVendors.Clear();
                return;
            }
            var selectedOptions = companyTypes.Split(';');

            // Customer Types
            var selectedCustomerHS = new HashSet <string>(selectedOptions[0].Split(','));
            var currentCustomerHS  = new HashSet <int>(companyToUpdate.CompanyCustomers.Select(s => s.CustomerTypeID));

            foreach (var s in _context.CustomerTypes)
            {
                if (selectedCustomerHS.Contains(s.CustomerTypeID.ToString()))
                {
                    if (!currentCustomerHS.Contains(s.CustomerTypeID))
                    {
                        companyToUpdate.CompanyCustomers.Add(new CompanyCustomer
                        {
                            CustomerTypeID = s.CustomerTypeID,
                            CompanyID      = companyToUpdate.CompanyID
                        });
                    }
                }
                else
                {
                    if (currentCustomerHS.Contains(s.CustomerTypeID))
                    {
                        CompanyCustomer specToRemove = companyToUpdate.CompanyCustomers.SingleOrDefault(c => c.CustomerTypeID == s.CustomerTypeID);
                        _context.Remove(specToRemove);
                    }
                }
            }

            //Contractor Types
            var selectedContractorHS = selectedOptions[1].Split(',')
                                       .ToDictionary(k => k.Split('|').First(), v => v.Split('|').Last());
            var currentContractors = companyToUpdate.CompanyContractors;

            foreach (var s in _context.ContractorTypes)
            {
                if (selectedContractorHS.TryGetValue(s.ContractorTypeID.ToString(), out var expiryDate))
                {
                    var companyContractor = currentContractors.FirstOrDefault(c => c.ContractorTypeID == s.ContractorTypeID);
                    if (companyContractor == null)
                    {
                        companyContractor = new CompanyContractor
                        {
                            ContractorTypeID = s.ContractorTypeID,
                            CompanyID        = companyToUpdate.CompanyID
                        };
                        companyToUpdate.CompanyContractors.Add(companyContractor);
                    }

                    if (DateTime.TryParse(expiryDate, out var expDate))
                    {
                        companyContractor.ExpiryDate = expDate;
                    }
                }
                else
                {
                    if (currentContractors.Any(c => c.ContractorTypeID == s.ContractorTypeID))
                    {
                        var specToRemove = companyToUpdate.CompanyContractors.SingleOrDefault(c => c.ContractorTypeID == s.ContractorTypeID);
                        _context.Remove(specToRemove);
                    }
                }
            }

            //Vendor Types
            var selectedVendorHS = new HashSet <string>(selectedOptions[2].Split(','));
            var currentVendorHS  = new HashSet <int>(companyToUpdate.CompanyVendors.Select(s => s.VendorTypeID));

            foreach (var s in _context.VendorTypes)
            {
                if (selectedVendorHS.Contains(s.VendorTypeID.ToString()))
                {
                    if (!currentVendorHS.Contains(s.VendorTypeID))
                    {
                        companyToUpdate.CompanyVendors.Add(new CompanyVendor
                        {
                            VendorTypeID = s.VendorTypeID,
                            CompanyID    = companyToUpdate.CompanyID
                        });
                    }
                }
                else
                {
                    if (currentVendorHS.Contains(s.VendorTypeID))
                    {
                        CompanyVendor specToRemove = companyToUpdate.CompanyVendors.SingleOrDefault(v => v.VendorTypeID == s.VendorTypeID);
                        _context.Remove(specToRemove);
                    }
                }
            }
        }