Пример #1
0
        public void TestFixtureSetUp()
        {
            TestUtil.CleanUpData();

            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                PersonHandler perHandler = new PersonHandler(dbContext);

                testPerson = new Person();
                testPerson.RealName = "testRealName";
                testPerson.NickName = "testPerson";
                testPerson.RealNameAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                testPerson.SexualTrendAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                testPerson.MaritalStatusAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                testPerson.QQAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                testPerson.DOBAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                testPerson.BloodTypeAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                testPerson.HomePageAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                perHandler.Add(testPerson);
                perHandler.SaveChanges();

                commentPerson = new Person();
                commentPerson.RealName = "testCommentRealName";
                commentPerson.NickName = "testCommentPerson";
                commentPerson.RealNameAccessLevel = PersonInfoAccessInfo.All;
                commentPerson.SexualTrendAccessLevel = PersonInfoAccessInfo.All;
                commentPerson.MaritalStatusAccessLevel = PersonInfoAccessInfo.All;
                commentPerson.QQAccessLevel = PersonInfoAccessInfo.All;
                commentPerson.DOBAccessLevel = PersonInfoAccessInfo.All;
                commentPerson.BloodTypeAccessLevel = PersonInfoAccessInfo.All;
                commentPerson.HomePageAccessLevel = PersonInfoAccessInfo.All;
                perHandler.Add(commentPerson);
                perHandler.SaveChanges();
            }
        }
Пример #2
0
        /// <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;
            }
        }
Пример #3
0
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public async Task<SignOutStatus> SignOutAsync(string userName, string token)
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                UserAccountHandler uaHandler = new UserAccountHandler(dbContext);

                return await uaHandler.SignOutAsync(userName, token);
            }
        }
Пример #4
0
        /// <summary>
        /// 根据PersonID获取Person
        /// </summary>
        /// <param name="personId">PersonID</param>
        /// <returns></returns>
        public async Task<Person> GetPersonByIdAsync(long personId)
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                PersonHandler perHandler = new PersonHandler(dbContext);

                return await perHandler.GetByIdAsync(personId);                
            }
        }
Пример #5
0
        /// <summary>
        /// 重设密码
        /// </summary>
        /// <param name="email">邮箱</param>
        /// <param name="newPassword">新密码</param>
        /// <returns></returns>
        public async Task<bool> ResetPasswordAsync(string email, string newPassword)
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                UserAccountHandler uaHandler = new UserAccountHandler(dbContext);

                return await uaHandler.ResetPasswordAsync(email, newPassword);
            }
        }
Пример #6
0
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public async Task<Tuple<KoalaBlogIdentityObject, SignInStatus, string>> SignInAsync(string userName, string password, bool isPersistent)
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                UserAccountHandler uaHandler = new UserAccountHandler(dbContext);

                return await uaHandler.SignInAsync(userName, password, isPersistent);
            }
        }
Пример #7
0
        public async Task<string> GetPersonNickNameByUserAccountIDAsync(long userAccountID)
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                PersonHandler perHandler = new PersonHandler(dbContext);
                UserAccountXPersonHandler uaxpHandler = new UserAccountXPersonHandler(dbContext);

                //1. Get the UserAccountXPerson
                UserAccountXPerson uaxp = await uaxpHandler.LoadByUserAccountIDAsync(userAccountID);

                //2. Get the Person NickName
                return uaxp != null && uaxp.Person != null ? uaxp.Person.NickName : "";
            }
        }
Пример #8
0
        /// <summary>
        /// 删除单张图片
        /// </summary>
        /// <param name="contentId">ContentID</param>
        /// <returns></returns>
        public async Task DeleteSingleImageContentAsync(long contentId, string filePath)
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                using(var dbTransaction = dbContext.Database.BeginTransaction())
                {
                    try
                    {
                        ContentHandler contentHandler = new ContentHandler(dbContext);

                        //1. 根据ContentID获取Content对象。
                        var content = await contentHandler.GetByIdAsync(contentId);

                        if(content != null)
                        {
                            FileInfo fileInfo = new FileInfo(filePath + "\\" + Path.GetFileName(content.ContentPath));
                            
                            //2. 判断当前Content的图片路径是否存在图片,存在则删除图片。
                            if(fileInfo.Exists)
                            {
                                var deleteTask = Task.Factory.StartNew(() =>
                                                {
                                                    fileInfo.Delete();
                                                });

                                contentHandler.MarkAsDeleted(content);

                                await contentHandler.SaveChangesAsync();
                                await deleteTask;

                                //3. 刷新FileInfo对象。
                                fileInfo.Refresh();

                                //4. 如果图片依旧存在证明删除失败,回滚事务。
                                if(fileInfo.Exists)
                                {
                                    dbTransaction.Rollback();
                                }
                            }                     
                        }
                        dbTransaction.Commit();
                    }
                    catch (Exception)
                    {
                        dbTransaction.Rollback();                        
                    }
                }
            }
        }
Пример #9
0
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="email">邮箱</param>
        /// <returns></returns>
        public async Task<Tuple<UserAccount, RegisterStatus>> RegisterAsync(string userName, string password, string email)
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                UserAccountHandler uaHandler = new UserAccountHandler(dbContext);

                RegisterStatus registerStatus = RegisterStatus.Failure;

                UserAccount registerUser = await uaHandler.CreateAsync(userName, password, email);

                if (registerUser != null)
                {
                    registerStatus = RegisterStatus.Succeeded;
                }

                return new Tuple<UserAccount, RegisterStatus>(registerUser, registerStatus);
            }
        }
