Esempio n. 1
0
        public void Test_CommentReplyRequest_Validate_Throws_Exceptions()
        {
            // request body is null
            var request = new CommentReplyRequest {
                Id = "123"
            };

            Action act = () => request.Validate();

            act.Should().Throw <ArgumentNullException>();

            // id is null
            request = new CommentReplyRequest {
                RequestBody = new TraktCommentReplyPost()
            };

            act = () => request.Validate();
            act.Should().Throw <ArgumentNullException>();

            // empty id
            request = new CommentReplyRequest {
                Id = string.Empty, RequestBody = new TraktCommentReplyPost()
            };

            act = () => request.Validate();
            act.Should().Throw <ArgumentException>();

            // id with spaces
            request = new CommentReplyRequest {
                Id = "invalid id", RequestBody = new TraktCommentReplyPost()
            };

            act = () => request.Validate();
            act.Should().Throw <ArgumentException>();
        }
Esempio n. 2
0
        public async Task <ApiResult <CommentSubPageResponse> > AddReplyCommentAsync([FromBody] CommentReplyRequest request)
        {
            var userId = default(long?);
            var user   = GetUser();

            if (user != null)
            {
                userId = user.UserId;
            }
            return(await _commentService.ArticleSubCommentAsync(request, userId));
        }
Esempio n. 3
0
        /// <summary>
        /// 修改评论信息
        /// </summary>
        /// <param name="userid">创建者</param>
        /// <param name="commentReplyRequest">请求实体</param>
        /// <param name="cancellationToken">验证</param>
        /// <returns></returns>
        public virtual async Task UpdateAsync(string userid, CommentReplyRequest commentReplyRequest, CancellationToken cancellationToken = default(CancellationToken))
        {
            var oldcommenreply = await _icommentReplyStore.GetAsync(a => a.Where(b => b.Id == commentReplyRequest.Id && b.CustomerId == userid), cancellationToken);

            if (oldcommenreply == null)
            {
                return;
            }
            oldcommenreply.Content     = commentReplyRequest.Content;
            oldcommenreply.IsAnonymous = commentReplyRequest.IsAnonymous;
            await _icommentReplyStore.UpdateAsync(oldcommenreply, cancellationToken);
        }
Esempio n. 4
0
        public void Test_CommentReplyRequest_Returns_Valid_UriPathParameters()
        {
            // only id
            var request = new CommentReplyRequest {
                Id = "123", RequestBody = new TraktCommentReplyPost()
            };

            request.GetUriPathParameters().Should().NotBeNull()
            .And.HaveCount(1)
            .And.Contain(new Dictionary <string, object>
            {
                ["id"] = "123"
            });
        }
Esempio n. 5
0
        public async Task <ResponseMessage> UpdateCommentReply(UserInfo user, [FromBody] CommentReplyRequest commentReplyRequest)
        {
            ResponseMessage <CommentResponse> response = new ResponseMessage <CommentResponse>();

            if (!ModelState.IsValid)
            {
                response.Code    = ResponseCodeDefines.ModelStateInvalid;
                response.Message = ModelState.GetAllErrors();
                Logger.LogWarning($"用户{user?.UserName ?? ""}({user?.Id ?? ""})修改回复(UpdateCommentReply)模型验证失败:\r\n{response.Message ?? ""},\r\n请求参数为:\r\n" + (commentReplyRequest != null ? JsonHelper.ToJson(commentReplyRequest) : ""));
                return(response);
            }
            try
            {
                await _commentReplyManager.UpdateAsync(user.Id, commentReplyRequest, HttpContext.RequestAborted);
            }
            catch (Exception e)
            {
                response.Code    = ResponseCodeDefines.ServiceError;
                response.Message = e.Message;
                Logger.LogError($"用户{user?.UserName ?? ""}({user?.Id ?? ""})修改回复(UpdateCommentReply)请求失败:\r\n{response.Message ?? ""},\r\n请求参数为:\r\n" + (commentReplyRequest != null ? JsonHelper.ToJson(commentReplyRequest) : ""));
            }
            return(response);
        }
