Пример #1
0
        // -------------------------------------------------------------------- Private Methods

        /// <summary>
        /// Returns posts, total posts and post statuses.
        /// </summary>
        /// <param name="status">The post status <see cref="EPostStatus"/></param>
        /// <param name="pageNumber">Which page, 1-based.</param>
        /// <param name="pageSize">How many rows per page</param>
        /// <returns></returns>
        /// <remarks>
        /// I did a workaround for tabs, couldn't get "statuses" to work as the tab is not initially selected.
        /// </remarks>
        private async Task <PostListVM> GetPostListVmAsync(string status, int pageNumber, int pageSize)
        {
            var postList = status.Equals("published", StringComparison.InvariantCultureIgnoreCase) ?
                           await _blogSvc.GetPostsAsync(pageNumber, pageSize) :
                           await _blogSvc.GetPostsForDraftsAsync(); // TODO drafts need pagination too

            var postVms = from p in postList.Posts
                          select new PostVM
            {
                Id       = p.Id,
                Title    = p.Title,
                Date     = p.CreatedOn.ToString("yyyy-MM-dd"),
                Author   = p.User.DisplayName,
                EditLink = BlogRoutes.GetPostEditLink(p.Id),
                PostLink = $"{Request.Scheme}://{Request.Host}" + BlogRoutes.GetPostRelativeLink(p.CreatedOn, p.Slug),
            };

            var postCount = await _blogSvc.GetPostCountAsync();

            // prep vm
            return(new PostListVM
            {
                Posts = postVms,
                TotalPosts = postList.PostCount,
                PublishedCount = postCount.Published,
                DraftCount = postCount.Draft,
            });
        }
Пример #2
0
        /// <summary>
        /// Returns posts, total posts and post statuses.
        /// </summary>
        /// <param name="status">The post status <see cref="EPostStatus"/></param>
        /// <param name="pageNumber">Which page, 1-based.</param>
        /// <param name="pageSize">How many rows per page</param>
        /// <returns></returns>
        /// <remarks>
        /// I did a workaround for tabs, couldn't get "statuses" to work as the tab is not initially selected.
        /// </remarks>
        private async Task <PostListVM> GetPostListVMAsync(string status, int pageNumber, int pageSize)
        {
            var postList = status.Equals("published", StringComparison.InvariantCultureIgnoreCase) ?
                           await blogPostService.GetListAsync(pageNumber, pageSize, cacheable : false) :
                           await blogPostService.GetListForDraftsAsync(); // TODO drafts need pagination too

            var coreSettings = await settingService.GetSettingsAsync <CoreSettings>();

            var postVms = from p in postList.Posts
                          select new PostVM
            {
                Id        = p.Id,
                Title     = p.Title,
                Date      = p.CreatedOn.ToLocalTime(coreSettings.TimeZoneId).ToString(POST_DATE_STRING_FORMAT),
                Author    = p.User.DisplayName,
                Category  = p.CategoryTitle,
                EditLink  = BlogRoutes.GetPostEditLink(p.Id),
                PostLink  = $"{Request.Scheme}://{Request.Host}" + BlogRoutes.GetPostRelativeLink(p.CreatedOn, p.Slug),
                ViewCount = p.ViewCount,
            };

            var postCount = await statsService.GetPostCountAsync();

            // prep vm
            return(new PostListVM
            {
                Posts = postVms,
                TotalPosts = postList.TotalPostCount,
                PublishedCount = postCount.Published,
                DraftCount = postCount.Draft,
            });
        }
Пример #3
0
        public void GetPostEditLink_returns_relative_url_starts_with_slash()
        {
            var postId   = 1;
            var editLink = BlogRoutes.GetPostEditLink(postId);

            Assert.Equal("/admin/compose/1", editLink);
        }