Пример #10
0
        public void TestFixtureSetUp()
        {
            TestUtil.CleanUpData();

            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                UserAccountHandler uaHandler = new UserAccountHandler(dbContext);

                testUA1 = new UserAccount();
                testUA1.UserName = "******";
                testUA1.PasswordSalt = "testSalt1";
                testUA1.Password = "******";
                testUA1.Email = "*****@*****.**";
                testUA1.LastLogon = DateTime.Now;
                testUA1.EmailConfirmed = true;
                testUA1.Status = UserAccount.STATUS_ACTIVE;
                uaHandler.Add(testUA1);
                uaHandler.SaveChanges();
            }
        }
Пример #11
0
        /// <summary>
        /// 创建图片Content
        /// </summary>
        /// <param name="contentPath">图片路径</param>
        /// <param name="mimeType">图片类型</param>
        /// <returns></returns>
        public async Task<List<ContentDTO>> CreateImagesContentAsync(List<Tuple<string, string>> imageInfos)
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                ContentHandler contentHandler = new ContentHandler(dbContext);

                //1. 根据ImageInfos集合创建Content对象。
                var contents = await contentHandler.CreateImagesContentAsync(imageInfos);

                List<ContentDTO> contentDTOs = new List<ContentDTO>();

                if(contents != null && contents.Count > 0)
                {                    
                    //2. 生成DTO对象。
                    foreach (var content in contents)
                    {
                        contentDTOs.Add(content.ToDTO());
                    }
                }

                return contentDTOs;
            }
        }
Пример #12
0
        public async Task Test_04_Normal_GetBlogsAsync()
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext);
                PersonHandler perHandler = new PersonHandler(dbContext);
                PersonXPersonHandler pxpHandler = new PersonXPersonHandler(dbContext);

                //1. test normal.
                bool isChecked = false;
                try
                {
                    List<Blog> invalidPersonIdBlogs = await blogHandler.GetBlogsAsync(999999999);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.Message, "该用户不存在");
                }
                Assert.IsTrue(isChecked);

                Person master = CreatePerson("TestMasterPer", "TestMasterMind");

                Person mary = CreatePerson("TestMary", "TestMary");
                Person nick = CreatePerson("TestNick", "TestNick");
                Person tony = CreatePerson("TestTony", "TestTony");

                //1. create some blog and test it.
                await blogHandler.CreateBlogAsync(master.ID, "TestContentByMaster", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(mary.ID, "TestContentByMaryOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(mary.ID, "TestContentByMaryTwo", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(nick.ID, "TestContentByNickOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(tony.ID, "TestContentByTonyOne", BlogInfoAccessInfo.All);

                Follow(master.ID, mary.ID, nick.ID, tony.ID);
                
                List<Blog> blogs = await blogHandler.GetBlogsAsync(master.ID);
                
                Assert.AreEqual(blogs.Count, 5);
                Assert.AreEqual(blogs.Count(x => x.PersonID == mary.ID), 2);
                Assert.AreEqual(blogs.Count(x => x.PersonID == nick.ID), 1);
                Assert.AreEqual(blogs.Count(x => x.PersonID == tony.ID), 1);
                Assert.AreEqual(blogs.Count(x => x.PersonID == master.ID), 1);

                //2. add a group and add some group member test it.
                Group masterGroup = new Group()
                {
                    PersonID = master.ID,
                    Name = "TestMasterGroup",
                    Type = GroupType.GroupList
                };
                groupHandler.Add(masterGroup);
                groupHandler.SaveChanges();

                GroupMember GroupMemberByMary = new GroupMember()
                {
                    GroupID = masterGroup.ID,
                    PersonID = mary.ID
                };
                GroupMember GroupMemberByNick = new GroupMember()
                {
                    GroupID = masterGroup.ID,
                    PersonID = nick.ID
                };
                gmHandler.Add(GroupMemberByMary);
                gmHandler.Add(GroupMemberByNick);
                gmHandler.SaveChanges();

                //3. test get blog by group.
                blogs = await blogHandler.GetBlogsAsync(master.ID, masterGroup.ID);

                Assert.AreEqual(blogs.Count, 4);
                Assert.AreEqual(blogs.Count(x => x.PersonID == mary.ID), 2);
                Assert.AreEqual(blogs.Count(x => x.PersonID == nick.ID), 1);
                Assert.AreEqual(blogs.Count(x => x.PersonID == master.ID), 1);

                //4. add shield group and test it.
                Person mike = CreatePerson("TestMike", "TestMike");
                Person yoyo = CreatePerson("TestYOYO", "TestYOYO");
                Person pipi = CreatePerson("TestPIPI", "TestPIPI");
                Person poko = CreatePerson("TestPoko", "TestPoko");

                Follow(master.ID, mike.ID, yoyo.ID, pipi.ID, poko.ID);

                await blogHandler.CreateBlogAsync(mike.ID, "TestContentByMikeOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(mike.ID, "TestContentByMikeTwo", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(yoyo.ID, "TestContentByYoyoOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(yoyo.ID, "TestContentByYoyoTwo", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(pipi.ID, "TestContentByPipiOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(poko.ID, "TestContentByPokoOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(poko.ID, "TestContentByPokoTwo", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(poko.ID, "TestContentByPokoThree", BlogInfoAccessInfo.All);

                Group masterShieldGroup = new Group()
                {
                    PersonID = master.ID,
                    Name = "TestMasterShield",
                    Type = GroupType.ShieldList
                };
                groupHandler.Add(masterShieldGroup);
                groupHandler.SaveChanges();

                GroupMember GroupMemberByPoko = new GroupMember()
                {
                    GroupID = masterShieldGroup.ID,
                    PersonID = poko.ID
                };

                gmHandler.Add(GroupMemberByPoko);
                gmHandler.SaveChanges();

                blogs = await blogHandler.GetBlogsAsync(master.ID);

                Assert.AreEqual(blogs.Count, 10);

                foreach (var blog in blogs)
                {
                    Assert.AreNotEqual(blog.PersonID, poko.ID);
                    Assert.AreNotEqual(blog.Content, "TestContentByPokoOne");
                    Assert.AreNotEqual(blog.Content, "TestContentByPokoTwo");
                    Assert.AreNotEqual(blog.Content, "TestContentByPokoThree");
                }

                //5. add group member to normal group and test it.
                GroupMember GroupMemberByPoko_Normal = new GroupMember()
                {
                    GroupID = masterGroup.ID,
                    PersonID = poko.ID
                };
                gmHandler.Add(GroupMemberByMary);

                blogs = await blogHandler.GetBlogsAsync(master.ID, masterGroup.ID);

                Assert.AreEqual(blogs.Count, 4);
                Assert.AreEqual(blogs.Count(x => x.PersonID == mary.ID), 2);
                Assert.AreEqual(blogs.Count(x => x.PersonID == nick.ID), 1);
                Assert.AreEqual(blogs.Count(x => x.PersonID == master.ID), 1);
            }
        }
Пример #13
0
        public async Task Test_01_CreateBlogWithoutContentAsync()
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext);
                BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext);

                //1. test normal.
                Blog newBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlog", BlogInfoAccessInfo.All, null);

                Blog testBlog_1 = await blogHandler.GetByIdAsync(newBlog.ID);
                BlogAccessControl testAC_1 = await acHandler.Fetch(x => x.BlogID == testBlog_1.ID).FirstOrDefaultAsync();
                
                Assert.IsNotNull(testBlog_1);
                Assert.IsNotNull(testAC_1);

                Assert.AreEqual(testBlog_1.PersonID, testPerson.ID);
                Assert.AreEqual(testBlog_1.Content, "testBlog");
                Assert.AreEqual(testAC_1.AccessLevel, BlogInfoAccessInfo.All);

                //2. set BlogInfoAccessInfo GroupOnly and set GroupID is null then check it.
                bool isChecked = false;
                try
                {
                    newBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogTwo", BlogInfoAccessInfo.GroupOnly, null);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(DisplayableException));
                    Assert.AreEqual(ex.Message, "未指定组");
                }
                Assert.IsTrue(isChecked);

                //3. set not exist GroupID. expect Exception is type of foreign key exception.
                isChecked = false;
                try
                {
                    newBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogThree", BlogInfoAccessInfo.GroupOnly, 9999);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreNotEqual(ex.GetType(), typeof(AssertException));
                }
                Assert.IsTrue(isChecked);
            }

            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext);
                BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext);

                //4. create test group object and test it.
                Group testGroup = await groupHandler.CreateGroupAsync(testPerson.ID, "testGroup", GroupType.GroupList);
                bool isChecked = false;
                try
                {
                    Blog testBlog4Group = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogFour", BlogInfoAccessInfo.GroupOnly, testGroup.ID);

                    Blog testBlog_2 = await blogHandler.GetByIdAsync(testBlog4Group.ID);
                    BlogAccessControl testAC_2 = await acHandler.Fetch(x => x.BlogID == testBlog_2.ID).FirstOrDefaultAsync();
                    BlogAccessControlXGroup testACXG = await acxgHandler.Fetch(x => x.GroupID == testGroup.ID && x.BlogAccessControlID == testAC_2.ID).FirstOrDefaultAsync();

                    Assert.IsNotNull(testBlog_2);
                    Assert.IsNotNull(testAC_2);
                    Assert.IsNotNull(testACXG);

                    Assert.AreEqual(testBlog_2.PersonID, testPerson.ID);
                    Assert.AreEqual(testBlog_2.Content, "testBlogFour");
                    Assert.AreEqual(testAC_2.AccessLevel, BlogInfoAccessInfo.GroupOnly);
                }
                catch (Exception)
                {
                    isChecked = true;
                }
                Assert.IsFalse(isChecked);
            }
        }
