/// <summary> /// 发表评论 /// </summary> /// <param name="personId">PersonID</param> /// <param name="blogId">BlogID</param> /// <param name="content">评论内容</param> /// <param name="photoContentIds">评论附带图片的ID集合</param> /// <param name="baseCommentId">被评论的CommentID</param> /// <returns></returns> public async Task<CommentDTO> AddCommentAsync(long personId, long blogId, string content, List<long> photoContentIds = null, long? baseCommentId = null) { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { PersonHandler perHandler = new PersonHandler(dbContext); AvatarHandler avatarHandler = new AvatarHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); //1. 发表评论并返回Comment Entity对象。 var comment = await commentHandler.AddCommentAsync(personId, blogId, content, photoContentIds, baseCommentId); //2. 将Entity对象Convert为DTO对象。 var commentDto = comment.ToDTO(); //3. 判断Person对象是否为空,如果为空则获取。 if (commentDto.Person == null) { var personEntity = await perHandler.GetByIdAsync(comment.PersonID); if (personEntity != null) { commentDto.Person = personEntity.ToDTO(); //3.1 判断头像Url是否已经获取。 if (string.IsNullOrEmpty(commentDto.Person.AvatarUrl)) { commentDto.Person.AvatarUrl = await avatarHandler.GetActiveAvatarUrlByPersonId(comment.PersonID); } } } else { //3.2 如果Person对象不为空,判断头像Url是否已经获取。 if (string.IsNullOrEmpty(commentDto.Person.AvatarUrl)) { commentDto.Person.AvatarUrl = await avatarHandler.GetActiveAvatarUrlByPersonId(comment.PersonID); } } //4. 判断此评论是否评论了其他的评论。 if(comment.NewCommentXComments != null && comment.NewCommentXComments.Count > 0) { commentDto.BaseComment = comment.NewCommentXComments.First().BaseComment.ToDTO(); //4.1 判断Person对象是否为空,如果为空则获取,这里暂时不需要获取Avatar。 if (commentDto.BaseComment.Person == null) { var personEntity = await perHandler.GetByIdAsync(comment.NewCommentXComments.First().BaseComment.PersonID); if (personEntity != null) { commentDto.BaseComment.Person = personEntity.ToDTO(); } } } return commentDto; } }
/// <summary> /// 获取Blog的评论 /// </summary> /// <param name="blogId">BlogID</param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public async Task<Tuple<int, List<CommentDTO>>> GetCommentsAsync(long blogId, int pageIndex, int pageSize) { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { int totalCommentCount = 0; List<CommentDTO> commentDtoList = null; PersonHandler perHandler = new PersonHandler(dbContext); AvatarHandler avatarHandler = new AvatarHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); CommentXContentHandler cxContentHandler = new CommentXContentHandler(dbContext); CommentXCommentHandler cxCommentHandler = new CommentXCommentHandler(dbContext); //1. 获取Blog评论列表。 var comments = await commentHandler.GetCommentsAsync(blogId, pageIndex, pageSize); if (comments.Count > 0) { commentDtoList = new List<CommentDTO>(); foreach (var comment in comments) { CommentDTO commentDto = comment.ToDTO(); //2. 判断Person对象是否为空,如果为空则获取。 if (commentDto.Person == null) { var personEntity = await perHandler.GetByIdAsync(comment.PersonID); if (personEntity != null) { commentDto.Person = personEntity.ToDTO(); //2.1 判断头像Url是否已经获取。 if (string.IsNullOrEmpty(commentDto.Person.AvatarUrl)) { commentDto.Person.AvatarUrl = await avatarHandler.GetActiveAvatarUrlByPersonId(comment.PersonID); } } } else { //2.2 如果Person对象不为空,判断头像Url是否已经获取。 if (string.IsNullOrEmpty(commentDto.Person.AvatarUrl)) { commentDto.Person.AvatarUrl = await avatarHandler.GetActiveAvatarUrlByPersonId(comment.PersonID); } } //3. 判断Contents集合是否为空,如果为空则获取。 if (commentDto.Contents == null) { List<Content> contentList = await cxContentHandler.GetContentsAsync(comment.ID); if (contentList != null && contentList.Count > 0) { commentDto.Contents = new List<ContentDTO>(); foreach (var content in contentList) { ContentDTO contentDto = content.ToDTO(); commentDto.Contents.Add(contentDto); } } } //4. 判断此评论是否评论了其他的评论。 if (commentDto.BaseComment == null) { Comment baseComment = await cxCommentHandler.GetBaseCommentByCommentIdAsync(comment.ID); if (baseComment != null) { commentDto.BaseComment = baseComment.ToDTO(); //4.1 判断Person对象是否为空,如果为空则获取,这里暂时不需要获取Avatar。 if (commentDto.BaseComment.Person == null) { var personEntity = await perHandler.GetByIdAsync(baseComment.PersonID); if (personEntity != null) { commentDto.BaseComment.Person = personEntity.ToDTO(); } } } } //5. 获取评论点赞数量Task。 Task<int> likeCountTask = GetCommentLikeCountAsync(comment.ID); //6. 判断用户是否点赞了此评论Task。 Task<bool> isLikeTask = IsLikeAsync(CurrentThreadIdentityObject.PersonID, comment.ID); commentDto.IsLike = await isLikeTask; commentDto.LikeCount = await likeCountTask; commentDtoList.Add(commentDto); } //7. 获取Blog的总评论数量。 totalCommentCount = await GetCommentCountAsync(blogId); } return new Tuple<int, List<CommentDTO>>(totalCommentCount, commentDtoList); } }
/// <summary> /// 发布一篇Blog /// </summary> /// <param name="personId">PersonID</param> /// <param name="content">Blog的内容</param> /// <param name="attachContents">Blog的附件</param> /// <param name="accessInfo">Blog的访问控制</param> /// <param name="groupId">当Blog的访问控制为群可见则需要指定GroupID</param> /// <param name="forwardBlogId">转发的BlogID</param> /// <returns></returns> public async Task<BlogDTO> CreateBlogAsync(long personId, string content, BlogInfoAccessInfo accessInfo, long? groupId = null, List<long> attachContentIds = null, long? forwardBlogId = null) { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); PersonHandler perHandler = new PersonHandler(dbContext); AvatarHandler avatarHandler = new AvatarHandler(dbContext); //1. 发布Blog并返回Blog Entity对象。 var blog = await blogHandler.CreateBlogAsync(personId, content, accessInfo, groupId, attachContentIds, forwardBlogId); //2. 将Entity对象Convert为DTO对象。 var result = blog.ToDTO(); //3. 判断Person对象是否为空,如果为空则获取。 if(result.Person == null) { var personEntity = await perHandler.GetByIdAsync(blog.PersonID); if (personEntity != null) { result.Person = personEntity.ToDTO(); //3.1 判断头像Url是否已经获取。 if (string.IsNullOrEmpty(result.Person.AvatarUrl)) { result.Person.AvatarUrl = await avatarHandler.GetActiveAvatarUrlByPersonId(result.Person.ID); } } } else { //3.2 如果Person对象不为空,判断头像Url是否已经获取。 if (string.IsNullOrEmpty(result.Person.AvatarUrl)) { result.Person.AvatarUrl = await avatarHandler.GetActiveAvatarUrlByPersonId(result.Person.ID); } } //4. 判断是否转发了Blog,转发了则获取转发Blog的信息。 if(blog.NewBlogXBlogs != null && blog.NewBlogXBlogs.Count > 0) { var baseBlogXblog = blog.NewBlogXBlogs.SingleOrDefault(x => x.IsBase); if(baseBlogXblog != null) { CommentHandler commentHandler = new CommentHandler(dbContext); BlogXBlogHandler bxbHandler = new BlogXBlogHandler(dbContext); EntityLikeHandler likeHandler = new EntityLikeHandler(dbContext); BlogXContentHandler bxcHandler = new BlogXContentHandler(dbContext); result.BaseBlog = baseBlogXblog.BaseBlog.ToDTO(); //4.1 判断转发了Blog的Person对象是否为空,如果空则获取。 if(result.BaseBlog.Person == null) { var personEntity = await perHandler.GetByIdAsync(baseBlogXblog.BaseBlog.PersonID); if (personEntity != null) { result.BaseBlog.Person = personEntity.ToDTO(); } } //4.2 判断转发了Blog的是否有图片等等。 List<Content> contentList = await bxcHandler.GetContentsAsync(result.BaseBlog.ID); if(contentList != null && contentList.Count > 0) { result.BaseBlog.Contents = new List<ContentDTO>(); foreach (var contentObj in contentList) { ContentDTO contentDto = contentObj.ToDTO(); result.BaseBlog.Contents.Add(contentDto); } } //4.3 获取转发的Blog的转发数量。 result.BaseBlog.RepostCount = await bxbHandler.GetRepostCountAsync(result.BaseBlog.ID); //4.4 获取转发的Blog的评论数量。 result.BaseBlog.CommentCount = await commentHandler.GetCommentCountAsync(result.BaseBlog.ID); //4.5 获取转发的Blog的点赞数量 result.BaseBlog.LikeCount = await likeHandler.GetBlogLikeCountAsync(result.BaseBlog.ID); //4.6 获取转发的Blog是否已经点赞。 result.BaseBlog.IsLike = await likeHandler.IsLikeAsync(personId, result.BaseBlog.ID, typeof(Blog)); } } return result; } }
/// <summary> /// 获取用户关注的人Blog /// </summary> /// <param name="personId">PersonID</param> /// <param name="groupId">GroupID</param> /// <param name="pageIndex">页码</param> /// <param name="pageSize">数量/页</param> /// <returns></returns> public async Task<List<BlogDTO>> GetBlogsAsync(long personId, long? groupId, int pageIndex, int pageSize) { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { List<BlogDTO> blogDtoList = null; BlogHandler blogHandler = new BlogHandler(dbContext); //1. 获取正在关注的人Blog集合。 var blogs = await blogHandler.GetBlogsAsync(personId, groupId, pageIndex, pageSize); if (blogs.Count > 0) { PersonHandler perHandler = new PersonHandler(dbContext); AvatarHandler avatarHandler = new AvatarHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); BlogXBlogHandler bxbHandler = new BlogXBlogHandler(dbContext); EntityLikeHandler likeHandler = new EntityLikeHandler(dbContext); BlogXContentHandler bxcHandler = new BlogXContentHandler(dbContext); blogDtoList = new List<BlogDTO>(); foreach (var blog in blogs) { BlogDTO blogDto = blog.ToDTO(); //2. 判断Person对象是否为空,如果为空则获取。 if (blogDto.Person == null) { var personEntity = await perHandler.GetByIdAsync(blog.PersonID); if (personEntity != null) { blogDto.Person = personEntity.ToDTO(); //3.1 判断头像Url是否已经获取。 if (string.IsNullOrEmpty(blogDto.Person.AvatarUrl)) { blogDto.Person.AvatarUrl = await avatarHandler.GetActiveAvatarUrlByPersonId(blogDto.Person.ID); } } } else { //2.2 如果Person对象不为空,判断头像Url是否已经获取。 if (string.IsNullOrEmpty(blogDto.Person.AvatarUrl)) { blogDto.Person.AvatarUrl = await avatarHandler.GetActiveAvatarUrlByPersonId(blogDto.Person.ID); } } //3. 判断Contents集合是否为空,如果为空则获取。 if (blogDto.Contents == null) { List<Content> contentList = await bxcHandler.GetContentsAsync(blogDto.ID); if (contentList != null && contentList.Count > 0) { blogDto.Contents = new List<ContentDTO>(); foreach (var content in contentList) { ContentDTO contentDto = content.ToDTO(); blogDto.Contents.Add(contentDto); } } } //4. 判断此Blog是否转发了其他Blog。 if(blogDto.BaseBlog == null) { Blog baseBlog = await bxbHandler.GetBaseBlogByBlogIdAsync(blogDto.ID); if(baseBlog != null) { blogDto.BaseBlog = baseBlog.ToDTO(); //4.1 判断转发的Blog的Person对象是否为空,如果为空则获取。不需要获取头像。 if (blogDto.BaseBlog.Person == null) { var personEntity = await perHandler.GetByIdAsync(baseBlog.PersonID); if (personEntity != null) { blogDto.BaseBlog.Person = personEntity.ToDTO(); } } //4.2 判断转发的Blog是否有发Contents。 if (blogDto.BaseBlog.Contents == null) { List<Content> contentList = await bxcHandler.GetContentsAsync(blogDto.BaseBlog.ID); if (contentList != null && contentList.Count > 0) { blogDto.BaseBlog.Contents = new List<ContentDTO>(); foreach (var content in contentList) { ContentDTO contentDto = content.ToDTO(); blogDto.BaseBlog.Contents.Add(contentDto); } } } //4.3 获取转发的Blog的转发数量。 blogDto.BaseBlog.RepostCount = await bxbHandler.GetRepostCountAsync(blogDto.BaseBlog.ID); //4.4 获取转发的Blog的评论数量。 blogDto.BaseBlog.CommentCount = await commentHandler.GetCommentCountAsync(blogDto.BaseBlog.ID); //4.5 获取转发的Blog的点赞数量 blogDto.BaseBlog.LikeCount = await likeHandler.GetBlogLikeCountAsync(blogDto.BaseBlog.ID); //4.6 获取转发的Blog是否已经点赞。 blogDto.BaseBlog.IsLike = await likeHandler.IsLikeAsync(personId, blogDto.BaseBlog.ID, typeof(Blog)); } } //5. 获取用户是否收藏了Blog的Task。 Task<bool> isFavoriteTask = IsFavoriteAsync(personId, blog.ID); //6. 获取评论数量的Task。 CommentManager commentManager = new CommentManager(); Task<int> commentCountTask = commentManager.GetCommentCountAsync(blog.ID); //7. 获取转发数量的Task。 Task<int> repostCountTask = GetRepostCountAsync(blog.ID); //8. 获取点赞数量和用户是否已经点赞的Task。 Task<Tuple<int, bool>> likeObjTask = GetLikeObjectAsync(personId, blog.ID); blogDto.IsFavorite = await isFavoriteTask; blogDto.CommentCount = await commentCountTask; blogDto.RepostCount = await repostCountTask; //9. 获取点赞数量和用户是否点赞的对象Tuple,赋值。 Tuple<int, bool> likeObj = await likeObjTask; blogDto.IsLike = likeObj.Item2; blogDto.LikeCount = likeObj.Item1; blogDtoList.Add(blogDto); } } return blogDtoList; } }