public async Task <IActionResult> AddNewComment(int newsId, [FromBody] Comment comment) { try { var token = Request.Headers["Authorization"].ToString(); var news = await NewsService.GetByIdAsync <News>(newsId); if (news == null) { return(NotFound()); } comment.News = news; var entryId = await CommentService.AddAsync(comment); if (entryId > 0) { await CommentService.PublishEntity <Comment>(entryId, JwtHelper.GetUserIdFromJwt(token), JwtHelper.GetUserNameFromJwt(token)); } return(Ok(new Response { Status = ResponseType.Successful, Value = comment })); } catch { return(Ok(new Response { Status = ResponseType.Failed })); } }
public async Task AddAsync_CorrectComment_AddToDB() { //arrange BLComment comment = new BLComment() { Body = "Body", GameId = 1, Name = "Name" }; _commentRepository.Setup(cr => cr.InsertAsync(It.IsAny <Comment>())).Returns(Task.CompletedTask).Verifiable(); _unitOfWork.Setup(cr => cr.SaveAsync()).Returns(Task.CompletedTask).Verifiable(); //act await _commentService.AddAsync(comment); //assert _commentRepository.Verify(cs => cs.InsertAsync(It.IsAny <Comment>())); _unitOfWork.Verify(cs => cs.SaveAsync()); }
async Task ExecuteCommentCommand() { await CommentService.AddAsync(Story.Id, CommentDescription, Services.Setting.UserId); Story.CommentCount++; await ExecuteLoadCommentsCommand(); CommentDescription = ""; }
public void Add_ShouldThrowErrorWhenPhotoDoesNotExist() { var user = UserCreator.Create("test"); var photo = PhotoCreator.Create(user, false, true); var photosRepo = DeletableEntityRepositoryMock.Get <Photo>(new List <Photo>()); var commentsRepo = EfRepositoryMock.Get <Comment>(new List <Comment>()); var service = new CommentService(photosRepo.Object, commentsRepo.Object); Exception ex = Assert.Throws <AggregateException>(() => service.AddAsync(photo.Id, user.Id, "comment").Wait()); Assert.Contains("Photo does not exists!", ex.Message); }
public void Add_ShouldAddSucessfully() { var user = UserCreator.Create("test"); var photo = PhotoCreator.Create(user, false, true); var comments = new List <Comment>(); var photosRepo = DeletableEntityRepositoryMock.Get <Photo>(new List <Photo>() { photo }); var commentsRepo = EfRepositoryMock.Get <Comment>(comments); var service = new CommentService(photosRepo.Object, commentsRepo.Object); service.AddAsync(photo.Id, user.Id, "comment").Wait(); Assert.Single(comments); }
public async Task Test_Create_Comment_Async() { _mockSet.As <IDbAsyncEnumerable <CommentDb> >().Setup(b => b.GetAsyncEnumerator()).Returns(new TestDbAsyncEnumerator <CommentDb>(_commentsDb.GetEnumerator())); _mockSet.As <IQueryable <CommentDb> >().Setup(b => b.Provider).Returns(new TestDbAsyncQueryProvider <CommentDb>(_commentsDb.Provider)); _mockContext.Setup(c => c.Comments).Returns(_mockSet.Object); _mockRepo = new CommentRepository(_mockContext.Object); _mockMapper.Setup(m => m.Map <CommentInfo, CommentDb>(_oneCommentInfo)).Returns(_oneCommentDb); _service = new CommentService(_mockMapper.Object, _mockRepo); var result = await _service.AddAsync(_oneCommentInfo); _mockSet.Verify(b => b.Add(It.IsAny <CommentDb>()), Times.Once()); _mockContext.Verify(b => b.SaveChangesAsync(), Times.Once()); result.IsSuccess.Should().BeTrue(); }
public void AddAsync_InvalidOperationExceptionTest() { _uowMock.Setup(x => x.Comments.CreateAsync(It.IsAny <Comment>())).ReturnsAsync((Comment)null); Assert.That(async() => await _service.AddAsync(null), Throws.InstanceOf <OperationException>()); }
public async Task <IActionResult> Add(CommentPostDto commentPostDto) { await _commentService.AddAsync(commentPostDto); return(Created("Add", commentPostDto)); }