public IEnumerable <Author> GetAuthors(AuthorParams pagination)
 {
     try
     {
         if (!pagination.IncludeBooks)
         {
             return(_context.Authors.
                    OrderBy(a => a.FirstName).
                    ThenBy(a => a.LastName)
                    .Skip(pagination.PageSize *
                          (pagination.NumberPages - 1)) //the amount of author that going to be skipped example: if the page
                    .Take(pagination.PageSize)          //number is 3 and page size 10 will be skipped the 20 author of the page 1 and 2
                    .ToList());
         }
         return(_context.Authors.
                OrderBy(a => a.FirstName).
                ThenBy(a => a.LastName)
                .Skip(pagination.PageSize *
                      (pagination.NumberPages - 1))
                .Include(b => b.Books)
                .ToList());//the amount of author that going to be skipped example: if the page
     }
     catch (Exception)
     {
         throw new Exception($"failed getting the author of {pagination.NumberPages}" +
                             $"maybe there are no more author to retrieve");
     }
 }
Exemple #2
0
        public IActionResult Authors(AuthorParams AuthParams)
        {
            //throw new Exception("random test exception ");
            var authors = Repo.GetAuthors(AuthParams);
            var Autho   = Mapper.Map <IEnumerable <Author>, IEnumerable <AuthorToReturn> >(authors);

            return(Ok(Autho));
        }
 public IEnumerable <Book> GetBooksForAuthor(Guid authorId, AuthorParams BookParams)
 {
     return(_context.Books
            .Where(b => b.AuthorId == authorId).OrderBy(b => b.Title)
            .Skip(BookParams.PageSize * (BookParams.NumberPages - 1))
            .Take(BookParams.PageSize)
            .ToList());
 }
        public IActionResult Books(Guid AuthorId, AuthorParams BooksParams)
        {
            if (!_Repo.AuthorExists(AuthorId))
            {
                return(NotFound());
            }

            var Book   = _Repo.GetBooksForAuthor(AuthorId, BooksParams);
            var BookDt = Mapper.Map <IEnumerable <BookToReturn> >(Book);

            return(Ok(BookDt));
        }
Exemple #5
0
        public async Task <IActionResult> GetAuthors([FromQuery] AuthorParams authorParams)
        {
            if (string.IsNullOrEmpty(authorParams.AuthorName))
            {
                authorParams.AuthorName = "ALL";
            }

            var authors = await _author_Repo.GetAuthors(authorParams);

            var authorsToReturn = _mapper.Map <IEnumerable <AuthorsForListDto> >(authors);

            Response.AddPagination(authors.CurrentPage, authors.PageSize, authors.TotalCount, authors.TotalPages);
            return(Ok(authorsToReturn));
        }
Exemple #6
0
        public async Task <PagedList <Author> > GetAuthors(AuthorParams authorParams)
        {
            var authors = _context.Authors.OrderByDescending(u => u.DateOfBirth).AsQueryable();


            if (authorParams.AuthorName == "ALL")
            {
                if (!string.IsNullOrEmpty(authorParams.OrderBy))
                {
                    switch (authorParams.OrderBy)
                    {
                    case "A-Z":
                        authors = authors.OrderBy(u => u.AuthorName);
                        break;

                    default:
                        authors = authors.OrderByDescending(u => u.DateOfBirth);
                        break;
                    }
                }
                return(await PagedList <Author> .CreateAsync(authors, authorParams.PageNumber, authorParams.PageSize));
            }
            else
            {
                authors = authors.Where(p => p.AuthorName.ToLower().Contains(authorParams.AuthorName.ToLower()));
                if (!string.IsNullOrEmpty(authorParams.OrderBy))
                {
                    switch (authorParams.OrderBy)
                    {
                    case "A-Z":
                        authors = authors.OrderBy(u => u.AuthorName);
                        break;

                    default:
                        authors = authors.OrderByDescending(u => u.DateOfBirth);
                        break;
                    }
                }
                return(await PagedList <Author> .CreateAsync(authors, authorParams.PageNumber, authorParams.PageSize));
            }
        }