Example #1
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);
            }
        }
Example #2
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 });
                }
            }
        }
Example #3
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 });
                }
            }
        }
Example #4
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<Blog> CreateBlogAsync(long personId, string content, BlogInfoAccessInfo accessInfo, long? groupId = null, List<long> attachContentIds = null, long? forwardBlogId = null)
        {
            using(var dbTransaction = _dbContext.Database.BeginTransaction())
            {
                try
	            {
                    BlogAccessControlHandler bacHandler = new BlogAccessControlHandler(_dbContext);

		            //1. 创建Blog对象并保存。
                    Blog blog = new Blog()
                    {
                        PersonID = personId,
                        Content = content,
                        IsDeleted = false
                    };
                    this.Add(blog);
                    await SaveChangesAsync();

                    //2. 判断是否有附件,有则建立与Blog的关联。
                    if (attachContentIds != null && attachContentIds.Count > 0)
                    {
                        ContentHandler contentHandler = new ContentHandler(_dbContext);
                        BlogXContentHandler bxcHandler = new BlogXContentHandler(_dbContext);

                        #region 检查attachContent集合。

                        List<Content> attachContents = await contentHandler.Fetch(x => attachContentIds.Contains(x.ID)).ToListAsync();

                        bool isDifferentType = attachContents.Select(x => x.Type).Distinct().Count() > 1;

                        if(isDifferentType)
                        {
                            throw new DisplayableException("不能发不同类型内容的Blog");
                        }

                        int photoCount = attachContents.Count(x => x.Type == ContentType.Photo);
                        int musicCount = attachContents.Count(x => x.Type == ContentType.Music);
                        int videoCount = attachContents.Count(x => x.Type == ContentType.Video);

                        if(photoCount > 6)
                        {
                            throw new DisplayableException("发表图片不能超过6张");
                        }
                        if (musicCount > 1)
                        {
                            throw new DisplayableException("发表音乐不能超过1首");
                        }
                        if (videoCount > 1)
                        {
                            throw new DisplayableException("发表视频不能超过1个");
                        }
                        #endregion

                        foreach (var attachContentId in attachContentIds)
                        {
                            BlogXContent bxc = new BlogXContent()
                            {
                                BlogID = blog.ID,
                                ContentID = attachContentId
                            };
                            bxcHandler.Add(bxc);
                        }
                        
                        await SaveChangesAsync();
                    }

                    //3. 判断当前是否转发Blog,是则建立转发Blog与新Blog的关联。
                    if(forwardBlogId.HasValue)
                    {
                        //3.1 判断ForwardBlog是否存在。
                        Blog forwardBlog = await GetByIdAsync(forwardBlogId);

                        if(forwardBlog == null || forwardBlog.IsDeleted)
                        {
                            throw new DisplayableException("该Blog不存在或者已经被删除");
                        }

                        //3.2 判断ForwardBlog是否转发了Blog,如果是则关联2条Blog,否则只关联当前要转发的Blog。
                        BlogXBlogHandler bxbHandler = new BlogXBlogHandler(_dbContext);

                        BlogXBlog blogXblog = new BlogXBlog();
                        blogXblog.NewBlogID = blog.ID;
                        blogXblog.BaseBlogID = forwardBlog.ID;
                        blogXblog.IsBase = false;

                        //3.3 判断ForwardBlog是否有转发Blog。
                        Blog baseBlog = await bxbHandler.GetBaseBlogByBlogIdAsync(forwardBlog.ID);

                        if(baseBlog == null)
                        {
                            blogXblog.IsBase = true;
                        }
                        else
                        {
                            BlogXBlog baseBlogXblog = new BlogXBlog();
                            baseBlogXblog.NewBlogID = blog.ID;
                            baseBlogXblog.BaseBlogID = baseBlog.ID;
                            baseBlogXblog.IsBase = true;

                            bxbHandler.Add(baseBlogXblog);
                        }

                        bxbHandler.Add(blogXblog);

                        await SaveChangesAsync();
                    }

                    //4. 建立AccessControl访问控制。
                    BlogAccessControl blogAccessControl = new BlogAccessControl()
                    {
                        BlogID = blog.ID,
                        AccessLevel = accessInfo
                    };
                    bacHandler.Add(blogAccessControl);
                    await SaveChangesAsync();

                    //5. 判断如果访问控制为GroupOnly,则建立AccessControlXGroup关系。
                    if(accessInfo == BlogInfoAccessInfo.GroupOnly)
                    {
                        if(groupId == null)
                        {
                            throw new DisplayableException("未指定组");
                        }

                        BlogAccessControlXGroupHandler bacxgHandler = new BlogAccessControlXGroupHandler(_dbContext);

                        BlogAccessControlXGroup bacxg = new BlogAccessControlXGroup()
                        {
                            BlogAccessControlID = blogAccessControl.ID,
                            GroupID = (long)groupId
                        };
                        bacxgHandler.Add(bacxg);
                        await SaveChangesAsync();
                    }

                    dbTransaction.Commit();

                    return blog;
	            }
	            catch (Exception)
	            {
		            dbTransaction.Rollback();
		            throw;
	            }
            }
        }
Example #5
0
        /// <summary>
        /// 获取Blog的Content集合。
        /// </summary>
        /// <param name="blogId">BlogID</param>
        /// <returns></returns>
        public async Task<List<Content>> GetBlogContentsAsync(long blogId)
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogXContentHandler bxcHandler = new BlogXContentHandler(dbContext);

                return await bxcHandler.GetContentsAsync(blogId);
            }
        }
Example #6
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;
            }
        }
Example #7
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;
            }
        }
Example #8
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;
            }
        }