public IActionResult GetComment(int userId, int blogId, int postId, int commentId, [FromHeader(Name = nameof(HeaderNames.Accept))] string mediaType) { if (!_weblogDataRepository.UserExists(userId) || !_weblogDataRepository.BlogExists(blogId) || !_weblogDataRepository.PostExists(postId)) { return(NotFound()); } var commentFromRepo = _weblogDataRepository.GetComment(commentId); if (commentFromRepo is null) { return(NotFound()); } var commentToReturn = _mapper.Map <CommentDto>(commentFromRepo); var includeLinks = MediaTypes.IncludeLinks(mediaType); if (!includeLinks) { return(Ok(commentToReturn)); } var links = CreateLinksForComment(userId, blogId, postId, commentToReturn.CommentId, commentToReturn.UserId); var commentWithLinks = new CommentDtoWithLinks(commentToReturn, links); return(Ok(commentWithLinks)); }
public IActionResult CreateComment(int userId, int blogId, int postId, [FromBody] CommentForCreationDto comment, [FromHeader(Name = nameof(HeaderNames.Accept))] string mediaType) { if (!_weblogDataRepository.UserExists(userId) || !_weblogDataRepository.BlogExists(blogId) || !_weblogDataRepository.PostExists(postId)) { return(NotFound()); } if (!_weblogDataRepository.UserExists((int)comment.UserId)) { // adding comment with userId that doesn't exist ModelState.AddModelError(nameof(comment.UserId), "UserId does not exist."); return(ErrorHandler.UnprocessableEntity(ModelState, HttpContext)); } var emailAddress = comment.Credentials.EmailAddress; var password = comment.Credentials.Password; if (!_weblogDataRepository.Authorized((int)comment.UserId, emailAddress, password)) { return(Unauthorized()); } var commentEntity = _mapper.Map <Entities.Comment>(comment); _weblogDataRepository.AddComment(postId, commentEntity); _weblogDataRepository.Save(); var commentToReturn = _mapper.Map <CommentDto>(commentEntity); var includeLinks = MediaTypes.IncludeLinks(mediaType); if (!includeLinks) { return(CreatedAtRoute ( nameof(GetComment), new { userId, blogId, postId, commentId = commentToReturn.CommentId }, commentToReturn )); } var links = CreateLinksForComment(userId, blogId, postId, commentToReturn.CommentId, commentToReturn.UserId); var commentWithLinks = new CommentDtoWithLinks(commentToReturn, links); return(CreatedAtRoute ( nameof(GetComment), new { userId, blogId, postId, commentId = commentToReturn.CommentId }, commentWithLinks )); }