Ejemplo n.º 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();
            }
        }
Ejemplo n.º 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;
            }
        }
Ejemplo n.º 3
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);                
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get the current UserProfile object from the UserAccount in the CurrentPrinciple
        /// </summary>
        /// <param name="dbContext">Must be KoalaBlogDbContext</param>
        /// <returns></returns>
        protected async Task<Person> CurrentUserPersonRecord(DbContext dbContext)
        {
            PersonHandler perHandler = new PersonHandler(dbContext);

            if (CurrentThreadIdentityObject != null)
            {
                return await perHandler.LoadByUserAccountIDAsync(CurrentThreadIdentityObject.UserID);
            }

            return null;
        }
Ejemplo n.º 5
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 : "";
            }
        }
Ejemplo n.º 6
0
        public async Task<bool> ConfirmEmailAsync(string email, string code)
        {
            AssertUtil.Waterfall()
                .NotNullOrWhiteSpace(email, "邮箱不能为空")
                .NotNullOrWhiteSpace(code, "验证码不能为空")
                .IsValidEmail(email, "邮箱地址不正确")
                .Done();

            UserAccountHandler uaHandler = new UserAccountHandler(_dbContext);
            PersonHandler perHandler = new PersonHandler(_dbContext);
            AvatarHandler avatarHandler = new AvatarHandler(_dbContext);

            UserAccount user = await uaHandler.GetByEmailAsync(email);

            if (user != null)
            {
                //1. 判断验证码是否匹配。
                bool isMatched = await Entities.AnyAsync(x => x.UserAccountID == user.ID && x.Code == code);

                if (isMatched)
                {
                    //这里需要用事务来保证执行成功。
                    using(TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                    {
                        //2. 匹配则修改邮件验证为True。
                        user.EmailConfirmed = true;

                        uaHandler.MarkAsModified(user);

                        bool isSucceed = await SaveChangesAsync() > 0;

                        //3. 同时生成UserAccountXPerson记录。
                        if (isSucceed)
                        {
                            Person per = await perHandler.CreatePersonAsync(user);

                            //4. 生成默认Avatar。
                            await avatarHandler.CreateDefaultAvatar(per.ID);
                        }

                        transactionScope.Complete();

                        return true;
                    }
                }
            }
            return false;
        }
Ejemplo n.º 7
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);
            }
        }
Ejemplo n.º 8
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);
            }
        }
Ejemplo n.º 9
0
        /// <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;
            }
        }
Ejemplo n.º 10
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;
            }
        }
