Пример #1
0
 public static Comment ToModel(this CommentVModel c)
 {
     return(new Comment {
         Id = c.Id,
         Content = c.Content
     });
 }
Пример #2
0
        public async Task <IActionResult> Create(int postId, [FromBody] CommentVModel comment)
        {
            var newComment = comment.ToModel();

            newComment.PostId = postId;
            newComment.UserId = UserId;
            newComment        = _comments.Create(newComment, UserId);
            await _comments.SaveAsync();

            _logger.LogInformation($"User: {UserId} created a new comment {newComment}");
            return(Created(Request.Path.Value, newComment.ToVModel()));
        }
Пример #3
0
        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="comment"></param>
        /// <returns></returns>
        public long AddComment(CommentVModel comment)
        {
            //发布
            long cId = Comments.AddComment(Comments.CommentVModelToInfo(comment));

            if (cId > 0)
            {
                //更新文章评论数
                _articlesPublic.UpdateCommentNum(comment.AId, 1);
                //更新用户评论数
                _usersPublic.UpdateCommentNum(comment.UserId, 1);
            }
            return(cId);
        }
Пример #4
0
        public JsonResult Add(CommentVModel model)
        {
            var result = new JsonResultModel();

            if (ModelState.IsValid)
            {
                //add
                result.ResultState = Comments.AddComment(Comments.CommentVModelToInfo(model)) > 0;
            }
            else
            {
                result.ResultState = false;
                result.Message     = ModelStateHelper.GetAllErrorMessage(ModelState);
            }

            return(Json(result));
        }
Пример #5
0
        public async Task <IActionResult> Update(int postId, [FromBody] CommentVModel comment)
        {
            var oldComment = await _comments.GetByIdAsync(comment.Id);

            if (oldComment.UserId != UserId)
            {
                throw new UnauthorizedAccessException(userMismatchMessage);
            }
            if (oldComment.PostId != postId)
            {
                throw new AccessViolationException(resourceMismatchMessage);
            }
            oldComment.Content = comment.Content;
            _comments.Update(oldComment, UserId);
            await _comments.SaveAsync();

            _logger.LogInformation($"User: {UserId} modified a his comment {oldComment}");
            return(Ok(oldComment.ToVModel()));
        }
Пример #6
0
 /// <summary>
 /// ViewModel 转 DataModel
 /// </summary>
 /// <param name="commentVModel"></param>
 /// <returns></returns>
 public static CommentInfo CommentVModelToInfo(CommentVModel commentVModel)
 {
     if (commentVModel == null)
     {
         return(new CommentInfo());
     }
     return(new CommentInfo
     {
         Id = commentVModel.Id,
         AId = commentVModel.AId,
         UserId = commentVModel.UserId,
         CreateTime = commentVModel.CreateTime,
         Content = commentVModel.Content,
         IP = commentVModel.IP,
         UserAgent = commentVModel.UserAgent,
         State = commentVModel.State,
         RefuseReason = commentVModel.RefuseReason,
         IsDelete = commentVModel.IsDelete,
         LastEditUserId = commentVModel.LastEditUserId,
         LastEditTime = commentVModel.LastEditTime,
         IsStick = commentVModel.IsStick,
         DianZanNum = commentVModel.DianZanNum
     });
 }
Пример #7
0
        public ActionResult Add()
        {
            var vm = new CommentVModel();

            return(View(vm));
        }
Пример #8
0
        public ActionResult Add(int id, string content)
        {
            if (id < 0)
            {
                return(Json(new JsonResultModel {
                    Message = "参数无效!"
                }));
            }
            if (string.IsNullOrEmpty(content) || content.Length < Site.Config.WebSysConfig.CommentMinLength)
            {
                return(Json(new JsonResultModel {
                    Message = "评论内容太短了!"
                }));
            }
            if (content.Length > Site.Config.WebSysConfig.CommentMaxLength)
            {
                return(Json(new JsonResultModel {
                    Message = "评论内容太长了!"
                }));
            }
            if (!CommentsPublic.CheckCanPost(CurrentUserInfo.Id, Site.Config.UserConfig.AddCommentInterval))
            {
                return(Json(new JsonResultModel {
                    Message = "操作速度太快了,喝口水再试一下!"
                }));
            }
            var articles = Articles.GetArticleInfoById(id);

            if (articles.IsCloseComment)
            {
                return(Json(new JsonResultModel {
                    Message = string.Format("评论已关闭!原因:{0}", articles.CloseCommentReason)
                }));
            }
            var comment = new CommentVModel()
            {
                UserId    = CurrentUserInfo.Id,
                AId       = id,
                IP        = Common.Fetch.Ip,
                Content   = content,
                UserAgent = Common.Fetch.UserAgent
            };
            var user = Users.GetUserById(CurrentUserInfo.Id);

            if (user == null || user.IsNull)
            {
                return(Json(new JsonResultModel {
                    Message = "用户信息异常!"
                }));
            }
            var role = Roles.GetRole(user.RoleId);

            if (role == null || role.IsNull)
            {
                return(Json(new JsonResultModel {
                    Message = "角色信息异常!"
                }));
            }
            //根据角色判断是否需要审核
            comment.State = (short)(role.CommentNeedVerified ? 0 : 1);


            long cid = CommentsPublic.AddComment(comment);

            if (cid <= 0)
            {
                return(Json(new JsonResultModel {
                    Message = "评论失败!"
                }));
            }
            return(Json(new JsonResultModel <long> {
                ResultState = true, Message = "评论成功!", Body = cid
            }));
        }