Пример #14
0
        public async Task Test_03_ForwardBlogAsync()
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                BlogXBlogHandler bxbHandler = new BlogXBlogHandler(dbContext);
                ContentHandler contentHandler = new ContentHandler(dbContext);
                BlogXContentHandler bxcHandler = new BlogXContentHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext);
                BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext);

                //1. without content and test it.
                Blog testBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testWithoutContentForwardBlog", BlogInfoAccessInfo.All, null);

                Blog beForwardBlog = await blogHandler.GetByIdAsync(testBlog.ID);

                Assert.IsNotNull(beForwardBlog);
                Assert.AreEqual(beForwardBlog.Content, "testWithoutContentForwardBlog");

                //create forward blog
                Blog testForwardBlog_NoContent = await blogHandler.CreateBlogAsync(testPerson.ID, "testForwardBlogNoContent", BlogInfoAccessInfo.All, null, null, beForwardBlog.ID);

                Blog testForwardBlog = await blogHandler.GetByIdAsync(testForwardBlog_NoContent.ID);

                Assert.IsNotNull(testForwardBlog);
                Assert.IsFalse(testForwardBlog.IsDeleted);
                Assert.AreEqual(testForwardBlog.Content, "testForwardBlogNoContent");
                Assert.AreEqual(testForwardBlog.NewBlogXBlogs.Count, 1);

                Assert.IsNotNull(testForwardBlog.NewBlogXBlogs.First().NewBlog);
                Assert.IsNotNull(testForwardBlog.NewBlogXBlogs.First().BaseBlog);

                Assert.AreEqual(testForwardBlog.NewBlogXBlogs.First().NewBlog.Content, "testForwardBlogNoContent");
                Assert.AreEqual(testForwardBlog.NewBlogXBlogs.First().BaseBlog.Content, "testWithoutContentForwardBlog");

                //2. set error forward blog id and test it.
                bool isChecked = false;
                try
                {
                    Blog testErrorForwardBlogID = await blogHandler.CreateBlogAsync(testPerson.ID, "testForwardBlogNoContent", BlogInfoAccessInfo.All, null, null, 99999);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(DisplayableException));
                    Assert.AreEqual(ex.Message, "该Blog不存在或者已经被删除");
                }
                Assert.IsTrue(isChecked);

                //3. include content and test it.
                List<long> contentIds = new List<long>();

                for (int i = 0; i < 6; i++)
                {
                    Content photo = new Content()
                    {
                        ContentPath = "testPhotoContentPath",
                        ContentBinary = new byte[] { 12, 3, 4, 5, 7 },
                        Type = ContentType.Photo,
                        MimeType = "jpg"
                    };
                    contentHandler.Add(photo);
                    contentHandler.SaveChanges();

                    contentIds.Add(photo.ID);
                }

                Blog testIncludeContentBeForwardBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testIncludeContentBeForwardBlog", BlogInfoAccessInfo.All, attachContentIds: contentIds);

                Blog testBeForwardBlog = await blogHandler.GetByIdAsync(testIncludeContentBeForwardBlog.ID);

                Assert.IsNotNull(testBeForwardBlog);
                Assert.IsFalse(testBeForwardBlog.IsDeleted);
                Assert.AreEqual(testBeForwardBlog.Content, "testIncludeContentBeForwardBlog");
                Assert.AreEqual(testBeForwardBlog.BlogXContents.Count, 6);
                Assert.IsTrue(testBeForwardBlog.BlogXContents.Any(x => x.BlogID == testBeForwardBlog.ID && x.Content.ContentPath == "testPhotoContentPath" && x.Content.Type == ContentType.Photo));

                //forward the blog.
                contentIds = new List<long>();

                Content video = new Content()
                {                    
                    ContentPath = "testVideoContentPath",
                    ContentBinary = new byte[] { 1, 3, 5, 6, 99 },
                    Type = ContentType.Video,
                    MimeType = "jpg"
                };
                contentHandler.Add(video);
                contentHandler.SaveChanges();

                contentIds.Add(video.ID);

                Blog testIncludeContentForwardBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testIncludeContentForwardBlog", BlogInfoAccessInfo.All, attachContentIds: contentIds, forwardBlogId: testBeForwardBlog.ID);

                Blog testForwardBlogIncludeContent = await blogHandler.GetByIdAsync(testIncludeContentForwardBlog.ID);

                Assert.IsNotNull(testForwardBlogIncludeContent);
                Assert.IsFalse(testForwardBlogIncludeContent.IsDeleted);
                Assert.AreEqual(testForwardBlogIncludeContent.Content, "testIncludeContentForwardBlog");
                Assert.AreEqual(testForwardBlogIncludeContent.BlogXContents.Count, 1);
                Assert.IsTrue(testForwardBlogIncludeContent.BlogXContents.Any(x => x.BlogID == testForwardBlogIncludeContent.ID && x.Content.ContentPath == "testVideoContentPath" && x.Content.Type == ContentType.Video));

                Assert.AreEqual(testForwardBlogIncludeContent.NewBlogXBlogs.Count, 1);

                Assert.IsNotNull(testForwardBlogIncludeContent.NewBlogXBlogs.First().NewBlog);
                Assert.IsNotNull(testForwardBlogIncludeContent.NewBlogXBlogs.First().BaseBlog);

                Assert.AreEqual(testForwardBlogIncludeContent.NewBlogXBlogs.First().NewBlog.Content, "testIncludeContentForwardBlog");
                Assert.AreEqual(testForwardBlogIncludeContent.NewBlogXBlogs.First().BaseBlog.Content, "testIncludeContentBeForwardBlog");

                Assert.AreEqual(testForwardBlogIncludeContent.NewBlogXBlogs.First().NewBlog.BlogXContents.Count, 1);
                Assert.AreEqual(testForwardBlogIncludeContent.NewBlogXBlogs.First().BaseBlog.BlogXContents.Count, 6);

                foreach (var newBlogContent in testForwardBlogIncludeContent.NewBlogXBlogs.First().NewBlog.BlogXContents)
                {
                    Assert.AreEqual(newBlogContent.Blog.ID, testForwardBlogIncludeContent.ID);
                    Assert.AreEqual(newBlogContent.Content.ContentPath, "testVideoContentPath");
                    Assert.AreEqual(newBlogContent.Content.ContentBinary, new byte[] { 1, 3, 5, 6, 99 });
                }

                foreach (var baseBlogContent in testForwardBlogIncludeContent.NewBlogXBlogs.First().BaseBlog.BlogXContents)
                {
                    Assert.AreEqual(baseBlogContent.Blog.ID, testBeForwardBlog.ID);
                    Assert.AreEqual(baseBlogContent.Content.ContentPath, "testPhotoContentPath");
                    Assert.AreEqual(baseBlogContent.Content.ContentBinary, new byte[] { 12, 3, 4, 5, 7 });
                }
            }
        }
