public CommonResult<IEnumerable<NewsData>> GetFiltered(NewsFilterRequest filterRequest) { var newsCollection = _newsProvider.GetAllWithoutContent(); if(filterRequest.Tags != null && filterRequest.Tags.Any()) { newsCollection = FilterByTags(filterRequest, newsCollection); } if (filterRequest.Authors != null && filterRequest.Authors.Any()) { newsCollection = FilterByAuthors(filterRequest, newsCollection); } if (filterRequest.Query.NotNullNorEmpty()) { newsCollection = FilterByQuery(filterRequest, newsCollection); } if (newsCollection.Any()) { return CommonResult<IEnumerable<NewsData>>.Success(newsCollection); } else { return CommonResult<IEnumerable<NewsData>>.Failure("Nie znaleziono newsów spełniających podane kryteria."); } }
private static IEnumerable<NewsData> FilterByAuthors(NewsFilterRequest filterRequest, IEnumerable<NewsData> newsCollection) { return newsCollection.Where(news => filterRequest.Authors.Contains(news.Author)); }
private static IEnumerable<NewsData> FilterByQuery(NewsFilterRequest filterRequest, IEnumerable<NewsData> newsCollection) { return newsCollection.Where(news => news.Title.ToLower().Contains(filterRequest.Query.ToLower())); }
private static IEnumerable<NewsData> FilterByTags(NewsFilterRequest filterRequest, IEnumerable<NewsData> newsCollection) { return newsCollection.Where(news => filterRequest.Tags.All(x => news.Tags.Contains(x))); }