Ejemplo n.º 11
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);
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 添加一条评论
        /// </summary>
        /// <param name="personId">评论者ID</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<Comment> AddCommentAsync(long personId, long blogId, string content, List<long> photoContentIds = null, long? baseCommentId = null)
        {
            BlogHandler blogHandler = new BlogHandler(_dbContext);        
            GroupHandler groupHandler = new GroupHandler(_dbContext);
            PersonHandler perHandler = new PersonHandler(_dbContext);

            //1. 检查要评论的Blog是否存在。
            Blog beCommentBlog = await blogHandler.GetByIdAsync(blogId);

            //2. 如果为空或者被逻辑删除,则Exception。
            if (beCommentBlog == null || beCommentBlog.IsDeleted)
            {
                throw new DisplayableException("要评论的Blog不存在或者已经被删除");
            }

            //2.1 自己评论自己的Blog永远可以,所以只需要判断不同的PersonID。
            if(beCommentBlog.PersonID != personId)
            {
                //3. 检查当前用户是否被该评论Blog的用户加入了黑名单,如果是则不能进行评论。
                Group beCommentBlogPersonBlackList = await groupHandler.Include(x => x.GroupMembers).SingleOrDefaultAsync(x => x.PersonID == beCommentBlog.PersonID && x.Type == GroupType.BlackList);

                if (beCommentBlogPersonBlackList != null && beCommentBlogPersonBlackList.GroupMembers.Count > 0)
                {
                    bool isBlocked = beCommentBlogPersonBlackList.GroupMembers.Select(x => x.PersonID).Contains(personId);

                    if (isBlocked)
                    {
                        throw new DisplayableException("由于用户设置,你无法回复评论。");
                    }
                }

                //4. 检查评论Blog的用户消息设置,是否允许评论。
                Person beCommentBlogPerson = await perHandler.GetByIdAsync(beCommentBlog.PersonID);

                if (beCommentBlogPerson != null)
                {
                    //4.1 如果评论只允许关注的人评论,则判断Blog的用户的是否关注了当前用户。
                    if (beCommentBlogPerson.AllowablePersonForComment == AllowablePersonForComment.FollowerOnly)
                    {
                        //4.2 判断关注的人集合是否已经加载。
                        if (!_dbContext.Entry(beCommentBlogPerson).Collection(x => x.MyFollowingPersons).IsLoaded)
                        {
                            _dbContext.Entry(beCommentBlogPerson).Collection(x => x.MyFollowingPersons).Load();
                        }

                        bool isFollow = beCommentBlogPerson.MyFollowingPersons.Select(x => x.FollowingID).Contains(personId);

                        if (!isFollow)
                        {
                            throw new DisplayableException("由于用户设置,你无法回复评论。");
                        }
                    }
                    //4.3 如果评论只允许粉丝评论,则判断当前用户是否关注了该Blog用户。
                    else if (beCommentBlogPerson.AllowablePersonForComment == AllowablePersonForComment.FansOnly)
                    {
                        //4.4 判断粉丝集合是否已经加载。
                        if (!_dbContext.Entry(beCommentBlogPerson).Collection(x => x.MyFans).IsLoaded)
                        {
                            _dbContext.Entry(beCommentBlogPerson).Collection(x => x.MyFans).Load();
                        }

                        bool isFans = beCommentBlogPerson.MyFans.Select(x => x.FollowerID).Contains(personId);

                        if (!isFans)
                        {
                            throw new DisplayableException("由于用户设置,你无法回复评论。");
                        }
                    }
                }

                //5. 检查评论Blog的用户消息设置,是否允许评论附带图片。
                if(photoContentIds != null && photoContentIds.Count > 0)
                {
                    if(!beCommentBlogPerson.AllowCommentAttachContent)
                    {
                        throw new DisplayableException("由于用户设置,你回复评论无法添加图片。");
                    }
                }
            }
            
            using(var dbTransaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    //6. 建立Comment对象并保存。
                    Comment comment = new Comment()
                    {
                        PersonID = personId,
                        BlogID = blogId,
                        Content = content
                    };
                    this.Add(comment);
                    await SaveChangesAsync();

                    //7. 判断是否有图片,有则建立与Comment的关联。
                    if (photoContentIds != null && photoContentIds.Count > 0)
                    {
                        if(photoContentIds.Count > 6)
                        {
                            throw new DisplayableException("评论最多附件6张图片");
                        }

                        ContentHandler contentHandler = new ContentHandler(_dbContext);

                        //7.1 判断附件是否为图片。
                        List<Content> photoContents = await contentHandler.Fetch(x => photoContentIds.Contains(x.ID)).ToListAsync();

                        if(!photoContents.Any(x => x.Type == ContentType.Photo))
                        {
                            throw new DisplayableException("评论只能附件图片");
                        }

                        //7.2 建立Comment与Content的关联。
                        CommentXContentHandler cxcHandler = new CommentXContentHandler(_dbContext);

                        foreach (var photoContentId in photoContentIds)
                        {
                            CommentXContent cxc = new CommentXContent()
                            {
                                CommentID = comment.ID,
                                ContentID = photoContentId
                            };
                            cxcHandler.Add(cxc);
                        }
                        await SaveChangesAsync();
                    }

                    //8. 判断当前评论是否评论Blog里的其他评论,如果是则建立关联。
                    if(baseCommentId != null)
                    {
                        //8.1 判断被评论ID是否存在。
                        Comment baseComment = await GetByIdAsync(baseCommentId);

                        if(baseComment == null)
                        {
                            throw new DisplayableException("此评论不存在或者已经被删除");
                        }

                        //8.2 判断被评论的BlogID是否与当前评论的BlogID一致。
                        if(baseComment.BlogID != blogId)
                        {
                            throw new DisplayableException("此评论的BlogID与被评论的BlogID不一致");
                        }

                        //8.3 建立关联。
                        CommentXCommentHandler cxcHandler = new CommentXCommentHandler(_dbContext);

                        CommentXComment cxc = new CommentXComment()
                        {
                            BaseCommentID = (long)baseCommentId,
                            NewCommentID = comment.ID
                        };
                        cxcHandler.Add(cxc);
                        await SaveChangesAsync();
                    }

                    dbTransaction.Commit();

                    return comment;
                }
                catch (Exception)
                {
                    dbTransaction.Rollback();
                    throw;
                }
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 关注
        /// </summary>
        /// <param name="followerId">关注者ID</param>
        /// <param name="followingId">被关注者ID</param>
        /// <returns></returns>
        public async Task<bool> FollowAsync(long followerId, long followingId)
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                PersonHandler perHandler = new PersonHandler(dbContext);

                return await perHandler.FollowAsync(followerId, followingId);
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 获取当前用户关注的人Blog
        /// </summary>
        /// <param name="personId">用户ID</param>
        /// <param name="groupId">组ID</param>
        /// <returns></returns>
        public async Task<List<Blog>> GetBlogsAsync(long personId, long? groupId = null, int pageIndex = 1, int pageSize = int.MaxValue)
        {
            PersonHandler perHandler = new PersonHandler(_dbContext);
            GroupHandler groupHandler = new GroupHandler(_dbContext);
            BlogAccessControlHandler acHandler = new BlogAccessControlHandler(_dbContext);

            //1. 获取当前用户Person实体,并加载正在关注列表。
            Person per = await perHandler.Include(x => x.MyFollowingPersons).SingleOrDefaultAsync(x => x.ID == personId);

            if(per == null)
            {
                throw new DisplayableException("该用户不存在");
            }

            //2. 获取组成员或者已关注的用户ID集合,若指定了GroupID,则获取组里成员ID集合,反则获取已关注的人ID集合。
            List<long> perIds = new List<long>();

            if(groupId.HasValue)
            {
                Group normalGroup = await groupHandler.Include(x => x.GroupMembers).SingleOrDefaultAsync(x => x.ID == groupId);

                if(normalGroup != null)
                {
                    if(normalGroup.PersonID != personId)
                    {
                        throw new DisplayableException("该组不属于此用户");
                    }

                    //2.1 获取指定Group的用户ID集合。
                    if(normalGroup.GroupMembers.Count > 0)
                    {
                        perIds = normalGroup.GroupMembers.Select(x => x.PersonID).ToList();
                    }
                }
            }
            else
            {
                //2.2 获取当前用户已关注的用户ID集合。
                perIds = per.MyFollowingPersons.Select(x => x.FollowingID).ToList();
            }

            //2.3 加上当前用户的ID,提供查询当前用户的Blog。
            perIds.Add(personId);

            //3. 获取当前用户屏蔽名单Group。
            Group shieldGroup = await groupHandler.Include(x => x.GroupMembers).SingleOrDefaultAsync(x => x.PersonID == personId && x.Type == GroupType.ShieldList);

            if (shieldGroup != null && shieldGroup.GroupMembers.Count > 0)
            {
                List<long> shieldListIds = shieldGroup.GroupMembers.Select(x => x.PersonID).ToList();

                //3.1 过滤屏蔽名单上的用户,不加载屏蔽名单上用户的Blog。
                perIds = perIds.Except(shieldListIds).ToList();
            }

            //4. 获取过滤后的用户ID集合的Blog列表,以创建时间降序排序。
            List<Blog> blogs = await Fetch(x => perIds.Contains(x.PersonID) && !x.IsDeleted).OrderByDescending(x => x.CreatedDate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();

            if(blogs.Count > 0)
            {
                //5.1 获取排除当前用户ID的BlogIDs集合。
                List<long> blogListIds = blogs.Where(x => x.PersonID != personId).Select(x => x.ID).ToList();

                //5.2 获取Blog列表的AccessControl(权限控制)列表,排除公开的权限控制。
                List<BlogAccessControl> blogAccessControls = await acHandler.Fetch(x => blogListIds.Contains(x.BlogID) && x.AccessLevel != BlogInfoAccessInfo.All).ToListAsync();

                foreach (var blogAccessControl in blogAccessControls)
                {
                    Blog acBlog = blogs.Single(x => x.ID == blogAccessControl.BlogID);

                    //5.3 如果权限控制为仅自己可见,且发表Blog的用户ID不等于当前用户ID,则过滤掉此Blog。
                    if (blogAccessControl.AccessLevel == BlogInfoAccessInfo.MyselfOnly)
                    {
                        blogs.Remove(acBlog);
                    }
                    //5.4 如果权限控制为群可见,判断此群的人员名单是否包含当前用户,没有则过滤此Blog。
                    else if (blogAccessControl.AccessLevel == BlogInfoAccessInfo.GroupOnly)
                    {
                        BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(_dbContext);

                        BlogAccessControlXGroup acxGroup = await acxgHandler.Include(x => x.Group, x => x.Group.GroupMembers).SingleOrDefaultAsync(x => x.BlogAccessControlID == blogAccessControl.ID);

                        if (acxGroup != null)
                        {
                            bool isInGroupMember = false;

                            if (acxGroup.Group != null && acxGroup.Group.GroupMembers.Count > 0)
                            {
                                isInGroupMember = acxGroup.Group.GroupMembers.Select(x => x.PersonID).Any(x => x == personId);
                            }

                            if (!isInGroupMember)
                            {
                                blogs.Remove(acBlog);
                            }
                        }
                    }
                    //5.5 如果权限控制为朋友圈可见,判断当前用户是否与互相关注(Friends),如果不是则过滤此Blog。
                    else if (blogAccessControl.AccessLevel == BlogInfoAccessInfo.FriendOnly)
                    {
                        PersonXPersonHandler pxpHandler = new PersonXPersonHandler(_dbContext);

                        bool isFriend = await pxpHandler.IsFriendAsync(personId, acBlog.PersonID);

                        if (!isFriend)
                        {
                            blogs.Remove(acBlog);
                        }
                    }
                }
            }
            return blogs;
        }
Ejemplo n.º 15
0
        /// <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;
            }
        }
