/// <summary> /// 修改Blog,会自动修正Blog的数据 /// </summary> /// <param name="newBlog">修改完成的Blog</param> /// /// <param name="blog">要修改的Blog</param> /// <param name="tags"></param> /// <returns></returns> public void EditBlog(Blog newBlog, Blog blog, IList <Tag> tags, TagApplication tagApplication, BlogTagsApplication blogTagsApplication) { //先调用ValidateAndCorrectSubmit验证更合理 newBlog.Date = DateTime.Now; //进行事务添加 blogTagsApplication.DeleteByBlogId(blog.BlogId); //if (!blogTagsApplication.Commit()) //{ // throw new Exception("删除原Blog和标签关系异常"); //} IList <BlogTags> blogTags = blogTagsApplication.UpdateBlogTags(blog, tags, tagApplication); //对齐Blog对象 blog.BTitle = newBlog.BTitle; blog.BContext = newBlog.BContext; blog.BSummary = newBlog.BSummary; blog.BSummaryTrim = newBlog.BSummaryTrim; blog.BHTML = newBlog.BHTML; blog.BScript = newBlog.BScript; blog.BReprint = newBlog.BReprint; //blog.BPV = newBlog.BPV; //blog.Likes = newBlog.Likes; blog.BName = newBlog.BName; blog.BLastCommentUser = newBlog.BLastCommentUser; blog.BLastComment = newBlog.BLastComment; blog.BIsTop = newBlog.BIsTop; blog.BImgTrim = newBlog.BImgTrim; blog.BImg = newBlog.BImg; //blog.BComment = newBlog.BComment; //blog.BClick = newBlog.BClick; blog.ClassifyId = newBlog.ClassifyId; blog.BName = newBlog.BName; Update(blog); blogTagsApplication.AddLists(blogTags); }
/// <summary> /// 将Blog的Tag添加到数据库,并生成BlogTags(Blog和Tag关系表)的数组(尚未提交到数据库,需要自己提交,并且关系表中的对象(Blog/Tag)都为null) /// </summary> /// <param name="blog"></param> /// <param name="tags">Blog对应的标签对象</param> /// <returns></returns> public IList <BlogTags> UpdateBlogTags(Blog blog, IList <Tag> tags, TagApplication tagApplication) { var selectTagIds = tags.Where(tag => !string.IsNullOrWhiteSpace(tag.TagId)).Select(m => m.TagId); //得到要查询的Tag name列表(把为null的tag的tagId过滤掉,因为前端传递过来的tag,如果是新增的,则为null),然后进行数据库查询 IEnumerable <Tag> existTags = tagApplication.FindListByTagIdList(selectTagIds.ToArray()); //从数据库中查询到已存在的Tag IList <Tag> addTags = tags.Where(tag => { //前端传递过来,新增的tag的tagId都是null,同时去数据库中检测,如果发现有新增的项数据库中并没有 if (string.IsNullOrWhiteSpace(tag.TagId) && !existTags.Any(t => t.TagName == tag.TagName)) { tag.TagId = SaidCommon.GUID; tag.Count = 1;//tag应该由中间表记录和Blog的关系,而不应该直接查询Tag tag.Date = DateTime.Now; return(true); } return(false); }).ToList(); if (addTags.Count() > 0) { //if (tagApplication.AddList(addTags) < 1) //{ // throw new Exception("新增Tag失败"); //} tagApplication.AddList(addTags); if (!tagApplication.Commit()) { throw new Exception("新增Tag失败"); } } if (existTags != null && existTags.Count() > 0 && addTags.Count() > 0) { tags = existTags.Concat(addTags).ToList();//Concat参考:http://www.cnblogs.com/heyuquan/p/Linq-to-Objects.html } else if (addTags.Count() > 0) { tags = addTags; } else if (existTags.Count() > 0) { tags = existTags.ToList(); } //这里应该是调用BlogTagsApplication的方法 //新增Tag成功,生成BlogTags var blogTags = new List <BlogTags>(); foreach (var item in tags) { blogTags.Add(new BlogTags { BlogId = blog.BlogId, TagId = item.TagId, Date = DateTime.Now, BlogTagsId = SaidCommon.GUID }); } return(blogTags); }
/// <summary> /// 添加Blog,会自动修正Blog的数据,新增blogId /// </summary> /// <param name="blog"></param> /// <param name="tags"></param> /// <returns></returns> public void AddBlog(Blog blog, IList <Tag> tags, BlogTagsApplication blogTagsApplication, TagApplication tagApplication) { //先调用ValidateAndCorrectSubmit验证更合理 blog.BlogId = string.IsNullOrWhiteSpace(blog.BName) ? FileCommon.CreateFileNameByTime() : blog.BName; blog.Date = DateTime.Now; IList <BlogTags> blogTags = blogTagsApplication.UpdateBlogTags(blog, tags, tagApplication); Add(blog); if (!Commit()) { throw new Exception("新增Blog异常"); } //新增BlogTags完毕,新增Blog blogTagsApplication.AddLists(blogTags); }