public void should_not_add_reply_when_no_content()
        {
            // Arrange
            var topic           = _app.NewTopic().WithAuthor(_app.MockUser()).Create();
            var replyController = _app.CreateController <ReplyController>();

            // Act
            var model = new ReplyCreationModel {
                Content = string.Empty
            };

            replyController.TryValidateModel(model);
            var replyResult = replyController.Reply(topic.Id, model);

            // Assert
            var statusCodeResult = replyResult as BadRequestObjectResult;

            Assert.NotNull(statusCodeResult);
            Assert.Equal(400, statusCodeResult.StatusCode);

            var errors = statusCodeResult.Value as SerializableError;

            Assert.NotNull(errors);
            Assert.Contains("必须填写回复内容", errors.Values.Cast <string[]>().SelectMany(err => err).ToList());
        }
        public IActionResult Reply(int topicId, ReplyCreationModel replyCreationModel)
        {
            var topic = _topicRepo.Get(topicId);

            if (topic == null)
            {
                ModelState.AddModelError("TopicId", "话题不存在");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            topic.LastRepliedAt = DateTime.UtcNow;
            topic.ReplyCount   += 1;
            _topicRepo.Update(topic);

            var reply = new Reply
            {
                TopicId   = topicId,
                CreatedBy = User.ExtractUserId().Value,
                Content   = replyCreationModel.Content
            };

            _replyRepo.Save(reply);
            return(NoContent());
        }
Exemple #3
0
        public void should_validate_empty_content_as_invalid()
        {
            var replyModel = new ReplyCreationModel();

            var modelState = _myApp.ValidateModel(replyModel);

            Assert.False(modelState.IsValid);
        }
Exemple #4
0
        public IActionResult Reply(int topicId, ReplyCreationModel replyCreationModel)
        {
            var currentUser = HttpContext.DiscussionUser();

            if (!_siteSettings.CanAddNewReplies())
            {
                _logger.LogWarning("添加回复失败:{@ReplyAttempt}",
                                   new { currentUser.UserName, Result = new FeatureDisabledException().Message });
                return(BadRequest());
            }

            if (_siteSettings.RequireUserPhoneNumberVerified && !currentUser.PhoneNumberId.HasValue)
            {
                _logger.LogWarning("添加回复失败:{@ReplyAttempt}",
                                   new { currentUser.UserName, Result = new UserVerificationRequiredException().Message });
                return(BadRequest());
            }

            var topic = _topicRepo.Get(topicId);

            if (topic == null)
            {
                var errorMessage = "话题不存在";
                _logger.LogWarning("添加回复失败:{@ReplyAttempt}", new { currentUser.UserName, Result = errorMessage });
                ModelState.AddModelError("TopicId", errorMessage);
            }

            if (!ModelState.IsValid)
            {
                _logger.LogModelState("添加回复", ModelState, currentUser.Id, currentUser.UserName);
                return(BadRequest(ModelState));
            }

            var reply = new Reply
            {
                TopicId   = topicId,
                CreatedBy = currentUser.Id,
                Content   = replyCreationModel.Content
            };

            _replyRepo.Save(reply);

            // ReSharper disable once PossibleNullReferenceException
            topic.LastRepliedAt     = _clock.Now.UtcDateTime;
            topic.LastRepliedByUser = currentUser;
            topic.ReplyCount       += 1;
            _topicRepo.Update(topic);

            _logger.LogInformation("添加回复成功:{@ReplyAttempt}",
                                   new
            {
                TopicId = topic.Id, topic.ReplyCount, ReplyId = reply.Id, UserId = currentUser.Id,
                currentUser.UserName
            });
            return(NoContent());
        }