Ejemplo n.º 1
0
        public async Task <QueryResult <MovieList> > ListAsync(MovieListsQuery query)
        {
            IQueryable <MovieList> queryable = _context.MovieLists
                                               .Include(m => m.ApplicationUser)
                                               .Include(m => m.Movies);

            // AsNoTracking tells EF Core it doesn't need to track changes on listed entities. Disabling entity
            // tracking makes the code a little faster
            if (query.ApplicationUserRefId != null && query.ApplicationUserRefId.Length > 0)
            {
                queryable = queryable.Where(m => m.ApplicationUserRefId.Equals(query.ApplicationUserRefId));
            }

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

            // Here I apply a simple calculation to skip a given number of items, according to the current page and amount of items per page,
            // and them I return only the amount of desired items. The methods "Skip" and "Take" do the trick here.
            int skip    = (query.Page - 1) * query.ItemsPerPage;
            int display = Math.Min(totalItems - skip, query.ItemsPerPage);
            List <MovieList> movieLists = await queryable.Skip(skip)
                                          .Take(display)
                                          .ToListAsync();

            // Finally I return a query result, containing all items and the amount of items in the database (necessary for client calculations of pages).
            return(new QueryResult <MovieList>
            {
                Items = movieLists,
                TotalItems = totalItems,
            });
        }
Ejemplo n.º 2
0
        public async Task <MovieListResponse> ListAsync(int id, MovieListsQuery query)
        {
            var existingMovieList = await _movieListRepository.FindByIdAsync(id);

            if (existingMovieList == null || !(existingMovieList.ApplicationUserRefId.Equals(query.ApplicationUserRefId)))
            {
                return(new MovieListResponse("MovieList not found."));
            }

            return(new MovieListResponse(existingMovieList));
        }
Ejemplo n.º 3
0
        private string GetCacheKeyForMovieListsQuery(MovieListsQuery query)
        {
            string key = CacheKeys.MovieListsList.ToString();

            if (query.ApplicationUserRefId != null && query.ApplicationUserRefId.Length > 0)
            {
                key = string.Concat(key, "_", query.ApplicationUserRefId);
            }

            key = string.Concat(key, "_", query.Page, "_", query.ItemsPerPage);
            return(key);
        }
Ejemplo n.º 4
0
        public async Task <QueryResult <MovieList> > ListAsync(MovieListsQuery query)
        {
            // Here I list the query result from cache if they exist, but now the data can vary according to the category ID, page and amount of
            // items per page. I have to compose a cache to avoid returning wrong data.
            //string cacheKey = GetCacheKeyForMovieListsQuery(query);

            //var movieLists = await _cache.GetOrCreateAsync(cacheKey, (entry) => {
            //    entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1);
            //    return _movieListRepository.ListAsync(query);
            //});

            var movieLists = await _movieListRepository.ListAsync(query);

            return(movieLists);
        }
Ejemplo n.º 5
0
        public async Task <MovieListResponse> SaveAsync(MovieList movieList, MovieListsQuery query)
        {
            try
            {
                await _movieListRepository.AddAsync(movieList);

                await _unitOfWork.CompleteAsync();

                return(new MovieListResponse(movieList));
            }
            catch (Exception ex)
            {
                // Do some logging stuff
                return(new MovieListResponse($"An error occurred when saving the movie list: {ex.Message}"));
            }
        }
Ejemplo n.º 6
0
        public async Task <MovieListResponse> DeleteAsync(int id, MovieListsQuery query)
        {
            var existingMovieList = await _movieListRepository.FindByIdAsync(id);

            if (existingMovieList == null || !(existingMovieList.ApplicationUserRefId.Equals(query.ApplicationUserRefId)))
            {
                return(new MovieListResponse("MovieList not found."));
            }

            try
            {
                _movieListRepository.Remove(existingMovieList);
                await _unitOfWork.CompleteAsync();

                return(new MovieListResponse(existingMovieList));
            }
            catch (Exception ex)
            {
                // Do some logging stuff
                return(new MovieListResponse($"An error occurred when deleting the movie list: {ex.Message}"));
            }
        }