Пример #15
0
        public async Task Test_02_CreateBlogIncludeContentAsync()
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                ContentHandler contentHandler = new ContentHandler(dbContext);
                BlogXContentHandler bxcHandler = new BlogXContentHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext);
                BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext);

                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);

                Stopwatch sw = Stopwatch.StartNew();

                //1. test normal.
                Blog testBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlog", BlogInfoAccessInfo.All, null, contentIds);

                var time = sw.ElapsedMilliseconds;

                Blog testBlog_1 = await blogHandler.GetByIdAsync(testBlog.ID);
                BlogAccessControl testAC_1 = await acHandler.Fetch(x => x.BlogID == testBlog_1.ID).FirstOrDefaultAsync();
                BlogXContent testBXC_1 = await bxcHandler.Fetch(x => x.BlogID == testBlog_1.ID).FirstOrDefaultAsync();
                Content testContent_1 = testBXC_1.Content;

                Assert.IsNotNull(testBlog_1);
                Assert.IsNotNull(testAC_1);
                Assert.IsNotNull(testBXC_1);
                Assert.IsNotNull(testContent_1);

                Assert.AreEqual(testBlog_1.PersonID, testPerson.ID);
                Assert.AreEqual(testBlog_1.Content, "testBlog");
                Assert.AreEqual(testAC_1.AccessLevel, BlogInfoAccessInfo.All);
                Assert.AreEqual(testContent_1.ContentPath, "testPath");
                Assert.AreEqual(testContent_1.ContentBinary, new byte[] { 1, 3, 5 });

                //2. create test group object and test it.
                contentIds = new List<long>();

                Content content1 = new Content()
                {                    
                    ContentPath = "testFilePathYesOrNo",
                    ContentBinary = new byte[] { 23, 31, 45, 78, 99 },
                    Type = ContentType.Video,
                    MimeType = "jpg"
                };
                contentHandler.Add(content1);
                contentHandler.SaveChanges();

                contentIds.Add(content1.ID);

                Group testGroup = await groupHandler.CreateGroupAsync(testPerson.ID, "testGroup", GroupType.GroupList);

                testBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogGroupOnly", BlogInfoAccessInfo.GroupOnly, testGroup.ID, contentIds);

                Blog testBlog_2 = await blogHandler.GetByIdAsync(testBlog.ID);
                BlogAccessControl testAC_2 = await acHandler.Fetch(x => x.BlogID == testBlog_2.ID).FirstOrDefaultAsync();
                BlogAccessControlXGroup testACXG_2 = await acxgHandler.Fetch(x => x.GroupID == testGroup.ID && x.BlogAccessControlID == testAC_2.ID).FirstOrDefaultAsync();
                BlogXContent testBXC_2 = await bxcHandler.Fetch(x => x.BlogID == testBlog_2.ID).FirstOrDefaultAsync();
                Content testContent_2 = testBXC_2.Content;

                Assert.IsNotNull(testBlog_2);
                Assert.IsNotNull(testAC_2);
                Assert.IsNotNull(testACXG_2);
                Assert.IsNotNull(testBXC_2);
                Assert.IsNotNull(testContent_2);

                Assert.AreEqual(testBlog_2.PersonID, testPerson.ID);
                Assert.AreEqual(testBlog_2.Content, "testBlogGroupOnly");
                Assert.AreEqual(testAC_2.AccessLevel, BlogInfoAccessInfo.GroupOnly);
                Assert.AreEqual(testContent_2.ContentPath, "testFilePathYesOrNo");
                Assert.AreEqual(testContent_2.ContentBinary, new byte[] { 23, 31, 45, 78, 99 });
                Assert.AreEqual(testContent_2.Type, ContentType.Video);    
            }

            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                ContentHandler contentHandler = new ContentHandler(dbContext);
                BlogXContentHandler bxcHandler = new BlogXContentHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext);
                BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext);

                List<long> contentIds = new List<long>();

                //3. create test group object but set BlogInfoAccessInfo other value.

                Content content1 = new Content()
                {
                    ContentPath = "testFilePathYesOrNo",
                    ContentBinary = new byte[] { 23, 31, 45, 78, 99 },
                    Type = ContentType.Video,
                    MimeType = "jpg"
                };
                contentHandler.Add(content1);
                contentHandler.SaveChanges();

                contentIds.Add(content1.ID);

                Group testGroup_1 = await groupHandler.CreateGroupAsync(testPerson.ID, "testGroupWithTest", GroupType.GroupList);

                Blog testBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogNotSetGroupOnly", BlogInfoAccessInfo.MyselfOnly, testGroup_1.ID, contentIds);

                Blog testBlog_3 = await blogHandler.GetByIdAsync(testBlog.ID);
                BlogAccessControl testAC_3 = await acHandler.Fetch(x => x.BlogID == testBlog_3.ID).FirstOrDefaultAsync();
                BlogAccessControlXGroup testACXG_3 = await acxgHandler.Fetch(x => x.GroupID == testGroup_1.ID && x.BlogAccessControlID == testAC_3.ID).FirstOrDefaultAsync();
                BlogXContent testBXC_3 = await bxcHandler.Fetch(x => x.BlogID == testBlog_3.ID).FirstOrDefaultAsync();
                Content testContent_3 = testBXC_3.Content;

                Assert.IsNotNull(testBlog_3);
                Assert.IsNotNull(testAC_3);
                Assert.IsNotNull(testBXC_3);
                Assert.IsNotNull(testContent_3);

                //it expect null because BlogInfoAccessInfo is not GroupOnly.
                Assert.IsNull(testACXG_3);

                Assert.AreEqual(testBlog_3.PersonID, testPerson.ID);
                Assert.AreEqual(testBlog_3.Content, "testBlogNotSetGroupOnly");
                Assert.AreEqual(testAC_3.AccessLevel, BlogInfoAccessInfo.MyselfOnly);
                Assert.AreEqual(testContent_3.ContentPath, "testFilePathYesOrNo");
                Assert.AreEqual(testContent_3.ContentBinary, new byte[] { 23, 31, 45, 78, 99 });
                Assert.AreEqual(testContent_3.Type, ContentType.Video);

                //4. create multiple content and test it.
                contentIds = new List<long>();

                for (int i = 0; i < 3; i++)
                {
                    Content c = new Content()
                    {
                        ContentPath = "testMultiplePath",
                        ContentBinary = new byte[] { 12, 3, 4, 5, 7 },
                        Type = ContentType.Video,
                        MimeType = "jpg"
                    };
                    contentHandler.Add(c);
                    contentHandler.SaveChanges();

                    contentIds.Add(c.ID);
                }
                
                bool isChecked = false;
                try
                {
                    Blog testBlog_MultipleContent_1 = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogMultipleContent1", BlogInfoAccessInfo.MyselfOnly, testGroup_1.ID, contentIds);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(DisplayableException));
                    Assert.AreEqual(ex.Message, "发表视频不能超过1个");
                }
                Assert.IsTrue(isChecked);

                //5. create multiple content but different type and test it.
                contentIds = new List<long>();

                Content cPhoto = new Content()
                {
                    ContentPath = "testMultiplePath",
                    ContentBinary = new byte[] { 12, 3, 4, 5, 7 },
                    Type = ContentType.Photo,
                    MimeType = "jpg"
                };
                Content cMusic = new Content()
                {
                    ContentPath = "testMultiplePath",
                    ContentBinary = new byte[] { 12, 3, 4, 5, 7 },
                    Type = ContentType.Photo,
                    MimeType = "jpg"
                };
                Content cVideo = new Content()
                {
                    ContentPath = "testMultiplePath",
                    ContentBinary = new byte[] { 12, 3, 4, 5, 7 },
                    Type = ContentType.Video,
                    MimeType = "jpg"
                };
                contentHandler.Add(cPhoto);
                contentHandler.Add(cMusic);
                contentHandler.Add(cVideo);
                contentHandler.SaveChanges();

                contentIds.Add(cPhoto.ID);
                contentIds.Add(cMusic.ID);
                contentIds.Add(cVideo.ID);

                isChecked = false;
                try
                {
                    Blog testBlog_MultipleContent_2 = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogMultipleContent1", BlogInfoAccessInfo.MyselfOnly, testGroup_1.ID, contentIds);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(DisplayableException));
                    Assert.AreEqual(ex.Message, "不能发不同类型内容的Blog");
                }
                Assert.IsTrue(isChecked);

                //6. create some photo content and test it.
                contentIds = new List<long>();

                for (int i = 0; i < 6; i++)
                {
                    Content photo = new Content()
                    {
                        ContentPath = "testPhotoContentPath",
                        ContentBinary = new byte[] { 12, 3, 4, 5, 7 },
                        Type = ContentType.Photo,
                        MimeType = "jpg"
                    };
                    contentHandler.Add(photo);
                    contentHandler.SaveChanges();

                    contentIds.Add(photo.ID);
                }

                Group testGroup_2 = await groupHandler.CreateGroupAsync(testPerson.ID, "testGroupWithTest", GroupType.GroupList);

                Blog testBlog_MultiplePhoto_3 = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogMultiplePhoto", BlogInfoAccessInfo.GroupOnly, testGroup_2.ID, contentIds);

                Blog testPhotoBlog = await blogHandler.GetByIdAsync(testBlog_MultiplePhoto_3.ID);
                BlogAccessControl testPhotoAC = await acHandler.Fetch(x => x.BlogID == testPhotoBlog.ID).FirstOrDefaultAsync();
                BlogAccessControlXGroup testPhotoACXG = await acxgHandler.Fetch(x => x.GroupID == testGroup_2.ID && x.BlogAccessControlID == testPhotoAC.ID).FirstOrDefaultAsync();
                List<BlogXContent> testPhotoBXC = await bxcHandler.Fetch(x => x.BlogID == testPhotoBlog.ID).ToListAsync();


                Assert.IsFalse(testPhotoBlog.IsDeleted);
                Assert.IsNotNull(testPhotoBlog);
                Assert.IsNotNull(testPhotoAC);
                Assert.IsNotNull(testPhotoACXG);
                Assert.IsNotNull(testPhotoBXC);

                Assert.AreEqual(testPhotoBXC.Count, 6);
                Assert.AreEqual(testPhotoBlog.PersonID, testPerson.ID);
                Assert.AreEqual(testPhotoBlog.Content, "testBlogMultiplePhoto");
                Assert.AreEqual(testPhotoAC.AccessLevel, BlogInfoAccessInfo.GroupOnly);

                foreach (var photoBXC in testPhotoBXC)
                {
                    Assert.IsNotNull(photoBXC.Content);
                    Assert.AreEqual(photoBXC.Content.Type, ContentType.Photo);
                    Assert.AreEqual(photoBXC.Content.ContentPath, "testPhotoContentPath");
                    Assert.AreEqual(photoBXC.Content.ContentBinary, new byte[] { 12, 3, 4, 5, 7 });
                }
            }
        }
