예제 #1
0
        public CommentController(CommentContext context)
        {
            commentContext = context;

            if (commentContext.CommentItems.Count() == 0)
            {
                // Create a new item if collection is empty, which means you can't delete all items
                commentContext.CommentItems.Add(new CommentItem());
                commentContext.SaveChanges();
            }
        }
예제 #2
0
        /// <summary>
        /// 添加一个新评论
        /// </summary>
        /// <param name="comment"></param>
        public string AddComment(Comment comment)
        {
            //判断添加评论的文章是否存在
            var check = _articleContext.Article.Where(a => a.Id == comment.ArticleId).FirstOrDefault();

            if (check == null)
            {
                return("添加评论的文章不存在");
            }
            string name = _userContext.User.Find(comment.UserId).Name;

            if (name == null)
            {
                comment.CommentName = "Anonymous";
            }
            comment.CommentName = name;
            _commentContext.Comment.Add(comment);
            if (_commentContext.SaveChanges() == 1)
            {
                return("评论添加成功");
            }
            return("评论添加失败");
        }