Esempio n. 1
0
        public PagedList<Movie> GetPagedList(MovieFilter filter = null)
        {
            PagedList<Movie> result;

            if (filter == null)
            {
                filter = new MovieFilter();
            }

            _logProvider.LogDebug("Enter MoviesService.GetPagedList() with filter : " + filter);

            if (filter.SortBy.IsNotNullOrEmpty() && filter.SortBy.Equals(NotSortableAttribute))
            {
                throw new ArgumentException(NotSortableAttribute + " is not a sortable attribute");
            }

            try
            {
                // Get the cache key based on filter parameters
                var cacheKey = GetCacheKey(filter);

                var filteredMovies = _cacheProvider.GetCacheValue<List<Movie>>(cacheKey);

                if (filteredMovies == null)
                {
                    var allMovies = GetAllMovies();
                    var searchText = filter.SearchText;

                    if (searchText.IsNotNullOrEmpty() && filter.SearchBy.IsNotNullOrEmpty())
                    {
                        // Search by a property
                        filteredMovies = SearchByProperty(
                            allMovies,
                            filter.SearchBy,
                            searchText,
                            filter.IsCaseSensitive).ToList();
                    }
                    else
                    {
                        // Search across all properties
                        filteredMovies = allMovies
                            .Where(m => searchText.IsNullOrEmpty() // if SearchText is empty, no filter apply
                                        || m.MovieId.ToString() == searchText
                                        || m.Title.Contains(searchText, filter.IsCaseSensitive)
                                        || m.Classification.Contains(searchText, filter.IsCaseSensitive)
                                        || m.Genre.Contains(searchText)
                                        || filter.SearchText == m.Rating.ToString()
                                        || filter.SearchText == m.ReleaseDate.ToString())
                            .ToList();
                    }

                    _cacheProvider.SetCacheValue(
                        cacheKey,
                        filteredMovies,
                        null,
                        new TimeSpan(0, MovieDataCacheExpirationInHours, 0, 0));
                }

                result = Pager.Paginate(filteredMovies, new PagerSettings
                {
                    PageNumber = filter.CurrentPageIndex,
                    PageSize = filter.PageSize,
                    SortBy = filter.SortBy,
                    SortDirection = filter.SortOrder,
                    Paginate = true
                });

            }
            catch (Exception e)
            {
                _logProvider.LogException(e);

                throw new MoviesServiceException(e.Message);
            }

            return result;
        }
Esempio n. 2
0
 // Construct a cache key based on filter parameters
 private string GetCacheKey(MovieFilter filter)
 {
     return string.Format("{0}.SearchBy={1}.SearchText={2}.IsCaseSensitive={3}", BaseMovieDataCacheKey, filter.SearchBy, filter.SearchText, filter.IsCaseSensitive);
 }