Пример #16
0
        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);
            }
        }
Пример #17
0
        private void Follow(long selfPerId, params long[] followingIds)
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                PersonXPersonHandler pxpHandler = new PersonXPersonHandler(dbContext);

                foreach (var followingId in followingIds)
	            {
                    PersonXPerson pxp = new PersonXPerson()
                    {
                        FollowerID = selfPerId,
                        FollowingID = followingId
                    };
                    pxpHandler.Add(pxp);
	            }
                pxpHandler.SaveChanges();
            }
        }
Пример #18
0
        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();
            }
        }
Пример #19
0
        /// <summary>
        /// 获取评论的Content集合
        /// </summary>
        /// <param name="commentId">CommentID</param>
        /// <returns></returns>
        public async Task<List<Content>> GetCommentContentsAsync(long commentId)
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                CommentXContentHandler cxcHandler = new CommentXContentHandler(dbContext);

                return await cxcHandler.GetContentsAsync(commentId);
            }
        }
Пример #20
0
        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);
            }
        }
Пример #21
0
        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);
            }
        }
Пример #22
0
        public async Task Test_05_UnFollowAsync()
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                PersonHandler perHandler = new PersonHandler(dbContext);

                //1. test normal and confirm follow succeed.

                Person kity = CreatePerson("TestKity", "TestKity");
                Person judy = CreatePerson("TestJudy", "TestJudy");

                bool isSucceed = await perHandler.FollowAsync(kity.ID, judy.ID);

                kity = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == kity.ID);
                judy = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == judy.ID);

                Assert.IsTrue(isSucceed);
                Assert.AreEqual(kity.MyFollowingPersons.Count, 1);
                Assert.AreEqual(kity.MyFans.Count, 0);
                Assert.AreEqual(judy.MyFollowingPersons.Count, 0);
                Assert.AreEqual(judy.MyFans.Count, 1);

                //2. unfollow now.
                bool isUnFollowSucceed = await perHandler.UnFollowAsync(kity.ID, judy.ID);

                kity = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == kity.ID);
                judy = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == judy.ID);

                Assert.IsTrue(isUnFollowSucceed);
                Assert.AreEqual(kity.MyFollowingPersons.Count, 0);
                Assert.AreEqual(kity.MyFans.Count, 0);
                Assert.AreEqual(judy.MyFollowingPersons.Count, 0);
                Assert.AreEqual(judy.MyFans.Count, 0);

                //3. unfollow again, it will be do nothing.
                isUnFollowSucceed = await perHandler.UnFollowAsync(kity.ID, judy.ID);

                Assert.IsTrue(isUnFollowSucceed);
            }
        }