Ejemplo n.º 16
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);
            }
        }
Ejemplo n.º 17
0
        public async Task Test_02_ModifyPersonNickNameAsync()
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                //1. Create a test person obj.
                PersonHandler perHandler = new PersonHandler(dbContext);
                Person p = new Person()
                {
                    RealName = "testMike",
                    NickName = "testIAMMike",
                    Gender = Gender.Male,
                    DOB = new DateTime(2015, 7, 15),
                    RealNameAccessLevel = PersonInfoAccessInfo.MyselfOnly,
                    SexualTrendAccessLevel = PersonInfoAccessInfo.MyselfOnly,
                    MaritalStatusAccessLevel = PersonInfoAccessInfo.MyselfOnly,
                    QQAccessLevel = PersonInfoAccessInfo.MyselfOnly,
                    DOBAccessLevel = PersonInfoAccessInfo.MyselfOnly,
                    BloodTypeAccessLevel = PersonInfoAccessInfo.MyselfOnly,
                    HomePageAccessLevel = PersonInfoAccessInfo.MyselfOnly
                };
                perHandler.Add(p);
                await perHandler.SaveChangesAsync();

                Assert.AreNotEqual(p.ID, 0);

                //2. Modify the person nick name and test it.
                bool isSucceed = await perHandler.ModifyPersonNickNameAsync(p.ID, "testIAMShy");
                Assert.IsTrue(isSucceed);

                //2.1 Get the person by id
                Person modifiedPerson = await perHandler.GetByIdAsync(p.ID);

                Assert.IsNotNull(modifiedPerson);
                Assert.AreEqual(modifiedPerson.NickName, "testIAMShy");
                Assert.AreEqual(modifiedPerson.RealName, "testMike");
                Assert.AreEqual(modifiedPerson.Gender, Gender.Male);
                Assert.AreEqual(modifiedPerson.DOB, new DateTime(2015, 7, 15));
                Assert.AreEqual(modifiedPerson.RealNameAccessLevel, PersonInfoAccessInfo.MyselfOnly);

                //3. Give the error parameter and test it.
                bool isChecked = false;
                try
                {
                    isSucceed = await perHandler.ModifyPersonNickNameAsync(0, "testSorryShy");
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(AssertException));
                    Assert.AreEqual(ex.Message, "personId must be greater than zero");
                }
                Assert.IsTrue(isChecked);

                isChecked = false;
                try
                {
                    isSucceed = await perHandler.ModifyPersonNickNameAsync(0, "");
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(AssertException));
                    AssertException assertException = ex as AssertException;
                    Assert.IsNotNull(assertException.ExceptionMessageList);
                    Assert.AreEqual(assertException.ExceptionMessageList.Count, 2);
                    Assert.AreEqual(assertException.ExceptionMessageList[0], "personId must be greater than zero");
                    Assert.AreEqual(assertException.ExceptionMessageList[1], "nickName can't be empty");
                }
                Assert.IsTrue(isChecked);

                isChecked = false;
                try
                {
                    isSucceed = await perHandler.ModifyPersonNickNameAsync(99999999, "testIAMShy");
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(AssertException));
                    Assert.AreEqual(ex.Message, "This person doesn't exist");
                }
                Assert.IsTrue(isChecked);
            }
        }
