public IActionResult Add(AddCommentRequest model)
        {
            model.PostIP = this.Context.GetClientIP();

            var result = this.CommentService.Add(model);

            this.Response.Cookies.Append("comment_nickname", model.Nickname);
            this.Response.Cookies.Append("comment_email", model.Email);

            var or = new ObjectResult(result);
            return or;
        }
Esempio n. 2
0
        public OperationResult<int?> Add(AddCommentRequest model)
        {
            if (!this.ArticleService.IsArticleExisted(model.ArticleID))
            {
                return OperationResult<int?>.ErrorResult("不存在的文章");
            }
            if (model.ReplyTo.HasValue && !this.IsCommentExisted(model.ReplyTo.Value))
            {
                return OperationResult<int?>.ErrorResult("不存在的评论");
            }

            using (var conn = this.OpenConnection())
            {
                string sql = @"
INSERT INTO [Comment] (ArticleID,ReplyTo,Content,Email,Nickname,PostDate,PostIP,Status,NotifyOnReply)
VALUES (@ArticleID,@ReplyTo,@Content,@Email,@Nickname,@PostDate,@PostIP,@Status,@NotifyOnReply);

SELECT @@IDENTITY;
";

                var para = new
                {
                    model.ArticleID,
                    model.ReplyTo,
                    model.Content,
                    model.Email,
                    model.Nickname,
                    PostDate = DateTime.Now,
                    model.PostIP,
                    Status = 1,
                    model.NotifyOnReply
                };

                var id = conn.ExecuteScalar<int>(sql, para);

                string cacheKey = Helpers.CacheKeyHelper.GetArticleCommentsCacheKey(model.ArticleID);
                this.Cache.Remove(cacheKey);

                return OperationResult<int?>.SuccessResult(id);
            }
        }