Пример #23
0
        public async Task Test_05_AccessControl_GetBlogsAsync()
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext);
                PersonHandler perHandler = new PersonHandler(dbContext);
                PersonXPersonHandler pxpHandler = new PersonXPersonHandler(dbContext);

                Person faker = CreatePerson("TestFaker", "TestFaker");
                Person marin = CreatePerson("TestMarin", "TestMarin");
                Person deft = CreatePerson("TestDeft", "TestDeft");

                Follow(faker.ID, marin.ID, deft.ID);

                //1. test access info MySelfOnly.
                await blogHandler.CreateBlogAsync(faker.ID, "TestContentByFakerOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(marin.ID, "TestContentByMarinOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(marin.ID, "TestContentByMarinTwo", BlogInfoAccessInfo.MyselfOnly);
                await blogHandler.CreateBlogAsync(deft.ID, "TestContentByDeftOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(deft.ID, "TestContentByDeftTwo", BlogInfoAccessInfo.MyselfOnly);

                List<Blog> blogs = await blogHandler.GetBlogsAsync(faker.ID);

                Assert.AreEqual(blogs.Count, 3);

                foreach (var blog in blogs)
                {
                    Assert.AreNotEqual(blog.Content, "TestContentByMarinTwo");
                    Assert.AreNotEqual(blog.Content, "TestContentByDeftTwo");
                }

                //2. test access info GroupOnly.
                Person paul = CreatePerson("TestPaul", "TestPaul");
                Person judy = CreatePerson("TestJudy", "TestJudy");
                Person anne = CreatePerson("TestAnne", "TestAnne");
                Person alisa = CreatePerson("TestAlisa", "TestAlisa");

                Follow(paul.ID, judy.ID, anne.ID, alisa.ID);

                //judy group not include paul.
                Group judyGroup = await groupHandler.CreateGroupAsync(judy.ID, "TestJudyGroup", GroupType.GroupList);

                //anne group include paul.
                Group anneGroup = await groupHandler.CreateGroupAsync(anne.ID, "TestAnneGroup", GroupType.GroupList);

                GroupMember groupMemberByAnne = new GroupMember()
                {
                    GroupID = anneGroup.ID,
                    PersonID = paul.ID
                };
                gmHandler.Add(groupMemberByAnne);
                gmHandler.SaveChanges();

                //alisa group include judy, anne but not paul.
                Group alisaGroup = await groupHandler.CreateGroupAsync(alisa.ID, "TestAlisaGroup", GroupType.GroupList);

                GroupMember groupMemberByAlisaOne = new GroupMember()
                {
                    GroupID = alisaGroup.ID,
                    PersonID = judy.ID
                };
                GroupMember groupMemberByAlisaTwo = new GroupMember()
                {
                    GroupID = alisaGroup.ID,
                    PersonID = anne.ID
                };

                gmHandler.Add(groupMemberByAlisaOne);
                gmHandler.Add(groupMemberByAlisaTwo);
                gmHandler.SaveChanges();

                await blogHandler.CreateBlogAsync(paul.ID, "TestContentByPaul", BlogInfoAccessInfo.MyselfOnly);
                await blogHandler.CreateBlogAsync(judy.ID, "TestContentByJudyOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(judy.ID, "TestContentByJudyTwo", BlogInfoAccessInfo.GroupOnly, judyGroup.ID);
                await blogHandler.CreateBlogAsync(anne.ID, "TestContentByAnne", BlogInfoAccessInfo.GroupOnly, anneGroup.ID);
                await blogHandler.CreateBlogAsync(alisa.ID, "TestContentByAlisa", BlogInfoAccessInfo.GroupOnly, alisaGroup.ID);

                blogs = await blogHandler.GetBlogsAsync(paul.ID);

                Assert.AreEqual(blogs.Count, 3);

                foreach (var blog in blogs)
                {
                    Assert.AreNotEqual(blog.Content, "TestContentByJudyTwo");
                    Assert.AreNotEqual(blog.Content, "TestContentByAlisa");
                }

                //3. test access info FriendOnly.
                Person sam = CreatePerson("TestSam", "TestSam");
                Person joan = CreatePerson("TestJoan", "TestJoan");
                Person lily = CreatePerson("TestLily", "TestLily");

                Follow(sam.ID, joan.ID, lily.ID);

                //3.1 test joan and sam is friend but not lily.
                Follow(joan.ID, sam.ID);

                await blogHandler.CreateBlogAsync(joan.ID, "TestContentByJoanOne", BlogInfoAccessInfo.FriendOnly);
                await blogHandler.CreateBlogAsync(joan.ID, "TestContentByJoanTwo", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(lily.ID, "TestContentByLilyOne", BlogInfoAccessInfo.FriendOnly);
                await blogHandler.CreateBlogAsync(lily.ID, "TestContentByLilyTwo", BlogInfoAccessInfo.FriendOnly);

                blogs = await blogHandler.GetBlogsAsync(sam.ID);

                Assert.AreEqual(blogs.Count, 2);

                foreach (var blog in blogs)
                {
                    Assert.AreNotEqual(blog.Content, "TestContentByLilyOne");
                    Assert.AreNotEqual(blog.Content, "TestContentByLilyTwo");
                }

                //3.2 now lily follow sam too.
                Follow(lily.ID, sam.ID);

                blogs = await blogHandler.GetBlogsAsync(sam.ID);

                Assert.AreEqual(blogs.Count, 4);
            }
        }
Пример #24
0
        /// <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);
            }
        }