Ejemplo n.º 18
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);
            }
        }
Ejemplo n.º 19
0
        public async Task Test_03_ModifyPersonInfoAsync()
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                //1. Create a test person obj.
                PersonHandler perHandler = new PersonHandler(dbContext);
                Person p = new Person()
                {
                    RealName = "testJay",
                    NickName = "testMayBeJay",
                    Gender = Gender.Female,
                    DOB = new DateTime(2015, 8, 23),
                    RealNameAccessLevel = PersonInfoAccessInfo.MyselfOnly,
                    SexualTrendAccessLevel = PersonInfoAccessInfo.MyselfOnly,
                    MaritalStatusAccessLevel = PersonInfoAccessInfo.MyselfOnly,
                    QQAccessLevel = PersonInfoAccessInfo.MyselfOnly,
                    DOBAccessLevel = PersonInfoAccessInfo.MyselfOnly,
                    BloodTypeAccessLevel = PersonInfoAccessInfo.MyselfOnly,
                    HomePageAccessLevel = PersonInfoAccessInfo.MyselfOnly
                };
                perHandler.Add(p);
                await perHandler.SaveChangesAsync();

                Assert.AreNotEqual(p.ID, 0);

                //2. Modify person info.
                Person beModifiedPerson = await perHandler.GetByIdAsync(p.ID);
                beModifiedPerson.RealName = "testGoodMan";
                beModifiedPerson.NickName = "testIAMGoodMan";
                beModifiedPerson.Gender = Gender.Male;
                beModifiedPerson.DOB = new DateTime(2015, 8, 21);
                beModifiedPerson.RealNameAccessLevel = PersonInfoAccessInfo.All;
                beModifiedPerson.DOBAccessLevel = PersonInfoAccessInfo.FollowerOnly;
                await perHandler.ModifyPersonInfoAsync(beModifiedPerson);

                //3. Test the modified person.
                Person modifiedPerson = await perHandler.GetByIdAsync(p.ID);
                Assert.IsNotNull(modifiedPerson);
                Assert.AreEqual(modifiedPerson.RealName, "testGoodMan");
                Assert.AreEqual(modifiedPerson.NickName, "testIAMGoodMan");
                Assert.AreEqual(modifiedPerson.Gender, Gender.Male);
                Assert.AreEqual(modifiedPerson.RealNameAccessLevel, PersonInfoAccessInfo.All);
                Assert.AreEqual(modifiedPerson.DOBAccessLevel, PersonInfoAccessInfo.FollowerOnly);
                Assert.AreEqual(modifiedPerson.HomePageAccessLevel, PersonInfoAccessInfo.MyselfOnly);
            }
        }
