Example #1
0
        public async Task <IActionResult> Addreply([FromBody] replyViewModel replyViewModel)
        {
            if (string.IsNullOrEmpty(replyViewModel.Original_Evaluate_ID))
            {
                return(Json(new { code = "400", message = "找不到原评论ID" }));
            }
            if (string.IsNullOrWhiteSpace(replyViewModel.Content) || string.IsNullOrEmpty(replyViewModel.Content))
            {
                return(Json(new { code = "400", message = "内容不能为空!" }));
            }

            string super_Evaluate_name = null;

            if (replyViewModel.super_Evaluate_ID != null)
            {
                super_Evaluate_name = _accountService.GetNickName(replyViewModel.super_Evaluate_ID);
                if (super_Evaluate_name == null)
                {
                    return(Json(new { code = "400", message = "被回复人ID错误" }));
                }
            }


            //获取用户ID
            string phone   = HttpContext.User.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.OtherPhone).Value;
            string user_id = _accountService.GetUserID(phone);
            ////获取名字
            //string user_name = _accountService.GetNickName(user_id);

            User user = _accountService.GetAcccount(user_id);

            //防止自己回复自己
            replyViewModel.super_Evaluate_ID = replyViewModel.super_Evaluate_ID == user.ID ? null : replyViewModel.super_Evaluate_ID;
            super_Evaluate_name = super_Evaluate_name == user.Nickname ? null : super_Evaluate_name;

            ////获取原评论
            //Comment comment = await _commentService.GetCommentAsync(replyViewModel.Original_Evaluate_ID);
            //if (comment == null)
            //{
            //    return Json(new { code = "400", message = "原评论ID出现问题" });
            //}
            ////使评论+1(这里可以优化成事件)
            //comment.reply_Count += 1;

            //检查客户端传输的ID是不是错误的
            if (!await _commentService.IsExistAsync(replyViewModel.Original_Evaluate_ID, Domain.Enums.CommentCatetory.evaluate))
            {
                return(Json(new { code = "400", message = "原评论ID出现问题" }));
            }


            string Create_replyid = Guid.NewGuid().ToString("N");


            DateTime Create_Time = DateTime.Now;

            Reply reply = new Reply()
            {
                ID     = Create_replyid,
                UserID = user_id,
                Original_Evaluate_ID = replyViewModel.Original_Evaluate_ID,
                Add_Time             = Create_Time,
                //防止自己回复自己
                Super_Evaluate_ID = replyViewModel.super_Evaluate_ID,
                //防止自己回复自己
                Super_Evaluate_Name = super_Evaluate_name,
                Original_Forum_ID   = replyViewModel.Original_Forum_ID,
                Reply_Content       = replyViewModel.Content
            };

            try
            {
                await _commentService.AddReplyAsync(reply);

                //发布AddReply事件,使评论的ReplyCount+1
                _container.Publish("AddReply", new ReplySumbitEven()
                {
                    category = 2, SubscriberId = replyViewModel.super_Evaluate_ID, publisherId = user_id, forumID = replyViewModel.Original_Forum_ID, commentId = replyViewModel.Original_Evaluate_ID
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.StackTrace);
                return(Json(new { code = "500", message = "出现错误" }));
            }

            replyViewModel.NickName            = user.Nickname;
            replyViewModel.super_Evaluate_Name = super_Evaluate_name;
            replyViewModel.Id          = Create_replyid;
            replyViewModel.Create_Time = Create_Time.ToShortDateString();
            replyViewModel.HeadAddress = user.HeadAddress;

            return(Json(new { code = "200", message = "回复成功", data = replyViewModel }));
        }