/// <summary> /// 添加评论 /// </summary> /// <param name="comment"></param> /// <returns></returns> public int AddComment(Comment comment) { if (!_roleService.AddCommentPower()) { throw new DomainException("NoPower", "对不起,您没有权限发表评论。"); } else { if (_sessionManager.User == null) { if (string.IsNullOrEmpty(comment.Author)) { throw new DomainException("NoAuthor", "对不起,需要填写昵称才能发表评论。"); } else if (string.IsNullOrEmpty(comment.AuthorMail)) { throw new DomainException("NoAuthor", "对不起,需要填写邮箱才能发表评论。"); } } else { comment.User = _sessionManager.User; comment.Author = comment.User.UserName; comment.AuthorMail = comment.User.Email; } int id = 0; try { //打开事务 _commentRepository.BeginTransaction(); //取得评论所在文章 comment.Article = _articleService.GetArticle(comment.Article.ArticleId); //如果父评论不为空,而且父评论的文章ID跟当前的文章ID不一致,抛出异常 if (comment.Parent != null && comment.Article.ArticleId != comment.Parent.Article.ArticleId) { throw new DomainException("DataError", "对不起,您提交的数据异常!"); } //检查镶套层数 CheckCommentLevel(comment); //给文章添加评论个数 comment.Article.CommentCount++; //评论的创建时间 comment.CreateDate = DateTime.Now; //评论默认状态 if (_roleService.AddArticlePower()) { comment.Status = CommentStatus.Open; } else { comment.Status = int.Parse(_settiongService.GetSetting("CommentSatus")); } //添加文章 id = _commentRepository.Add(comment); //事务提交 _commentRepository.CommitTransaction(); } catch (Exception ex) { //回滚事务 _commentRepository.RollbackTransaction(); throw ex; } return(id); } }