public async Task<CourseCatalogSearchResult> GetCourseCatalogSearchResultAsync(CourseCatalogSearchRequest request, string userId, Specializations? specializations) { var courseQuery = GetCoursesQueryForSpecializations(specializations); if (!string.IsNullOrWhiteSpace(request.trainingProviderName) && request.trainingProviderName != NavigationViewModel.ALLToken) { courseQuery = courseQuery .Where(x => x.TrainingProvider.Name == request.trainingProviderName); } var recordsTotal = await courseQuery.CountAsync(); var recordsFiltered = recordsTotal; if (!string.IsNullOrWhiteSpace(request.searchTerm)) { courseQuery = courseQuery .Where(x => x.Title.Contains(request.searchTerm)); recordsFiltered = await courseQuery.CountAsync(); } var catalog = await courseQuery .SortCourses(request.order) .Skip(request.start) .Take(request.length) .Select(x => new CourseCatalogEntryViewModel { TrainingProvider = new TrainingProviderViewModel { Name = x.TrainingProvider.Name, LogoFileName = x.TrainingProvider.LogoFileName, SiteUrl = x.TrainingProvider.SiteUrl }, Category = new CategoryViewModel { Title = x.Category.Title, UrlName = x.Category.UrlName, LogoFileName = x.Category.LogoFileName }, Course = new CourseViewModel { Id = x.Id, Title = x.Title, SiteUrl = x.SiteUrl, UrlName = x.UrlName, HasClosedCaptions = x.HasClosedCaptions, Level = x.Level, Rating = x.Rating, Duration = x.Duration, ReleaseDate = x.ReleaseDate, LearningState = x.Subscriptions .Where(s => s.UserId == userId) .Select(s => s.State) .FirstOrDefault(), Authors = x.CourseAuthors .Where(a => (!a.IsDeleted)) .Select(a => new CourseAuthorViewModel { FullName = a.TrainingProviderAuthor.FullName, UrlName = a.TrainingProviderAuthor.UrlName, IsCoAuthor = a.IsAuthorCoAuthor }).ToList() } }).ToListAsync(); foreach (var entry in catalog) { entry.Category.TrainingProvider = entry.TrainingProvider; } var searchResult = new CourseCatalogSearchResult { draw = request.draw, recordsTotal = recordsTotal, recordsFiltered = recordsFiltered, catalog = catalog }; return searchResult; }
public async Task <CourseCatalogSearchResult> GetCourseCatalogSearchResultAsync(CourseCatalogSearchRequest request, string userId, Specializations?specializations) { var courseQuery = GetCoursesQueryForSpecializations(specializations); if (!string.IsNullOrWhiteSpace(request.trainingProviderName) && request.trainingProviderName != NavigationViewModel.ALLToken) { courseQuery = courseQuery .Where(x => x.TrainingProvider.Name == request.trainingProviderName); } var recordsTotal = await courseQuery.CountAsync(); var recordsFiltered = recordsTotal; if (!string.IsNullOrWhiteSpace(request.searchTerm)) { courseQuery = courseQuery .Where(x => x.Title.Contains(request.searchTerm)); recordsFiltered = await courseQuery.CountAsync(); } var catalog = await courseQuery .SortCourses(request.order) .Skip(request.start) .Take(request.length) .Select(x => new CourseCatalogEntryViewModel { TrainingProvider = new TrainingProviderViewModel { Name = x.TrainingProvider.Name, LogoFileName = x.TrainingProvider.LogoFileName, SiteUrl = x.TrainingProvider.SiteUrl }, Category = new CategoryViewModel { Title = x.Category.Title, UrlName = x.Category.UrlName, LogoFileName = x.Category.LogoFileName }, Course = new CourseViewModel { Id = x.Id, Title = x.Title, SiteUrl = x.SiteUrl, UrlName = x.UrlName, HasClosedCaptions = x.HasClosedCaptions, Level = x.Level, Rating = x.Rating, Duration = x.Duration, ReleaseDate = x.ReleaseDate, LearningState = x.Subscriptions .Where(s => s.UserId == userId) .Select(s => s.State) .FirstOrDefault(), Authors = x.CourseAuthors .Where(a => (!a.IsDeleted)) .Select(a => new CourseAuthorViewModel { FullName = a.TrainingProviderAuthor.FullName, UrlName = a.TrainingProviderAuthor.UrlName, IsCoAuthor = a.IsAuthorCoAuthor }).ToList() } }).ToListAsync(); foreach (var entry in catalog) { entry.Category.TrainingProvider = entry.TrainingProvider; } var searchResult = new CourseCatalogSearchResult { draw = request.draw, recordsTotal = recordsTotal, recordsFiltered = recordsFiltered, catalog = catalog }; return(searchResult); }