Example #1
0
        public async Task <IActionResult> Index(int page = 1)
        {
            var pagesize = _blogConfig.ContentSettings.PostListPageSize;
            var posts    = await _postService.List(pagesize, page);

            var count = _cache.GetOrCreate(CacheDivision.General, "postcount", _ => _postService.CountVisible());

            var list = new StaticPagedList <PostDigest>(posts, page, pagesize, count);

            return(View(list));
        }
Example #2
0
        public IActionResult Avatar([FromServices] IBlogCache cache)
        {
            var fallbackImageFile = Path.Join($"{_env.WebRootPath}", "images", "default-avatar.png");

            if (string.IsNullOrWhiteSpace(_blogConfig.GeneralSettings.AvatarBase64))
            {
                return(PhysicalFile(fallbackImageFile, "image/png"));
            }

            try
            {
                return(cache.GetOrCreate(CacheDivision.General, "avatar", entry =>
                {
                    Logger.LogTrace("Avatar not on cache, getting new avatar image...");
                    var avatarBytes = Convert.FromBase64String(_blogConfig.GeneralSettings.AvatarBase64);
                    return File(avatarBytes, "image/png");
                }));
            }
            catch (FormatException e)
            {
                Logger.LogError($"Error {nameof(Avatar)}(), Invalid Base64 string", e);
                return(PhysicalFile(fallbackImageFile, "image/png"));
            }
            catch (Exception ex)
            {
                Logger.LogError($"Error {nameof(Avatar)}()", ex);
                return(new EmptyResult());
            }
        }
Example #3
0
        public async Task <IActionResult> List(string routeName, int page = 1)
        {
            if (string.IsNullOrWhiteSpace(routeName))
            {
                return(NotFound());
            }

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

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

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

            var postCount = _blogCache.GetOrCreate(CacheDivision.PostCountCategory, cat.Id.ToString(),
                                                   entry => _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));
        }
Example #4
0
        public async Task <IActionResult> OnGetAsync(string routeName)
        {
            if (string.IsNullOrWhiteSpace(routeName))
            {
                return(NotFound());
            }

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

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

            ViewData["CategoryDisplayName"] = cat.DisplayName;
            ViewData["CategoryRouteName"]   = cat.RouteName;
            ViewData["CategoryDescription"] = cat.Note;

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

            var postList = await _postQueryService.ListAsync(pageSize, P, cat.Id);

            Posts = new(postList, P, pageSize, postCount);
            return(Page());
        }
Example #5
0
        public async Task OnGet(int p = 1)
        {
            var pagesize = _blogConfig.ContentSettings.PostListPageSize;
            var posts    = await _postQueryService.ListFeaturedAsync(pagesize, p);

            var count = _cache.GetOrCreate(CacheDivision.PostCountFeatured, "featured", _ => _postQueryService.CountByFeatured());

            var list = new StaticPagedList <PostDigest>(posts, p, pagesize, count);

            Posts = list;
        }
Example #6
0
        public async Task <IActionResult> Index(int page = 1)
        {
            try
            {
                var pagesize = _blogConfig.ContentSettings.PostListPageSize;
                var postList = await _postService.GetPagedPostsAsync(pagesize, page);

                var postCount = _cache.GetOrCreate(CacheDivision.General, "postcount", entry => _postService.CountVisiblePosts());

                var postsAsIPagedList = new StaticPagedList <PostListEntry>(postList, page, pagesize, postCount);
                return(View(postsAsIPagedList));
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Error getting post list.");
                return(ServerError("Error getting post list."));
            }
        }
Example #7
0
        public async Task <IActionResult> OnGet(string normalizedName)
        {
            var tagResponse = _tagService.Get(normalizedName);

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

            var pagesize = _blogConfig.ContentSettings.PostListPageSize;
            var posts    = await _postQueryService.ListByTagAsync(tagResponse.Id, pagesize, P);

            var count = _cache.GetOrCreate(CacheDivision.PostCountTag, tagResponse.Id.ToString(), _ => _postQueryService.CountByTag(tagResponse.Id));

            ViewData["TitlePrefix"] = tagResponse.DisplayName;

            var list = new StaticPagedList <PostDigest>(posts, P, pagesize, count);

            Posts = list;

            return(Page());
        }