Ejemplo n.º 20
0
        public async Task Test_04_FollowAsync()
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                PersonHandler perHandler = new PersonHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext);

                //1. test normal.
                Person sam = CreatePerson("TestSam", "TestSam");
                Person joan = CreatePerson("TestJoan", "TestJoan");

                bool isSucceed = await perHandler.FollowAsync(sam.ID, joan.ID);

                sam = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == sam.ID);
                joan = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == joan.ID);

                Assert.IsTrue(isSucceed);
                Assert.AreEqual(sam.MyFollowingPersons.Count, 1);
                Assert.AreEqual(sam.MyFans.Count, 0);
                Assert.AreEqual(joan.MyFollowingPersons.Count, 0);
                Assert.AreEqual(joan.MyFans.Count, 1);

                //2. test one more time follow, and check it count was changed.
                isSucceed = await perHandler.FollowAsync(sam.ID, joan.ID);

                sam = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == sam.ID);
                joan = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == joan.ID);

                Assert.IsTrue(isSucceed);
                Assert.AreEqual(sam.MyFollowingPersons.Count, 1);
                Assert.AreEqual(sam.MyFollowingPersons.First().FollowingID, joan.ID);
                Assert.AreEqual(sam.MyFans.Count, 0);
                Assert.AreEqual(joan.MyFollowingPersons.Count, 0);
                Assert.AreEqual(joan.MyFans.Count, 1);
                Assert.AreEqual(joan.MyFans.First().FollowerID, sam.ID);

                //3. test black list.
                Person jay = CreatePerson("TestJay", "TestJay");
                Person ken = CreatePerson("TestKen", "TestKen");

                Group blackGroup = await groupHandler.CreateGroupAsync(ken.ID, "TestBGroup", GroupType.BlackList);

                GroupMember gm_sam = new GroupMember()
                {
                    PersonID = jay.ID,
                    GroupID = blackGroup.ID
                };
                gmHandler.Add(gm_sam);
                gmHandler.SaveChanges();

                bool isChecked = false;
                try
                {
                    isSucceed = await perHandler.FollowAsync(jay.ID, ken.ID);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(DisplayableException));
                    Assert.AreEqual(ex.Message, "由于用户设置,你无法关注。");
                }
                Assert.IsTrue(isChecked);

                jay = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == jay.ID);
                ken = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == ken.ID);

                Assert.AreEqual(jay.MyFollowingPersons.Count, 0);
                Assert.AreEqual(jay.MyFans.Count, 0);
                Assert.AreEqual(ken.MyFollowingPersons.Count, 0);
                Assert.AreEqual(ken.MyFans.Count, 0);
            }
        }
Ejemplo n.º 21
0
        /// <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;
            }
        }