示例#1
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;
	            }
            }
        }