Esempio n. 1
0
    public IActionResult Index([FromQuery] int page)
    {
        var authenticated = User.Identity?.IsAuthenticated ?? false;
        var viewModel     = _cacheClient.GetOrCreate(
            CacheKeyBuilder.Build(nameof(HomeController), nameof(Index), page, authenticated),
            () =>
        {
            var posts =
                _blogService.GetAllPosts().Where(x => x.Raw.IsPostPublished() || authenticated).ToList();
            var toppedPosts = posts.Where(x => x.Raw.IsTopping).ToList();
            foreach (var blogPost in toppedPosts)
            {
                posts.Remove(blogPost);
            }

            posts.InsertRange(0, toppedPosts);

            var postsPerPage = Convert.ToInt32(_blogOptions.PostsPerPage);
            var model        = new PagedViewModel <PostViewModel>(page, posts.Count, postsPerPage)
            {
                Url = Request.Path
            };

            foreach (var blogPost in posts.Chunk(postsPerPage).ElementAtOrDefault(model.CurrentPage - 1) ??
                     Enumerable.Empty <BlogPostRuntime>())
            {
                var postViewModel = new PostViewModel {
                    Current = blogPost
                };
                postViewModel.SetAdditionalInfo();
                model.Items.Add(postViewModel);
            }

            return(model);
        });

        if (viewModel.CurrentPage > 1)
        {
            ViewData["RobotsEnabled"] = false;
            ViewData["Title"]         = $"首页:第{viewModel.CurrentPage}页";
        }

        return(View(viewModel));
    }
    public IActionResult Post([FromRoute] string link)
    {
        var authenticated = User.Identity?.IsAuthenticated ?? false;
        var viewModel     = _cacheClient.GetOrCreate(
            CacheKeyBuilder.Build(nameof(HomeController), nameof(Post), authenticated, link),
            () =>
        {
            var post = _blogService.GetAllPosts().FirstOrDefault(x =>
                                                                 StringUtil.EqualsIgnoreCase(x.Raw.Link, link) &&
                                                                 (x.Raw.IsPostPublished() || authenticated));
            if (post == null)
            {
                return(null);
            }

            var previousPost = _blogService.GetAllPosts()
                               .FirstOrDefault(x => x.Raw.PublishTime < post.Raw.PublishTime);
            var nextPost = _blogService.GetAllPosts()
                           .LastOrDefault(x => x.Raw.PublishTime > post.Raw.PublishTime);
            var model = new PostViewModel
            {
                Current  = post,
                Previous = previousPost,
                Next     = nextPost
            };
            model.SetAdditionalInfo();
            return(model);
        });

        if (viewModel == null)
        {
            return(NotFound());
        }

        _blogService.EnqueuePostAccess(viewModel.Current.Raw.Link);
        return(View("Index", viewModel));
    }