public IActionResult GetComment(Guid postAuthorId, Guid postId, Guid commentId, string fields, [FromHeader(Name = "Content-Type")] string mediaType) { if (validator.FieldsAreInvalid <CommentOutputDto>(fields)) { return(BadRequest()); } if (AuthorOrPostNotExist(postAuthorId, postId)) { return(NotFound()); } var commentFromRepo = blogRepository.GetCommentForPost(postId, commentId); if (commentFromRepo is null) { return(NotFound()); } InsertAuthorsInto(commentFromRepo); var mappedComment = mapper.Map <CommentOutputDto>(commentFromRepo); var shapedComment = properties.ShapeSingleData(mappedComment, fields); if (IncludeLinks(mediaType)) { var idsSet = new CommentIdsSet(postAuthorId, postId, commentId); var linkedResource = GetLinkedResource(shapedComment, idsSet, fields); return(Ok(linkedResource)); } return(Ok(shapedComment)); }
private IDictionary <string, object> ShapeAndLinkSingleComment (CommentOutputDto commentToReturn, CommentIdsSet idsSet) { var shapedComment = properties.ShapeSingleData(commentToReturn); return(GetLinkedResource(shapedComment, idsSet)); }
public IActionResult CreateComment(Guid postAuthorId, Guid postId, CommentInputDto newComment, [FromHeader(Name = "Content-Type")] string mediaType) { var idsSet = new CommentIdsSet(postAuthorId, postId); newComment.AuthorId = GetUserId(); return(AddComment(idsSet, newComment, IncludeLinks(mediaType))); }
private IActionResult AddComment(CommentIdsSet idsSet, CommentInputDto newComment, bool includeLinks = false) { if (validator.DontMatchRules(newComment as ICommentInputDto, ModelState)) { return(ValidationProblem(ModelState)); } if (AuthorOrPostNotExist(idsSet.postAuthorId, idsSet.postId)) { return(NotFound()); } var commentToAdd = mapper.Map <Comment>(newComment); InsertAuthorsInto(commentToAdd); if (idsSet.commentId != Guid.Empty) { commentToAdd.Id = idsSet.commentId; } blogRepository.AddCommentForPost(idsSet.postId, commentToAdd); blogRepository.SaveChanges(); var mappedComment = mapper.Map <CommentOutputDto>(commentToAdd); idsSet.commentId = mappedComment.Id; dynamic toReturn = mappedComment; if (includeLinks) { toReturn = ShapeAndLinkSingleComment(mappedComment, idsSet); } return(CreatedAtRoute("GetComment", new { idsSet.postAuthorId, idsSet.postId, commentId = mappedComment.Id }, toReturn)); }
public IActionResult UpdateComment(Guid postAuthorId, Guid postId, Guid commentId, CommentInputDto updatedComment, [FromHeader(Name = "Content-Type")] string mediaType) { var idsSet = new CommentIdsSet(postAuthorId, postId, commentId); var commentFromRepo = blogRepository.GetCommentForPost(postId, commentId); if (commentFromRepo is null) { return(AddComment(idsSet, updatedComment, IncludeLinks(mediaType))); } mapper.Map(updatedComment, commentFromRepo); blogRepository.SaveChanges(); if (IncludeLinks(mediaType)) { var mappedComment = mapper.Map <CommentOutputDto>(commentFromRepo); return(Ok(ShapeAndLinkSingleComment(mappedComment, idsSet))); } return(NoContent()); }
public IActionResult PartiallyUpdateComment(Guid postAuthorId, Guid postId, Guid commentId, JsonPatchDocument <CommentInputDto> patchDocument, [FromHeader(Name = "Content-Type")] string mediaType) { if (AuthorOrPostNotExist(postAuthorId, postId)) { return(NotFound()); } var commentFromRepo = blogRepository.GetCommentForPost(postId, commentId); if (commentFromRepo is null) { return(NotFound()); } var commentInputDto = mapper.Map <CommentInputDto>(commentFromRepo); patchDocument.ApplyTo(commentInputDto, ModelState); if (validator.DontMatchRules(commentInputDto as ICommentInputDto, ModelState)) { return(ValidationProblem(ModelState)); } mapper.Map(commentInputDto, commentFromRepo); blogRepository.SaveChanges(); if (IncludeLinks(mediaType)) { var idsSet = new CommentIdsSet(postAuthorId, postId, commentFromRepo.Id); var mappedComment = mapper.Map <CommentOutputDto>(commentFromRepo); return(Ok(ShapeAndLinkSingleComment(mappedComment, idsSet))); } return(NoContent()); }