private async Task <PaginationViewModel> CreateSearchPaginationModel(List <string> keywordsList, PresentationSearchType searchType, int numOfPages, PagingOptions currentPagingOptions) { var keywordsInOneString = ""; keywordsList.ForEach(item => keywordsInOneString += item + ","); keywordsInOneString = WebUtility.UrlEncode(keywordsInOneString); var searchTypeString = ""; if ((searchType & PresentationSearchType.Name) != 0) { searchTypeString += "name,"; } if ((searchType & PresentationSearchType.Tags) != 0) { searchTypeString += "tags,"; } if ((searchType & PresentationSearchType.Description) != 0) { searchTypeString += "description,"; } searchTypeString = WebUtility.UrlEncode(searchTypeString); return(PaginationViewModel.BuildModelWith(numOfPages, currentPagingOptions, index => { return "/Explore/SearchPresentations?keywords=" + keywordsInOneString + "&where=" + searchTypeString + "&page=" + index + "&itemsPerPage=" + currentPagingOptions.ItemsPerPage; })); }
public static PresentationSearchType CreateSearchType(string where) { if (where == null || where.Length == 0) { where = "name"; } PresentationSearchType searchType = PresentationSearchType.None; var whereList = WebUtility.UrlDecode(where).Split(new char[] { ',', ' ' }); foreach (var item in whereList) { if (searchTypesPerWhereValues.ContainsKey(item.ToLower())) { searchType |= searchTypesPerWhereValues[item.ToLower()]; } } return(searchType); }
public async Task <PagedOperationResult <List <Presentation> > > SearchPublicPresentations(List <string> keywords, PagingOptions options, PresentationSearchType searchType, string excludeFromUserId) { IQueryable <Presentation> selectedPresentations; if (excludeFromUserId != null) { selectedPresentations = _context.UserPresentations.Where(u => u.UserId != excludeFromUserId) .Include(u => u.Presentation).Where(u => u.Presentation.IsPublic).Select(u => u.Presentation); } else { selectedPresentations = _context.Presentations.Where(p => p.IsPublic); } return(await SearchInPresentations(selectedPresentations, keywords, options, searchType)); }
private async Task <PagedOperationResult <List <Presentation> > > SearchInPresentations(IQueryable <Presentation> selectedPresentations, List <string> keywords, PagingOptions options, PresentationSearchType searchType) { var presentationsResult = new List <Presentation>(); IQueryable <Presentation> finalPresentations = null; foreach (var word in keywords) { var lowerWord = word.ToLower(); List <IQueryable <Presentation> > unionList = new List <IQueryable <Presentation> >(); if ((searchType & PresentationSearchType.Name) > 0) { var query = selectedPresentations.Where(p => p.Name.ToLower().Contains(lowerWord)); unionList.Add(query); } if ((searchType & PresentationSearchType.Description) > 0) { var query = selectedPresentations.Where(p => p.Description.ToLower().Contains(lowerWord)); unionList.Add(query); } if ((searchType & PresentationSearchType.Tags) > 0) { var query = selectedPresentations.Include(p => p.PresentationTags). Where(p => p.PresentationTags.Any(pt => pt.Tag.Name.ToLower().Contains(lowerWord))); unionList.Add(query); } if (unionList.Count > 0) { var endResult = unionList.First(); if (unionList.Count > 1) { for (int i = 1; i < unionList.Count; i++) { var queryableAtI = unionList[i]; endResult = endResult.Union(queryableAtI); } } if (finalPresentations == null) { finalPresentations = endResult; } else { finalPresentations = finalPresentations.Union(endResult); } } } var numOfPages = (int)Math.Ceiling((float)finalPresentations.Count() / options.ItemsPerPage); if (numOfPages == 0) { numOfPages++; } return(new PagedOperationResult <List <Presentation> > { Value = await finalPresentations.Skip(options.ToSkip).Take(options.ItemsPerPage).ToListAsync(), TotalPages = numOfPages }); }
public async Task <PagedOperationResult <List <Presentation> > > SearchUserPresentations(List <string> keywords, string userId, PagingOptions options, PresentationSearchType searchType) { var userPresentations = _context.UserPresentations.Where(u => u.UserId == userId) .Include(u => u.Presentation) .Select(u => u.Presentation); return(await SearchInPresentations(userPresentations, keywords, options, searchType)); }