示例#1
0
        public IEnumerable <Article> GetAllArticles(ArticlesResourceParameters articlesResourceParameters)
        {
            if (articlesResourceParameters == null)
            {
                throw new ArgumentNullException(nameof(articlesResourceParameters));
            }

            var collection = _context.Articles as IQueryable <Article>;

            if (!string.IsNullOrWhiteSpace(articlesResourceParameters.Title))
            {
                var title = articlesResourceParameters.Title.Trim();
                collection = collection.Where(article => article.Title == title);
            }

            if (!string.IsNullOrWhiteSpace(articlesResourceParameters.SearchQuery))
            {
                var searchQuery = articlesResourceParameters.SearchQuery.Trim();
                collection = collection.Where(article => article.Title.Contains(searchQuery));
            }

            return(collection
                   .Skip(articlesResourceParameters.PageSize * (articlesResourceParameters.PageNumber - 1))
                   .Take(articlesResourceParameters.PageSize)
                   .ToList());
        }
        public ActionResult <IEnumerable <ArticleDto> > GetArticles([FromQuery] ArticlesResourceParameters articlesResourceParameters)
        {
            var articlesFromRepo = _articleLibraryRepository.GetAllArticles(articlesResourceParameters);

            return(Ok(_mapper.Map <IEnumerable <ArticleDto> >(articlesFromRepo)));
        }