Пример #25
0
        private Person CreatePerson(string realName, string nickName)
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                PersonHandler perHandler = new PersonHandler(dbContext);

                Person p = new Person();

                p = new Person();
                p.RealName = realName;
                p.NickName = nickName;
                p.RealNameAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                p.SexualTrendAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                p.MaritalStatusAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                p.QQAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                p.DOBAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                p.BloodTypeAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                p.HomePageAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                perHandler.Add(p);
                perHandler.SaveChanges();

                return p;
            }
        }
Пример #26
0
        /// <summary>
        /// Comment点赞或者取消赞
        /// </summary>
        /// <param name="personId">PersonID</param>
        /// <param name="blogId">BlogID</param>
        /// <returns></returns>
        public async Task<Tuple<bool, int>> LikeAsync(long personId, long commentId)
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                EntityLikeHandler entityLikeHandler = new EntityLikeHandler(dbContext);

                return await entityLikeHandler.LikeAsync(personId, commentId, typeof(Comment));
            }
        }
Пример #27
0
        /// <summary>
        /// 获取用户自己的Contents
        /// </summary>
        /// <param name="personId">PersonID</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns></returns>
        public async Task<Tuple<bool, List<ContentDTO>>> GetOwnContents(long personId, int pageIndex = 1, int pageSize = int.MaxValue)
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                List<ContentDTO> contentDtoList = null;

                BlogXContentHandler bxcHandler = new BlogXContentHandler(dbContext);

                //1. 获取Contents集合。
                var contentsTuple = await bxcHandler.GetContentsByPersonIdAsync(personId, pageIndex, pageSize);

                var contents = contentsTuple.Item2;

                if(contents != null && contents.Count > 0)
                {
                    contentDtoList = new List<ContentDTO>();

                    //2. 遍历生成DTO对象。
                    foreach (var content in contents)
                    {
                        contentDtoList.Add(content.ToDTO());
                    }
                }

                return new Tuple<bool,List<ContentDTO>>(contentsTuple.Item1, contentDtoList);
            }
        }
