Example #1
0
        public JsonResult Add(AddCommentViewModel addCommentViewModel, AddCommentValidator addCommentValidator)
        {
            Throw.IfNull(addCommentViewModel, nameof(addCommentViewModel));
            Throw.IfNull(addCommentValidator, nameof(addCommentValidator));

            var modelState = addCommentValidator.Validate(addCommentViewModel);
            var validation = new List <string>();

            if (modelState.IsValid)
            {
                _logger.Debug($"{nameof(Add)} has been called with valid model.");

                _commentService.Add(new Comment
                {
                    AuthorName = addCommentViewModel.AuthorName,
                    Text       = addCommentViewModel.Text,
                    EventId    = addCommentViewModel.EventId.GetValueOrDefault()
                });
            }
            else
            {
                foreach (var error in modelState.Errors)
                {
                    validation.Add(error.ErrorMessage);
                }

                _logger.Warn($"{nameof(Add)} has been called with invalid model.");
            }

            return(Json(validation));
        }
        public IActionResult Post([FromBody] AddCommentDto dto
                                  , [FromServices] IAddCommentCommand command
                                  , [FromServices] AddCommentValidator validator)
        {
            var result = validator.Validate(dto);

            if (result.IsValid)
            {
                Comment comment = _mapper.Map <Comment>(dto);
                _useCaseExecutor.ExecuteCommand(command, comment);
                return(Ok("Comment created successfully"));
            }

            return(UnprocessableEntity(UnprocessableEntityResponse.Message(result.Errors)));
        }