public async Task <IActionResult> AddComment(CommentAddDTO commentAddDTO) { commentAddDTO.PostedTime = DateTime.Now; await _commentService.AddAsync(_mapper.Map <Comment>(commentAddDTO)); return(Created("", commentAddDTO)); }
public async Task <CommentDTO> AddCommentAsync(CommentAddDTO commentAddDTO, int userId) { if (commentAddDTO == null) { throw null; } var photo = await _unit.Photos.GetByIdAsync(commentAddDTO.PhotoId); if (photo == null) { throw new ValidationException("Photo was not found"); } Comment comment = _mp.Map <Comment>(commentAddDTO); comment.UserId = userId; await _unit.Comments.AddAsync(comment); await _unit.SaveAsync(); var commentDTO = _mp.Map <CommentDTO>(comment); var user = await _unit.UserManager.FindByIdAsync(comment.UserId.ToString()); commentDTO.UserName = user.UserName; return(commentDTO); }
public async Task <ActionResult <CommentDTO> > PostComment(int id, [FromBody] CommentAddDTO commentAddDTO) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (commentAddDTO.PhotoId != id) { return(BadRequest()); } CommentDTO commentDTO; try { commentDTO = await _commentService.AddCommentAsync(commentAddDTO, UserId); } catch (Exception e) { return(BadRequest(e.Message)); } return(Ok(commentDTO)); }
public void AddCommentAsync_WrongPhotoId_Should_ThrowException() { var dto = new CommentAddDTO { PhotoId = 1, Text = "text" }; mockUow.Setup(uow => uow.Photos.GetByIdAsync(dto.PhotoId)).ReturnsAsync((Photo)null); mockHelper.Setup(helper => helper.ThrowPhotoGalleryNotFoundExceptionIfModelIsNull( It.IsAny <Photo>())).Throws <PhotoGalleryNotFoundException>(); service.Invoking(s => s.AddCommentAsync(dto)).Should().Throw <PhotoGalleryNotFoundException>(); }
public async Task AddCommentAsync_Should_ReturnCommentDTO() { var dto = new CommentAddDTO { PhotoId = 1, Text = "text" }; mockUow.Setup(uow => uow.Photos.GetByIdAsync(dto.PhotoId)).ReturnsAsync(new Photo()); mockUow.Setup(uow => uow.Comments.AddAsync(It.IsAny <Comment>())); var result = await service.AddCommentAsync(dto); result.Should().BeEquivalentTo(dto); }
public async Task AddCommentAsync_Should_AddCommentToUow() { var dto = new CommentAddDTO { PhotoId = 1, Text = "text" }; mockUow.Setup(uow => uow.Photos.GetByIdAsync(dto.PhotoId)).ReturnsAsync(new Photo()); mockUow.Setup(uow => uow.Comments.AddAsync(It.Is <Comment>(c => c.Text == dto.Text))).Verifiable(); await service.AddCommentAsync(dto); mockUow.Verify(); }
public async Task <CommentDTO> AddCommentAsync(CommentAddDTO commentAddDTO) { var photo = await unitOfWork.Photos.GetByIdAsync(commentAddDTO.PhotoId); helper.ThrowPhotoGalleryNotFoundExceptionIfModelIsNull(photo); var comment = mapper.Map <Comment>(commentAddDTO); await unitOfWork.Comments.AddAsync(comment); await unitOfWork.SaveAsync(); var commentDTO = mapper.Map <CommentDTO>(comment); return(commentDTO); }