/// <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="blogId">BlogID</param> /// <returns></returns> public async Task<int> GetCommentCountAsync(long blogId) { using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { CommentHandler commentHandler = new CommentHandler(dbContext); return await commentHandler.GetCommentCountAsync(blogId); } }
public async Task Test_04_AddCommentAllowableCheck() { long eddyId = 0; long eastId = 0; long blogId = 0; using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); ContentHandler contentHandler = new ContentHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); //1. check allow follower comment. Person eddy = CreatePerson("TestEddy", "TestEddy", AllowablePersonForComment.FollowerOnly, true); Person east = CreatePerson("TestEast", "TestEast", AllowablePersonForComment.FollowerOnly, true); Blog testBlog = await blogHandler.CreateBlogAsync(eddy.ID, "TestCommentBlog", BlogInfoAccessInfo.MyselfOnly); bool isChecked = false; try { Comment comment = await commentHandler.AddCommentAsync(east.ID, testBlog.ID, "hey, i am new comment test."); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "由于用户设置,你无法回复评论。"); } Assert.IsTrue(isChecked); eddyId = eddy.ID; eastId = east.ID; blogId = testBlog.ID; } using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); ContentHandler contentHandler = new ContentHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); //1.1 eddy follow east, and test it again. Follow(eddyId, eastId); Comment comment_1 = await commentHandler.AddCommentAsync(eastId, blogId, "hey, i am new comment test."); Assert.IsNotNull(comment_1); } long alienId = 0; long paulId = 0; long fansBlogId = 0; using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); ContentHandler contentHandler = new ContentHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); //1. check allow fans comment. Person alien = CreatePerson("TestAlien", "TestAlien", AllowablePersonForComment.FansOnly, true); Person paul = CreatePerson("TestPaul", "TestPaul", AllowablePersonForComment.FollowerOnly, true); Blog testBlog = await blogHandler.CreateBlogAsync(alien.ID, "TestCommentBlog", BlogInfoAccessInfo.MyselfOnly); bool isChecked = false; try { Comment comment = await commentHandler.AddCommentAsync(paul.ID, testBlog.ID, "hey, i am new comment test."); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "由于用户设置,你无法回复评论。"); } Assert.IsTrue(isChecked); alienId = alien.ID; paulId = paul.ID; fansBlogId = testBlog.ID; } using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); ContentHandler contentHandler = new ContentHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); Follow(paulId, alienId); Comment comment_1 = await commentHandler.AddCommentAsync(paulId, fansBlogId, "hey, i am new comment test."); Assert.IsNotNull(comment_1); } using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); ContentHandler contentHandler = new ContentHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); //1. check allow fans comment. Person alien = CreatePerson("TestAlien", "TestAlien", AllowablePersonForComment.All, false); Person paul = CreatePerson("TestPaul", "TestPaul", AllowablePersonForComment.FollowerOnly, true); List<long> contentIds = new List<long>(); Content content = new Content() { ContentPath = "testPath", ContentBinary = new byte[] { 1, 3, 5 }, Type = ContentType.Photo, MimeType = "jpg" }; contentHandler.Add(content); contentHandler.SaveChanges(); contentIds.Add(content.ID); Blog testBlog = await blogHandler.CreateBlogAsync(alien.ID, "TestCommentBlog", BlogInfoAccessInfo.MyselfOnly); bool isChecked = false; try { Comment comment = await commentHandler.AddCommentAsync(paul.ID, testBlog.ID, "hey, i am new comment test.", photoContentIds: contentIds); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "由于用户设置,你回复评论无法添加图片。"); } Assert.IsTrue(isChecked); } }
public async Task Test_01_AddCommentWithoutContent() { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); GroupHandler groupHandler = new GroupHandler(dbContext); GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); Person sam = CreatePerson("TestSam", "TestSam", AllowablePersonForComment.All, true); Blog testBlog = await blogHandler.CreateBlogAsync(sam.ID, "TestCommentBlog", BlogInfoAccessInfo.MyselfOnly); Person shelly = CreatePerson("TestShelly", "TestShelly", AllowablePersonForComment.All, true); //1. normal test. Comment testComment = await commentHandler.AddCommentAsync(shelly.ID, testBlog.ID, "hello, i am a comment test"); Comment testNormalComment = await commentHandler.GetByIdAsync(testComment.ID); Assert.IsNotNull(testNormalComment); Assert.AreEqual(testNormalComment.PersonID, shelly.ID); Assert.AreEqual(testNormalComment.BlogID, testBlog.ID); Assert.AreEqual(testNormalComment.Content, "hello, i am a comment test"); //2. set error blog id and test it. bool isChecked = false; try { Comment testComment_1 = await commentHandler.AddCommentAsync(commentPerson.ID, 12313121, "hello, i am a comment test"); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "要评论的Blog不存在或者已经被删除"); } Assert.IsTrue(isChecked); //3. test black list comment. Group blackListGroup = new Group() { PersonID = sam.ID, Name = "TestBlackList", Type = GroupType.BlackList }; groupHandler.Add(blackListGroup); groupHandler.SaveChanges(); GroupMember GroupMemberByBlack = new GroupMember() { GroupID = blackListGroup.ID, PersonID = commentPerson.ID }; gmHandler.Add(GroupMemberByBlack); gmHandler.SaveChanges(); isChecked = false; try { Comment testComment_2 = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hello, i am a comment test 4 black list."); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "由于用户设置,你无法回复评论。"); } Assert.IsTrue(isChecked); //4. remove black list member. gmHandler.MarkAsDeleted(GroupMemberByBlack); gmHandler.SaveChanges(); } }
public async Task Test_03_AddCommentIncludeBaseComment() { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); ContentHandler contentHandler = new ContentHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); CommentXCommentHandler cxcHandler = new CommentXCommentHandler(dbContext); CommentXContentHandler ccHandler = new CommentXContentHandler(dbContext); Person rain = CreatePerson("TestRain", "TestRain", AllowablePersonForComment.All, true); Blog testBlog = await blogHandler.CreateBlogAsync(rain.ID, "TestCommentBlog", BlogInfoAccessInfo.MyselfOnly); //1. test normal. List<long> contentIds = new List<long>(); Content content = new Content() { ContentPath = "testBeCommentPath", ContentBinary = new byte[] { 1, 3, 5 }, Type = ContentType.Photo, MimeType = "jpg" }; contentHandler.Add(content); contentHandler.SaveChanges(); contentIds.Add(content.ID); Comment beComment = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hey, i am be comment test.", photoContentIds: contentIds); Comment test_beComment = await commentHandler.GetByIdAsync(beComment.ID); Assert.IsNotNull(test_beComment); Assert.AreEqual(test_beComment.PersonID, commentPerson.ID); Assert.AreEqual(test_beComment.BlogID, testBlog.ID); Assert.AreEqual(test_beComment.Content, "hey, i am be comment test."); Assert.AreEqual(test_beComment.CommentXContents.Count, 1); Assert.AreEqual(test_beComment.CommentXContents.First().Content.ContentPath, "testBeCommentPath"); Assert.AreEqual(test_beComment.CommentXContents.First().Content.Type, ContentType.Photo); Assert.AreEqual(test_beComment.CommentXContents.First().Content.ContentBinary, new byte[] { 1, 3, 5 }); contentIds = new List<long>(); Content newCommentContent = new Content() { ContentPath = "testBeCommentPath", ContentBinary = new byte[] { 1, 3, 5 }, Type = ContentType.Photo, MimeType = "jpg" }; contentHandler.Add(newCommentContent); contentHandler.SaveChanges(); contentIds.Add(newCommentContent.ID); Comment newComment = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hey, i am new comment test.", photoContentIds: contentIds, baseCommentId: test_beComment.ID); Comment test_newComment = await commentHandler.GetByIdAsync(newComment.ID); Assert.IsNotNull(test_newComment); Assert.AreEqual(test_newComment.PersonID, commentPerson.ID); Assert.AreEqual(test_newComment.BlogID, testBlog.ID); Assert.AreEqual(test_newComment.Content, "hey, i am new comment test."); Assert.AreEqual(test_newComment.CommentXContents.Count, 1); Assert.AreEqual(test_newComment.NewCommentXComments.Count, 1); Assert.AreEqual(test_newComment.CommentXContents.First().Content.ContentPath, "testBeCommentPath"); Assert.AreEqual(test_newComment.CommentXContents.First().Content.Type, ContentType.Photo); Assert.AreEqual(test_newComment.CommentXContents.First().Content.ContentBinary, new byte[] { 1, 3, 5 }); Assert.AreEqual(test_newComment.NewCommentXComments.First().BaseComment.ID, test_beComment.ID); Assert.AreEqual(test_newComment.NewCommentXComments.First().NewComment.ID, test_newComment.ID); Assert.AreEqual(test_newComment.NewCommentXComments.First().BaseComment.Content, "hey, i am be comment test."); Assert.AreEqual(test_newComment.NewCommentXComments.First().NewComment.Content, "hey, i am new comment test."); //2. delete a comment and test it. bool isChecked = false; try { long tmpCommentId = test_beComment.ID; //delete comment. //2.1 first, delete all navigation for comment. for (int i = 0; i < test_beComment.BaseCommentXComments.Count; i++) { cxcHandler.MarkAsDeleted(test_beComment.BaseCommentXComments.ElementAt(i)); } await cxcHandler.SaveChangesAsync(); for (int i = 0; i < test_beComment.CommentXContents.Count; i++) { ccHandler.MarkAsDeleted(test_beComment.CommentXContents.ElementAt(i)); } await ccHandler.SaveChangesAsync(); //2.2 then, delete comment. commentHandler.MarkAsDeleted(test_beComment); await commentHandler.SaveChangesAsync(); Comment test_notExistComment = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hey, i am new comment test.", baseCommentId: tmpCommentId); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "此评论不存在或者已经被删除"); } Assert.IsTrue(isChecked); } }
public async Task Test_02_AddCommentIncludeContent() { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); ContentHandler contentHandler = new ContentHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); Person jay = CreatePerson("TestJay", "TestJay", AllowablePersonForComment.All, true); Blog testBlog = await blogHandler.CreateBlogAsync(jay.ID, "TestCommentBlog", BlogInfoAccessInfo.MyselfOnly); //1. normal test. List<long> contentIds = new List<long>(); Content content = new Content() { ContentPath = "testPath", ContentBinary = new byte[] { 1, 3, 5 }, Type = ContentType.Photo, MimeType = "jpg" }; contentHandler.Add(content); contentHandler.SaveChanges(); contentIds.Add(content.ID); Comment testComment = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hello, i am a comment include content", photoContentIds: contentIds); Comment testNormalComment = await commentHandler.GetByIdAsync(testComment.ID); Assert.IsNotNull(testNormalComment); Assert.AreEqual(testNormalComment.PersonID, commentPerson.ID); Assert.AreEqual(testNormalComment.BlogID, testBlog.ID); Assert.AreEqual(testNormalComment.Content, "hello, i am a comment include content"); Assert.AreEqual(testNormalComment.CommentXContents.Count, 1); Assert.AreEqual(testNormalComment.CommentXContents.First().Content.ContentPath, "testPath"); Assert.AreEqual(testNormalComment.CommentXContents.First().Content.Type, ContentType.Photo); Assert.AreEqual(testNormalComment.CommentXContents.First().Content.ContentBinary, new byte[] { 1, 3, 5 }); //2. test multiple content. contentIds = new List<long>(); for (int i = 0; i < 5; i++) { Content photoContent = new Content() { ContentPath = "testMultiplePath", ContentBinary = new byte[] { 3, 6, 9 }, Type = ContentType.Photo, MimeType = "jpg" }; contentHandler.Add(photoContent); contentHandler.SaveChanges(); contentIds.Add(photoContent.ID); } Comment testComment_1 = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hello, i am a multiple comment include content oh yes", photoContentIds: contentIds); Comment testMultipleComment = await commentHandler.GetByIdAsync(testComment_1.ID); Assert.IsNotNull(testMultipleComment); Assert.AreEqual(testMultipleComment.PersonID, commentPerson.ID); Assert.AreEqual(testMultipleComment.BlogID, testBlog.ID); Assert.AreEqual(testMultipleComment.Content, "hello, i am a multiple comment include content oh yes"); Assert.AreEqual(testMultipleComment.CommentXContents.Count, 5); Assert.AreEqual(testMultipleComment.CommentXContents.First().Content.ContentPath, "testMultiplePath"); Assert.AreEqual(testMultipleComment.CommentXContents.First().Content.Type, ContentType.Photo); Assert.AreEqual(testMultipleComment.CommentXContents.First().Content.ContentBinary, new byte[] { 3, 6, 9 }); //3. set the content type is video or music and test it. bool isChecked = false; try { contentIds = new List<long>(); for (int i = 0; i < 5; i++) { Content photoContent = new Content() { ContentPath = "testMultiplePath", ContentBinary = new byte[] { 3, 6, 9 }, Type = i % 2 == 0 ? ContentType.Music : ContentType.Video, MimeType = "jpg" }; contentHandler.Add(photoContent); contentHandler.SaveChanges(); contentIds.Add(photoContent.ID); } Comment testComment_2 = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hello, i am a multiple comment include content but type not photo", photoContentIds: contentIds); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "评论只能附件图片"); } Assert.IsTrue(isChecked); } }
/// <summary> /// 获取用户自己的Blog /// </summary> /// <param name="personId">PersonID</param> /// <param name="pageIndex">页索引</param> /// <param name="pageSize">页大小</param> /// <returns></returns> public async Task<List<BlogDTO>> GetOwnBlogs(long personId, int pageIndex, int pageSize) { using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { List<BlogDTO> blogDtoList = null; BlogHandler blogHandler = new BlogHandler(dbContext); //1. 获取用户的Blogs集合。 var blogs = await blogHandler.GetBlogsByPersonId(personId, pageIndex, pageSize); if(blogs.Count > 0) { PersonHandler perHandler = new PersonHandler(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. 判断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. 获取评论数量。 blogDto.CommentCount = await commentHandler.GetCommentCountAsync(blog.ID); //6. 获取转发数量。 blogDto.RepostCount = await bxbHandler.GetRepostCountAsync(blog.ID); //7. 获取点赞数量和用户是否已经点赞。 Tuple<int, bool> likeObj = await GetLikeObjectAsync(personId, blog.ID); blogDto.IsLike = likeObj.Item2; blogDto.LikeCount = likeObj.Item1; blogDtoList.Add(blogDto); } } return blogDtoList; } }
/// <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; } }