Example #1
0
        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.");
            }
        }
Example #2
0
 private static IEnumerable<NewsData> FilterByAuthors(NewsFilterRequest filterRequest, IEnumerable<NewsData> newsCollection)
 {
     return newsCollection.Where(news => filterRequest.Authors.Contains(news.Author));
 }
Example #3
0
 private static IEnumerable<NewsData> FilterByQuery(NewsFilterRequest filterRequest, IEnumerable<NewsData> newsCollection)
 {
     return newsCollection.Where(news => news.Title.ToLower().Contains(filterRequest.Query.ToLower()));
 }
Example #4
0
 private static IEnumerable<NewsData> FilterByTags(NewsFilterRequest filterRequest, IEnumerable<NewsData> newsCollection)
 {
     return newsCollection.Where(news => filterRequest.Tags.All(x => news.Tags.Contains(x)));
 }