Beispiel #1
0
        public ActionResult <IEnumerable <AuthorDto> > GetAuthors(
            [FromQuery] AuthorsResourseParameters authorsResourseParameters)
        {
            var authors = this.courseLibraryRepository.GetAuthors(authorsResourseParameters);

            return(Ok(this.mapper.Map <IEnumerable <AuthorDto> >(authors)));
        }
Beispiel #2
0
        public IEnumerable <Author> GetAuthors(AuthorsResourseParameters authorsResourseParameters)
        {
            if (authorsResourseParameters == null)
            {
                throw new ArgumentNullException(nameof(authorsResourseParameters));
            }
            string mainCategory = authorsResourseParameters.MainCategory;
            string searchQuery  = authorsResourseParameters.SearchQuery;

            if (string.IsNullOrEmpty(mainCategory) &&
                string.IsNullOrEmpty(searchQuery))
            {
                return(GetAuthors());
            }
            //DEFERRED EXECUTION
            var authors = _context.Authors as IQueryable <Author>;

            if (!string.IsNullOrEmpty(mainCategory))
            {
                authors = authors.Where(author => author.MainCategory == mainCategory.Trim());
            }
            if (!string.IsNullOrEmpty(searchQuery))
            {
                searchQuery = searchQuery.Trim();
                authors     = authors.Where(author => author.MainCategory.Contains(searchQuery) ||
                                            author.FirstName.Contains(searchQuery) ||
                                            author.LastName.Contains(searchQuery));
            }
            return(authors.ToList());
        }
Beispiel #3
0
        public IEnumerable <Author> GetAuthors(AuthorsResourseParameters authorsResourseParameters)
        {
            if (authorsResourseParameters is null)
            {
                throw new ArgumentNullException(nameof(authorsResourseParameters));
            }

            if (string.IsNullOrWhiteSpace(authorsResourseParameters.MainCategory) &&
                string.IsNullOrWhiteSpace(authorsResourseParameters.SearchQuery))
            {
                return(GetAuthors());
            }

            var collection = _context.Authors.AsQueryable <Author>();

            if (!string.IsNullOrWhiteSpace(authorsResourseParameters.MainCategory))
            {
                var mainCategory = authorsResourseParameters.MainCategory.Trim();
                collection = collection.Where(a => a.MainCategory == mainCategory);
            }

            if (!string.IsNullOrWhiteSpace(authorsResourseParameters.SearchQuery))
            {
                var searchQuery = authorsResourseParameters.SearchQuery.Trim();
                collection = collection.Where(a => a.MainCategory.Contains(searchQuery) ||
                                              a.FirstName.Contains(searchQuery) ||
                                              a.LastName.Contains(searchQuery));
            }

            return(collection.ToList());
        }
Beispiel #4
0
        [HttpHead]                                                                                       // Response only headers with any information
        public IActionResult GetAuthors([FromQuery] AuthorsResourseParameters authorsResourseParameters) // Must implement "From" annotation, because class it`s a complex parameter
        {
            /*
             *  https://localhost:5001/api/authors?maincategory=singing&SearchQuery=arnold or https://localhost:5001/api/authors
             */

            var authorEntities = this._repos.GetAuthors(authorsResourseParameters);
            var authorDtos     = this._mapper.Map <IEnumerable <AuthorDto> >(authorEntities);

            return(Ok(authorDtos)); // we can use 'new JsonResult(authors);' but will be returned a default Ok status code.
        }