Example #1
0
        public async Task <IActionResult> List(string categoryName, int page = 1)
        {
            if (string.IsNullOrWhiteSpace(categoryName))
            {
                return(NotFound());
            }

            var pageSize    = _blogConfig.ContentSettings.PostListPageSize;
            var catResponse = await _categoryService.GetCategoryAsync(categoryName);

            if (!catResponse.IsSuccess)
            {
                return(ServerError($"Unsuccessful response: {catResponse.Message}"));
            }

            var cat = catResponse.Item;

            if (null == cat)
            {
                Logger.LogWarning($"Category '{categoryName}' not found.");
                return(NotFound());
            }

            ViewBag.CategoryDisplayName = cat.DisplayName;
            ViewBag.CategoryName        = cat.Name;
            ViewBag.CategoryDescription = cat.Note;

            var postCount = _postService.CountByCategoryId(cat.Id).Item;
            var postList  = await _postService.GetPagedPostsAsync(pageSize, page, cat.Id);

            var postsAsIPagedList = new StaticPagedList <PostListItem>(postList, page, pageSize, postCount);

            return(View(postsAsIPagedList));
        }
Example #2
0
        public async Task <IActionResult> CategoryList([FromServices] CategoryService categoryService, string routeName, int page = 1)
        {
            if (string.IsNullOrWhiteSpace(routeName))
            {
                return(NotFound());
            }

            var pageSize = _blogConfig.ContentSettings.PostListPageSize;
            var cat      = await categoryService.GetAsync(routeName);

            if (cat is null)
            {
                _logger.LogWarning($"Category '{routeName}' not found.");
                return(NotFound());
            }

            ViewBag.CategoryDisplayName = cat.DisplayName;
            ViewBag.CategoryRouteName   = cat.RouteName;
            ViewBag.CategoryDescription = cat.Note;

            var postCount = _cache.GetOrCreate(CacheDivision.PostCountCategory, cat.Id.ToString(),
                                               _ => _postService.CountByCategoryId(cat.Id));

            var postList = await _postService.GetPagedPostsAsync(pageSize, page, cat.Id);

            var postsAsIPagedList = new StaticPagedList <PostListEntry>(postList, page, pageSize, postCount);

            return(View(postsAsIPagedList));
        }