public void AddComment() { //-- arrange var countBeforeAdd = _repository.GetComments(1, _resourceParameters).Count(); var comment = new Comment { UserId = 1, Body = "body", TimeCreated = DateTime.Now }; //-- act _repository.AddComment(1, comment); _repository.Save(); var actual = _repository.GetComments(1, _resourceParameters).Count(); //-- assert Assert.AreEqual(countBeforeAdd + 1, actual); //-- clean up _repository.DeleteComment(comment); _repository.Save(); }
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 )); }