Esempio n. 1
0
        public IActionResult CreateReply(int commentId,
                                         [FromBody] ReplyForCreationDto reply)
        {
            if (reply == null)
            {
                return(BadRequest());
            }

            if (reply.Description == reply.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_commentRepository.CommentExists(commentId))
            {
                return(NotFound());
            }
            var finalReply = Mapper.Map <Entities.Reply>(reply);

            _commentRepository.AddReplyForComment(commentId, finalReply);

            if (!_commentRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

            //comment.Replies.Add(finalReply);
            var createdReplyToReturn = Mapper.Map <Models.ReplyDto>(finalReply);

            return(CreatedAtRoute("GetReply", new
                                  { commentId = commentId, id = createdReplyToReturn.Id }, createdReplyToReturn));

            //commentId, id = finalReply.Id }, finalReply);
        }