Esempio n. 6
0
        /// <summary>
        /// 新增评论信息
        /// </summary>
        /// <param name="userid">创建者</param>
        /// <param name="commentReplyRequest">请求实体</param>
        /// <param name="cancellationToken">验证</param>
        /// <returns></returns>
        public virtual async Task <CommentDetailResponse> CreateAsync(string userid, CommentReplyRequest commentReplyRequest, CancellationToken cancellationToken = default(CancellationToken))
        {
            var commentreply = _mapper.Map <CommentReply>(commentReplyRequest);
            var comment      = await _icommentStore.GetAsync(a => a.Where(b => b.Id == commentreply.CommentId), cancellationToken);

            if (comment == null)
            {
                return(null);
            }

            FilterHelper filter = new FilterHelper();

            commentReplyRequest.Content = filter.Filter(commentReplyRequest.Content);
            commentreply.Id             = Guid.NewGuid().ToString();
            commentreply.CreateTime     = DateTime.Now;
            commentreply.CustomerId     = userid;

            comment.ReplyNum++;

            var response = await _icommentReplyStore.CreateAsync(commentreply, comment, cancellationToken);

            return(_mapper.Map <CommentDetailResponse>(commentreply));
        }
Esempio n. 7
0
        public void Test_CommentReplyRequest_Returns_Valid_RequestObjectType()
        {
            var request = new CommentReplyRequest();

            request.RequestObjectType.Should().Be(RequestObjectType.Comments);
        }
Esempio n. 8
0
        public void Test_CommentReplyRequest_Has_AuthorizationRequirement_Required()
        {
            var requestMock = new CommentReplyRequest();

            requestMock.AuthorizationRequirement.Should().Be(AuthorizationRequirement.Required);
        }
Esempio n. 9
0
        public void Test_CommentReplyRequest_Has_Valid_UriTemplate()
        {
            var request = new CommentReplyRequest();

            request.UriTemplate.Should().Be("comments/{id}/replies");
        }
Esempio n. 10
0
        /// <summary>
        /// 回复评论
        /// </summary>
        /// <param name="request"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task <ApiResult <CommentSubPageResponse> > ArticleSubCommentAsync(CommentReplyRequest request, long?userId)
        {
            var response = new ApiResult <CommentSubPageResponse>();

            try
            {
                var comment = new Comment(true)
                {
                    ArticleId      = request.ArticleId,
                    Content        = request.Content,
                    CreateTime     = DateTime.Now,
                    IsReply        = 1,
                    ReplyCommentId = request.ReplyCommentId,
                    Status         = 1
                };

                var replyComment = await _commentRepository.TableNotTracking
                                   .FirstOrDefaultAsync(item => item.Id == request.ReplyCommentId);

                if (replyComment == null)
                {
                    response.Code    = Code.Error;
                    response.Message = "查无回复评论";
                    return(response);
                }
                if (replyComment.IsReply == 1)
                {
                    //回复评论为子评论
                    comment.IsSubReply        = 1;
                    comment.ReplySubCommentId = replyComment.Id;
                    comment.ReplySubUserId    = replyComment.UserId;
                    comment.ReplySubUserName  = replyComment.UserName;
                    comment.ReplyCommentId    = replyComment.ReplyCommentId;
                }
                else
                {
                    //回复评论为主评论
                    comment.IsSubReply     = 0;
                    comment.ReplyCommentId = replyComment.Id;
                }

                if (userId.HasValue)
                {
                    comment.UserId   = userId.Value;
                    comment.UserName = (await _userRepository.TableNotTracking.FirstOrDefaultAsync(item => item.Id == userId.Value))?.UserName;
                }
                else
                {
                    comment.UserName = request.UserName;
                }
                await IncArticleCommentCountAsync(request.ArticleId);

                await _commentRepository.InsertAsync(comment);

                await _efContextWork.SaveChangesAsync();

                var result = new CommentSubPageResponse
                {
                    Id                = comment.Id,
                    ArticleId         = comment.ArticleId,
                    UserId            = comment.UserId,
                    UserName          = comment.UserName,
                    Content           = comment.Content,
                    CreateTime        = comment.CreateTime,
                    SubReplyCommentId = comment.ReplySubCommentId,
                    SubReplyUserId    = comment.ReplySubUserId,
                    SubReplyUserName  = comment.ReplySubUserName,
                    Like              = comment.Like,
                    Reply             = comment.Reply
                };

                response.Code    = Code.Ok;
                response.Message = "评论成功";
                response.Data    = comment.MapTo <CommentSubPageResponse>();
                return(response);
            }
            catch (Exception ex)
            {
                _logger.LogError($"添加回复评论异常;method={nameof(ArticleCommentAsync)};param={request.ToJson()};exception messges={ex.Message}");
                response.Code    = Code.Error;
                response.Message = $"添加回复评论异常:{ex.Message}";
                return(response);
            }
        }