Пример #4
0
        public async Task <BlogPostVM> GetBlogPostVMAsync(BlogPost blogPost)
        {
            var coreSettings = await settingService.GetSettingsAsync <CoreSettings>();

            var blogSettings = await settingService.GetSettingsAsync <BlogSettings>();

            var permalinkPart    = BlogRoutes.GetPostPermalink(blogPost.Id);
            var relativeLink     = BlogRoutes.GetPostRelativeLink(blogPost.CreatedOn, blogPost.Slug);
            var requestHostShort = request.Host.ToString().StartsWith("www.") ?
                                   request.Host.ToString().Remove(0, 4) :
                                   request.Host.ToString();
            var permalinkShort = $"{request.Scheme}://{requestHostShort}{permalinkPart}";

            var hash = "";

            if (blogPost.Tags.Count > 0)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < blogPost.Tags.Count; i++)
                {
                    var tag = blogPost.Tags[i];
                    sb.Append(tag.Slug.Replace("-", ""));
                    if (i < blogPost.Tags.Count - 1)
                    {
                        sb.Append(",");
                    }
                }
                hash = sb.ToString();
            }

            return(new BlogPostVM
            {
                Title = blogPost.Title,
                Body = blogPost.Body,
                Excerpt = blogPost.Excerpt,
                Author = blogPost.User.DisplayName,
                CreatedOn = blogPost.CreatedOn,
                CreatedOnDisplay = blogPost.CreatedOn.ToDisplayString(coreSettings.TimeZoneId),
                Tags = blogPost.Tags.OrderBy(t => t.Title).ToList(),
                Category = blogPost.Category,
                ViewCount = blogPost.ViewCount,

                RelativeLink = relativeLink,
                Permalink = $"{request.Scheme}://{request.Host}{permalinkPart}",
                CanonicalUrl = $"{request.Scheme}://{request.Host}{relativeLink}",
                EditLink = BlogRoutes.GetPostEditLink(blogPost.Id),

                DisqusPageIdentifier = $"{ECommentTargetType.BlogPost}_{blogPost.Id}",
                ShowDisqus = blogSettings.AllowComments && blogSettings.CommentProvider == ECommentProvider.Disqus && !blogSettings.DisqusShortname.IsNullOrEmpty(),
                DisqusShortname = blogSettings.DisqusShortname,

                TwitterShareLink = hash.IsNullOrEmpty() ?
                                   $"https://twitter.com/intent/tweet?text={blogPost.Title}&url={permalinkShort}" :
                                   $"https://twitter.com/intent/tweet?text={blogPost.Title}&url={permalinkShort}&hashtags={hash}",
                FacebookShareLink = $"https://www.facebook.com/sharer/sharer.php?u={permalinkShort}",
                LinkedInShareLink = $"http://www.linkedin.com/shareArticle?mini=true&url={permalinkShort}&title={blogPost.Title}",
            });
        }
Пример #5
0
        public BlogPostViewModel(BlogPost blogPost, BlogSettings blogSettings, HttpRequest request)
        {
            Title            = blogPost.Title;
            Body             = blogPost.Body;
            Excerpt          = blogPost.Excerpt;
            Author           = blogPost.User.DisplayName;
            CreatedOn        = blogPost.CreatedOn;
            CreatedOnDisplay = blogPost.CreatedOnDisplay;
            Tags             = blogPost.Tags.OrderBy(t => t.Title).ToList();
            Category         = blogPost.Category;

            RelativeLink = BlogRoutes.GetPostRelativeLink(CreatedOn, blogPost.Slug);
            var permalinkPart = BlogRoutes.GetPostPermalink(blogPost.Id);

            Permalink    = $"{request.Scheme}://{request.Host}{permalinkPart}";
            CanonicalUrl = $"{request.Scheme}://{request.Host}{RelativeLink}";
            EditLink     = BlogRoutes.GetPostEditLink(blogPost.Id);

            DisqusPageIdentifier = $"{ECommentTargetType.BlogPost}_{blogPost.Id}";
            ShowDisqus           = blogSettings.AllowComments && blogSettings.CommentProvider == ECommentProvider.Disqus && !blogSettings.DisqusShortname.IsNullOrEmpty();
            DisqusShortname      = blogSettings.DisqusShortname;

            var hash = "";

            if (blogPost.Tags.Count > 0)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < blogPost.Tags.Count; i++)
                {
                    var tag = blogPost.Tags[i];
                    sb.Append(tag.Slug.Replace("-", ""));
                    if (i < blogPost.Tags.Count - 1)
                    {
                        sb.Append(",");
                    }
                }
                hash = sb.ToString();
            }

            var requestHostShort = request.Host.ToString().StartsWith("www.") ?
                                   request.Host.ToString().Remove(0, 4) : request.Host.ToString();
            var permalinkShort = $"{request.Scheme}://{requestHostShort}{permalinkPart}";

            TwitterShareLink = hash.IsNullOrEmpty() ?
                               $"https://twitter.com/intent/tweet?text={Title}&url={permalinkShort}" :
                               $"https://twitter.com/intent/tweet?text={Title}&url={permalinkShort}&hashtags={hash}";
            FacebookShareLink = $"https://www.facebook.com/sharer/sharer.php?u={permalinkShort}";
            GoogleShareLink   = $"https://plus.google.com/share?url={permalinkShort}";
            LinkedInShareLink = $"http://www.linkedin.com/shareArticle?mini=true&url={permalinkShort}&title={Title}";
        }