Open Source Web Application Framework for ASP.NET Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

112 lines
3.5 KiB

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Volo.Blogging.Blogs;
using Volo.Blogging.Blogs.Dtos;
using Volo.Blogging.Comments;
using Volo.Blogging.Comments.Dtos;
using Volo.Blogging.Posts;
namespace Volo.Blogging.Pages.Blog.Posts
{
public class DetailModel : PageModel
{
private const int TwitterLinkLength = 23;
private readonly IPostAppService _postAppService;
private readonly IBlogAppService _blogAppService;
private readonly ICommentAppService _commentAppService;
[BindProperty(SupportsGet = true)]
public string BlogShortName { get; set; }
[BindProperty(SupportsGet = true)]
public string PostUrl { get; set; }
[BindProperty]
public PostDetailsViewModel NewComment { get; set; }
public int CommentCount { get; set; }
public PostWithDetailsDto Post { get; set; }
public IReadOnlyList<CommentWithRepliesDto> CommentsWithReplies { get; set; }
public BlogDto Blog { get; set; }
public DetailModel(IPostAppService postAppService, IBlogAppService blogAppService, ICommentAppService commentAppService)
{
_postAppService = postAppService;
_blogAppService = blogAppService;
_commentAppService = commentAppService;
}
public async Task OnGetAsync()
{
await GetData();
}
public async Task OnPostAsync()
{
await _commentAppService.CreateAsync(new CreateCommentDto()
{
RepliedCommentId = NewComment.RepliedCommentId,
PostId = NewComment.PostId,
Text = NewComment.Text
});
await GetData();
}
public async Task OnDeleteAsync(Guid commentId)
{
await _commentAppService.DeleteAsync(commentId);
await GetData();
}
private async Task GetData()
{
Blog = await _blogAppService.GetByShortNameAsync(BlogShortName);
Post = await _postAppService.GetForReadingAsync(new GetPostInput { BlogId = Blog.Id, Url = PostUrl });
CommentsWithReplies = await _commentAppService.GetHierarchicalListOfPostAsync(new GetCommentListOfPostAsync() { PostId = Post.Id });
CountComments();
}
public void CountComments()
{
CommentCount = CommentsWithReplies.Count;
foreach (var commentWithReply in CommentsWithReplies)
{
CommentCount += commentWithReply.Replies.Count;
}
}
public string GetTwitterShareUrl(string title, string url)
{
var readAtString = " | Read More At";
var linkedAccounts = "" ;
var otherCharsLength = (readAtString + linkedAccounts).Length;
var maxTitleLength = 280 - TwitterLinkLength - otherCharsLength;
title = title.Length < maxTitleLength ? title : title.Substring(0, maxTitleLength -3) + "...";
var text = title + readAtString + url + linkedAccounts;
var builder = new UriBuilder("https://twitter.com/intent/tweet") { Query = "text=" + HttpUtility.UrlEncode(text) };
return builder.ToString();
}
public class PostDetailsViewModel
{
public Guid? RepliedCommentId { get; set; }
public Guid PostId { get; set; }
public string Text { get; set; }
}
}
}