Пример #1
0
        public async Task <QueryResult <Anime> > ListAsync(AnimesQuery query)
        {
            IQueryable <Anime> queryable = _context.Animes
                                           .Include(a => a.Category)
                                           .AsNoTracking();

            // AsNoTracking tells EF Core it doesn't need to track changes on listed entities.

            if (query.CategoryId.HasValue && query.CategoryId > 0)
            {
                queryable = queryable.Where(p => p.CategoryId == query.CategoryId);
            }

            //  Counts all items present in the database for the given query, to return as part of the pagination data.
            int totalItems = await queryable.CountAsync();

            // Returns only the amount of desired items.
            List <Anime> animes = await queryable.Skip((query.Page - 1) *query.ItemsPerPage)
                                  .Take(query.ItemsPerPage)
                                  .ToListAsync();

            // Return a query result, containing all items and the amount of items in the database.
            return(new QueryResult <Anime>
            {
                Items = animes,
                TotalItems = totalItems,
            });
        }
Пример #2
0
 public async Task <QueryResult <Anime> > ListAsync(AnimesQuery query)
 {
     return(await _animeRepository.ListAsync(query));
 }