public async Task OnGetAsync()
        {
            var allBlogs = await _blogService.GetAllBlogs();

            Count = allBlogs.Count();

            if (_paginationService.IsInvalidCurrentPage(CurrentPage, TotalPages))
            {
                CurrentPage = 1;
            }

            Blogs = _paginationService.PaginateList(allBlogs, CurrentPage, PageSize);
        }
Exemple #2
0
        public async Task <IActionResult> OnGetAsync()
        {
            if (string.IsNullOrWhiteSpace(Title))
            {
                if (_cache.TryGetValue(CacheKeys.Blogs, out IEnumerable <BlogItem> blogs))
                {
                    Count = blogs.Count();
                    if (_paginationService.IsInvalidCurrentPage(CurrentPage, TotalPages))
                    {
                        CurrentPage = 1;
                    }
                    Blogs = _paginationService.PaginateList(blogs, CurrentPage, PageSize);
                }
                else
                {
                    var allBlogs = await _blogService.GetActiveBlogs();

                    Count = allBlogs.Count();
                    if (_paginationService.IsInvalidCurrentPage(CurrentPage, TotalPages))
                    {
                        CurrentPage = 1;
                    }
                    Blogs = _paginationService.PaginateList(allBlogs, CurrentPage, PageSize);

                    var cacheOptions = new MemoryCacheEntryOptions
                    {
                        SlidingExpiration = TimeSpan.FromDays(1)
                    };

                    _cache.Set(CacheKeys.Blogs, allBlogs.ToList(), cacheOptions);
                }
            }
            else
            {
                CurrentBlog = await _blogService.GetBlogByTitle(Title.Replace('-', ' '));

                if (CurrentBlog is null)
                {
                    return(NotFound());
                }
            }

            Blogs ??= new List <BlogItem>();

            return(Page());
        }