public PageOfPersonDocuments SearchPersons(int pageNumber, PersonSearchColumn personSearchColumn, SortDirection sortDirection, int numRecordsInPage, Dictionary<PersonSearchColumn, string> criteria) { var pageOfListPerson = footlooseService.SearchPersonDocuments(pageNumber, personSearchColumn, sortDirection, numRecordsInPage, criteria); var pageOfPersons = new PageOfPersonDocuments(); pageOfPersons.Data = pageOfListPerson.Data; pageOfPersons.PageIndex = pageOfListPerson.PageIndex; pageOfPersons.PageSize = pageOfListPerson.PageSize; pageOfPersons.TotalItemCount = pageOfListPerson.TotalItemCount; return pageOfPersons; }
public ActionResult Search(SearchParameters searchParameters) { // Serialize sort column to an enum of PersonSearchColumn PersonSearchColumn personSearchColumn = PersonSearchColumn.LastName; Enum.TryParse <PersonSearchColumn>(searchParameters.SortColumn, out personSearchColumn); // Serialize sort direction to an enum of SortDirection SortDirection sortDirection = SortDirection.Ascending; Enum.TryParse <SortDirection>(searchParameters.SortDirection, out sortDirection); // Serialize the search criteria array into a dictionary of person search column and value Dictionary <PersonSearchColumn, string> searchCriteria = new Dictionary <PersonSearchColumn, string>(); if (searchParameters.SearchCriteria != null && searchParameters.SearchCriteria.Count() > 0) { foreach (var searchCriterion in searchParameters.SearchCriteria) { if (!string.IsNullOrEmpty(searchCriterion.Value)) { PersonSearchColumn column = PersonSearchColumn.None; Enum.TryParse <PersonSearchColumn>(searchCriterion.Key, out column); if (column != PersonSearchColumn.None) { searchCriteria.Add(column, searchCriterion.Value); } } } } var personsPage = _footlooseFSService.SearchPersonDocuments(searchParameters.PageNumber, personSearchColumn, sortDirection, searchParameters.NumberRecordsPerPage, searchCriteria); personsPage.SearchCriteria = searchParameters.SearchCriteria; return(PartialView(personsPage)); }
public PageOfList <PersonDocument> SearchPersonDocuments(int pageNumber, PersonSearchColumn personSearchColumn, SortDirection sortDirection, int numRecordsInPage, Dictionary <PersonSearchColumn, string> searchCriteria) { // Search for persons using the Document DB repository // Determine the starting row int startRow; int totalItemCount = 0; if (numRecordsInPage == -1) { startRow = 0; } else { startRow = (pageNumber - 1) * numRecordsInPage; } PageOfList <PersonDocument> searchResults = null; var unitOfWork = new FootlooseFSDocUnitOfWork(); IQueryable <PersonDocument> personsQueryable = unitOfWork.Persons.GetQueryable(); foreach (KeyValuePair <PersonSearchColumn, string> entry in searchCriteria) { if (entry.Key == PersonSearchColumn.PersonID) { var personID = Int32.Parse(entry.Value); personsQueryable = personsQueryable.Where(p => p.PersonID == personID); } var searchValue = entry.Value.ToLower(); if (entry.Key == PersonSearchColumn.FirstName) { personsQueryable = personsQueryable.Where(p => p.FirstName.ToLower().StartsWith(searchValue)); } if (entry.Key == PersonSearchColumn.LastName) { personsQueryable = personsQueryable.Where(p => p.LastName.ToLower().StartsWith(searchValue)); } if (entry.Key == PersonSearchColumn.EmailAddress) { personsQueryable = personsQueryable.Where(p => p.EmailAddress.ToLower().StartsWith(searchValue)); } if (entry.Key == PersonSearchColumn.Phone) { personsQueryable = personsQueryable.Where(p => p.PhoneNumber.ToLower().StartsWith(searchValue)); } if (entry.Key == PersonSearchColumn.City) { personsQueryable = personsQueryable.Where(p => p.City.ToLower().StartsWith(searchValue)); } if (entry.Key == PersonSearchColumn.State) { personsQueryable = personsQueryable.Where(p => p.State.ToLower().StartsWith(searchValue)); } if (entry.Key == PersonSearchColumn.StreetAddress) { personsQueryable = personsQueryable.Where(p => p.StreetAddress.ToLower().StartsWith(searchValue)); } if (entry.Key == PersonSearchColumn.Zip) { personsQueryable = personsQueryable.Where(p => p.Zip.ToLower().StartsWith(searchValue)); } } IOrderedQueryable <PersonDocument> personOrderedQueryable = null; // Apply the sorting using the requested sort column and direction if (sortDirection == SortDirection.Ascending) { if (personSearchColumn == PersonSearchColumn.PersonID) { personOrderedQueryable = personsQueryable.OrderBy(p => p.PersonID); } else if (personSearchColumn == PersonSearchColumn.FirstName) { personOrderedQueryable = personsQueryable.OrderBy(p => p.FirstName); } else if (personSearchColumn == PersonSearchColumn.LastName) { personOrderedQueryable = personsQueryable.OrderBy(p => p.LastName); } else if (personSearchColumn == PersonSearchColumn.EmailAddress) { personOrderedQueryable = personsQueryable.OrderBy(p => p.EmailAddress); } else if (personSearchColumn == PersonSearchColumn.Phone) { personOrderedQueryable = personsQueryable.OrderBy(p => p.PhoneNumber); } else if (personSearchColumn == PersonSearchColumn.City) { personOrderedQueryable = personsQueryable.OrderBy(p => p.City); } else if (personSearchColumn == PersonSearchColumn.State) { personOrderedQueryable = personsQueryable.OrderBy(p => p.State); } else if (personSearchColumn == PersonSearchColumn.StreetAddress) { personOrderedQueryable = personsQueryable.OrderBy(p => p.StreetAddress); } else if (personSearchColumn == PersonSearchColumn.Zip) { personOrderedQueryable = personsQueryable.OrderBy(p => p.Zip); } } else { if (personSearchColumn == PersonSearchColumn.PersonID) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.PersonID); } else if (personSearchColumn == PersonSearchColumn.FirstName) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.FirstName); } else if (personSearchColumn == PersonSearchColumn.LastName) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.LastName); } else if (personSearchColumn == PersonSearchColumn.EmailAddress) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.EmailAddress); } else if (personSearchColumn == PersonSearchColumn.Phone) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.PhoneNumber); } else if (personSearchColumn == PersonSearchColumn.City) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.City); } else if (personSearchColumn == PersonSearchColumn.State) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.State); } else if (personSearchColumn == PersonSearchColumn.StreetAddress) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.StreetAddress); } else if (personSearchColumn == PersonSearchColumn.Zip) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.Zip); } } // Get the number of records int recordCount = personOrderedQueryable.Count(); // Apply the paging List <PersonDocument> persons; if (numRecordsInPage != -1) { persons = personOrderedQueryable.Skip(startRow) .Take(numRecordsInPage) .ToList(); } else { persons = personOrderedQueryable.ToList(); } return(new PageOfList <PersonDocument>(persons, pageNumber, numRecordsInPage, recordCount)); }
public PageOfList <Person> SearchPersons(int pageNumber, PersonSearchColumn personSearchColumn, SortDirection sortDirection, int numRecordsInPage, Dictionary <PersonSearchColumn, string> searchCriteria) { // Search for persons using the SQL repository // Determine the starting row int startRow; int totalItemCount = 0; if (numRecordsInPage == -1) { startRow = 0; } else { startRow = (pageNumber - 1) * numRecordsInPage; } PageOfList <Person> searchResults = null; using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork()) { IQueryable <Person> personsQueryable = unitOfWork.Persons.GetQueryable(); foreach (KeyValuePair <PersonSearchColumn, string> entry in searchCriteria) { if (entry.Key == PersonSearchColumn.PersonID) { var personID = Int32.Parse(entry.Value); personsQueryable = personsQueryable.Where(p => p.PersonID == personID); } if (entry.Key == PersonSearchColumn.FirstName) { personsQueryable = personsQueryable.Where(p => p.FirstName.StartsWith(entry.Value)); } if (entry.Key == PersonSearchColumn.LastName) { personsQueryable = personsQueryable.Where(p => p.LastName.StartsWith(entry.Value)); } if (entry.Key == PersonSearchColumn.EmailAddress) { personsQueryable = personsQueryable.Where(p => p.EmailAddress.StartsWith(entry.Value)); } if (entry.Key == PersonSearchColumn.Phone) { personsQueryable = personsQueryable.Where(p => p.Phones.Any(h => h.PhoneTypeID == 1 && h.Number.StartsWith(entry.Value))); } if (entry.Key == PersonSearchColumn.City) { personsQueryable = personsQueryable.Where(p => p.Addresses.Any(pa => pa.AddressTypeID == 1 && pa.Address.City.StartsWith(entry.Value))); } if (entry.Key == PersonSearchColumn.State) { personsQueryable = personsQueryable.Where(p => p.Addresses.Any(pa => pa.AddressTypeID == 1 && pa.Address.State.StartsWith(entry.Value))); } if (entry.Key == PersonSearchColumn.StreetAddress) { personsQueryable = personsQueryable.Where(p => p.Addresses.Any(pa => pa.AddressTypeID == 1 && pa.Address.StreetAddress.StartsWith(entry.Value))); } if (entry.Key == PersonSearchColumn.Zip) { personsQueryable = personsQueryable.Where(p => p.Addresses.Any(pa => pa.AddressTypeID == 1 && pa.Address.Zip.StartsWith(entry.Value))); } } // Include the related phones and addresses in the search query personsQueryable = personsQueryable.Include("Addresses.Address").Include("Phones"); IOrderedQueryable <Person> personOrderedQueryable = null; // Apply the sorting using the requested sort column and direction if (sortDirection == SortDirection.Ascending) { if (personSearchColumn == PersonSearchColumn.PersonID) { personOrderedQueryable = personsQueryable.OrderBy(p => p.PersonID); } else if (personSearchColumn == PersonSearchColumn.FirstName) { personOrderedQueryable = personsQueryable.OrderBy(p => p.FirstName); } else if (personSearchColumn == PersonSearchColumn.LastName) { personOrderedQueryable = personsQueryable.OrderBy(p => p.LastName); } else if (personSearchColumn == PersonSearchColumn.EmailAddress) { personOrderedQueryable = personsQueryable.OrderBy(p => p.EmailAddress); } else if (personSearchColumn == PersonSearchColumn.Phone) { personOrderedQueryable = personsQueryable.OrderBy(p => p.Phones.FirstOrDefault().Number); } else if (personSearchColumn == PersonSearchColumn.City) { personOrderedQueryable = personsQueryable.OrderBy(p => p.Addresses.FirstOrDefault().Address.City); } else if (personSearchColumn == PersonSearchColumn.State) { personOrderedQueryable = personsQueryable.OrderBy(p => p.Addresses.FirstOrDefault().Address.State); } else if (personSearchColumn == PersonSearchColumn.StreetAddress) { personOrderedQueryable = personsQueryable.OrderBy(p => p.Addresses.FirstOrDefault().Address.StreetAddress); } else if (personSearchColumn == PersonSearchColumn.Zip) { personOrderedQueryable = personsQueryable.OrderBy(p => p.Addresses.FirstOrDefault().Address.Zip); } } else { if (personSearchColumn == PersonSearchColumn.PersonID) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.PersonID); } else if (personSearchColumn == PersonSearchColumn.FirstName) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.FirstName); } else if (personSearchColumn == PersonSearchColumn.LastName) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.LastName); } else if (personSearchColumn == PersonSearchColumn.EmailAddress) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.EmailAddress); } else if (personSearchColumn == PersonSearchColumn.Phone) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.Phones.FirstOrDefault().Number); } else if (personSearchColumn == PersonSearchColumn.City) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.Addresses.FirstOrDefault().Address.City); } else if (personSearchColumn == PersonSearchColumn.State) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.Addresses.FirstOrDefault().Address.State); } else if (personSearchColumn == PersonSearchColumn.StreetAddress) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.Addresses.FirstOrDefault().Address.StreetAddress); } else if (personSearchColumn == PersonSearchColumn.Zip) { personOrderedQueryable = personsQueryable.OrderByDescending(p => p.Addresses.FirstOrDefault().Address.Zip); } } // Get the number of records int recordCount = personOrderedQueryable.Count(); // Apply the paging and make the SQL call List <Person> persons; if (numRecordsInPage != -1) { persons = personOrderedQueryable.Skip(startRow) .Take(numRecordsInPage) .ToList(); } else { persons = personOrderedQueryable.ToList(); } searchResults = new PageOfList <Person>(persons, pageNumber, numRecordsInPage, recordCount); } return(searchResults); }
public PageOfList<PersonDocument> SearchPersonDocuments(int pageNumber, PersonSearchColumn personSearchColumn, SortDirection sortDirection, int numRecordsInPage, Dictionary<PersonSearchColumn, string> searchCriteria) { // Search for persons using the Document DB repository // Determine the starting row from the pageNumber and numRecordsInPage int startRow; int totalItemCount = 0; if (numRecordsInPage == -1) startRow = 0; else startRow = (pageNumber - 1) * numRecordsInPage; PageOfList<PersonDocument> searchResults = null; // Note the Document Unit of Work will be disposed when out of scope (does not require using statement) var unitOfWork = new FootlooseFSDocUnitOfWork(); IQueryable<PersonDocument> personsQueryable = unitOfWork.Persons.GetQueryable(); // For each search criteria given add an appropriate filter to the Person Queryable foreach (KeyValuePair<PersonSearchColumn, string> entry in searchCriteria) { if (entry.Key == PersonSearchColumn.PersonID) { var personID = Int32.Parse(entry.Value); personsQueryable = personsQueryable.Where(p => p.PersonID == personID); } var searchValue = entry.Value.ToLower(); if (entry.Key == PersonSearchColumn.FirstName) personsQueryable = personsQueryable.Where(p => p.FirstName.ToLower().StartsWith(searchValue)); if (entry.Key == PersonSearchColumn.LastName) personsQueryable = personsQueryable.Where(p => p.LastName.ToLower().StartsWith(searchValue)); if (entry.Key == PersonSearchColumn.EmailAddress) personsQueryable = personsQueryable.Where(p => p.EmailAddress.ToLower().StartsWith(searchValue)); if (entry.Key == PersonSearchColumn.Phone) personsQueryable = personsQueryable.Where(p => p.PhoneNumber.ToLower().StartsWith(searchValue)); if (entry.Key == PersonSearchColumn.City) personsQueryable = personsQueryable.Where(p => p.City.ToLower().StartsWith(searchValue)); if (entry.Key == PersonSearchColumn.State) personsQueryable = personsQueryable.Where(p => p.State.ToLower().StartsWith(searchValue)); if (entry.Key == PersonSearchColumn.StreetAddress) personsQueryable = personsQueryable.Where(p => p.StreetAddress.ToLower().StartsWith(searchValue)); if (entry.Key == PersonSearchColumn.Zip) personsQueryable = personsQueryable.Where(p => p.Zip.ToLower().StartsWith(searchValue)); } IOrderedQueryable<PersonDocument> personOrderedQueryable = null; // Apply the sorting using the requested sort column and direction if (sortDirection == SortDirection.Ascending) { if (personSearchColumn == PersonSearchColumn.PersonID) personOrderedQueryable = personsQueryable.OrderBy(p => p.PersonID); else if (personSearchColumn == PersonSearchColumn.FirstName) personOrderedQueryable = personsQueryable.OrderBy(p => p.FirstName); else if (personSearchColumn == PersonSearchColumn.LastName) personOrderedQueryable = personsQueryable.OrderBy(p => p.LastName); else if (personSearchColumn == PersonSearchColumn.EmailAddress) personOrderedQueryable = personsQueryable.OrderBy(p => p.EmailAddress); else if (personSearchColumn == PersonSearchColumn.Phone) personOrderedQueryable = personsQueryable.OrderBy(p => p.PhoneNumber); else if (personSearchColumn == PersonSearchColumn.City) personOrderedQueryable = personsQueryable.OrderBy(p => p.City); else if (personSearchColumn == PersonSearchColumn.State) personOrderedQueryable = personsQueryable.OrderBy(p => p.State); else if (personSearchColumn == PersonSearchColumn.StreetAddress) personOrderedQueryable = personsQueryable.OrderBy(p => p.StreetAddress); else if (personSearchColumn == PersonSearchColumn.Zip) personOrderedQueryable = personsQueryable.OrderBy(p => p.Zip); } else { if (personSearchColumn == PersonSearchColumn.PersonID) personOrderedQueryable = personsQueryable.OrderByDescending(p => p.PersonID); else if (personSearchColumn == PersonSearchColumn.FirstName) personOrderedQueryable = personsQueryable.OrderByDescending(p => p.FirstName); else if (personSearchColumn == PersonSearchColumn.LastName) personOrderedQueryable = personsQueryable.OrderByDescending(p => p.LastName); else if (personSearchColumn == PersonSearchColumn.EmailAddress) personOrderedQueryable = personsQueryable.OrderByDescending(p => p.EmailAddress); else if (personSearchColumn == PersonSearchColumn.Phone) personOrderedQueryable = personsQueryable.OrderByDescending(p => p.PhoneNumber); else if (personSearchColumn == PersonSearchColumn.City) personOrderedQueryable = personsQueryable.OrderByDescending(p => p.City); else if (personSearchColumn == PersonSearchColumn.State) personOrderedQueryable = personsQueryable.OrderByDescending(p => p.State); else if (personSearchColumn == PersonSearchColumn.StreetAddress) personOrderedQueryable = personsQueryable.OrderByDescending(p => p.StreetAddress); else if (personSearchColumn == PersonSearchColumn.Zip) personOrderedQueryable = personsQueryable.OrderByDescending(p => p.Zip); } // Get the number of records int recordCount = personOrderedQueryable.Count(); // Apply the paging List<PersonDocument> persons; if (numRecordsInPage != -1) persons = personOrderedQueryable.Skip(startRow) .Take(numRecordsInPage) .ToList(); else persons = personOrderedQueryable.ToList(); return new PageOfList<PersonDocument>(persons, pageNumber, numRecordsInPage, recordCount); }