Пример #1
0
        private string GetCacheKeyForRatingsQuery(RatingsQuery query)
        {
            string key = CacheKeys.RatingsList.ToString();

            if (query.MovieId.HasValue && query.MovieId > 0)
            {
                key = string.Concat(key, "_", query.MovieId.Value);
            }

            key = string.Concat(key, "_", query.Page, "_", query.ItemsPerPage);
            return(key);
        }
Пример #2
0
        public async Task <QueryResult <Rating> > ListAsync(RatingsQuery query)
        {
            IQueryable <Rating> queryable = _context.Ratings
                                            .Include(p => p.MovieId)
                                            .AsNoTracking();           // AsNoTracking tells EF Core to skip tracking changes on listed entities improving performance.

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

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

            List <Rating> ratings = await queryable.Skip((query.Page - query.ItemsPerPage) *query.ItemsPerPage)
                                    .Take(query.ItemsPerPage)
                                    .ToListAsync(); // Calculate how many items to skip, according to the current page and amount of items per page, then return only the amount of desired items. The methods "Skip" and "Take" do the trick here.

            return(new QueryResult <Rating>         // Return a query result, containing all items and the amount of items in the database (necessary for client calculations of pages).
            {
                Items = ratings,
                TotalItems = totalItems,
            });
        }