Пример #28
0
        /// <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);
            }
        }
Пример #29
0
        /// <summary>
        /// 获取评论的点赞数量
        /// </summary>
        /// <param name="commentId"></param>
        /// <returns></returns>
        public async Task<int> GetCommentLikeCountAsync(long commentId)
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                EntityLikeHandler likeHandler = new EntityLikeHandler(dbContext);

                return await likeHandler.GetCommentLikeCountAsync(commentId);
            }
        }
Пример #30
0
        public async Task Test_01_CreatePersonAsync()
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                PersonHandler perHandler = new PersonHandler(dbContext);
                UserAccountXPersonHandler uaxpHandler = new UserAccountXPersonHandler(dbContext);

                //1. Test normal create person
                Person p = await perHandler.CreatePersonAsync(testUA1);

                Assert.IsNotNull(p);
                Assert.AreEqual(p.NickName, "testUserAccount");
                Assert.AreEqual(p.Gender, null);
                Assert.AreEqual(p.RealNameAccessLevel, PersonInfoAccessInfo.MyselfOnly);
                Assert.AreEqual(p.SexualTrendAccessLevel, PersonInfoAccessInfo.MyselfOnly);
                Assert.AreEqual(p.DOBAccessLevel, PersonInfoAccessInfo.MyselfOnly);

                //2. Get the UserAccountXPerson and test
                UserAccountXPerson uaxp = await uaxpHandler.LoadByUserAccountIDAsync(testUA1.ID);
                Assert.IsNotNull(uaxp);
                Assert.AreEqual(uaxp.PersonID, p.ID);
                Assert.AreEqual(uaxp.UserAccountID, testUA1.ID);

                //3. Give the null parameter and check it.
                bool isChecked = false;
                try
                {
                    Person per = await perHandler.CreatePersonAsync(null);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(AssertException));
                    Assert.AreEqual(ex.Message, "UserAccount can't be null");
                }
                Assert.IsTrue(isChecked);

                //4. Give the error user account and check it.
                isChecked = false;
                try
                {
                    UserAccount ua = new UserAccount() { ID = 99999 };
                    Person per = await perHandler.CreatePersonAsync(ua);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(AssertException));
                    Assert.AreEqual(ex.Message, "This user account doesn't exist");
                }
                Assert.IsTrue(isChecked);

                //5. Give the same user account and check it.
                isChecked = false;
                try
                {
                    Person per = await perHandler.CreatePersonAsync(testUA1);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(AssertException));
                    Assert.AreEqual(ex.Message, "Existing relationships");
                }
                Assert.IsTrue(isChecked);
            }
        }