public void CreateStar(CreateUpdateStarInput input) { Logger.Info("添加明星的输入信息(CreateUpdateStarInput对象): " + input); var star = new Star { Name = input.Name, Nickname = input.Nickname, ChineseName = input.ChineseName, Gender = input.Gender, Description = input.Description, IsBlocked = false,//添加明星默认不屏蔽 CreationTime = DateTime.Now, LastUpdateTime = DateTime.Now, }; //添加该明星对应的首个博客(一个明星对应多个博客,在创建明星时只能添加一个博客,创建完之后才可以增加更多) _starRepository.Insert(star); _unitOfWorkManager.Current.SaveChanges(); var blog = new Blog { Url = input.BlogUrl, CreationTime = DateTime.Now, LastUpdateTime = DateTime.Now, StarId = star.Id, }; blog.ResolveUrl(input.BlogUrl); var sameBlog = _blogRepository.GetAll().Where(b => b.Name == blog.Name).Where(b => b.Provider == blog.Provider); if (sameBlog.Count() > 0) { //保证微博地址在数据库中的唯一性,爬虫就不会重复下载博文 //抛出异常后上面_starRepository.Insert(star)的操作会被回滚 throw new UserFriendlyException("你添加的微博" + input.BlogUrl + "已经存在,不能重复添加"); } else { _blogRepository.Insert(blog); } }
public void CreateUpdateBlog(CreateUpdateBlogInput input) { //System.Web.HttpContext.Current.Server.MapPath( var star = _starRepository.Get(input.starId); if (star == null) { throw new UserFriendlyException("错误的明星ID!"); } if(input.BlogUrls.Count()<3 || input.BlogIds.Count()<3) throw new UserFriendlyException("地址信息至少要有3条!"); for (int i = 0; i < 3; i++) { var url = input.BlogUrls.ElementAt(i); var id = input.BlogIds.ElementAt(i); if (string.IsNullOrEmpty(url)) { if (id != -1) { //把已有的URL置为空字符串,这意味着管理者想删除该微博信息 _blogRepository.Delete(id); } } else { var blog = new Blog { Url = url }; blog.ResolveUrl(url); if (!string.IsNullOrEmpty(blog.Name) && blog.Provider != BlogProvider.UNDEFINED) { var sameBlog = _blogRepository.GetAll().Where(b => b.StarId != input.starId && b.Name == blog.Name && b.Provider == blog.Provider); if (sameBlog.Count() > 0) { throw new UserFriendlyException("你添加的微博" + url + "已经存在,不能重复添加"); } } if (id != -1) { //编辑微博 var existBlog = _blogRepository.Get(id); if (existBlog == null) { throw new UserFriendlyException("错误的微博ID!"); } existBlog.LastUpdateTime = DateTime.Now; existBlog.Url = blog.Url; existBlog.Name = blog.Name; existBlog.Provider = blog.Provider; existBlog.ProviderName = blog.ProviderName; _blogRepository.Update(existBlog); } else { //添加微博 blog.CreationTime = DateTime.Now; blog.LastUpdateTime = DateTime.Now; blog.StarId = input.starId; blog.Description = ""; _blogRepository.Insert(blog); } } } }