Exemple #1
0
        public virtual async Task DeleteAsync(Guid id)
        {
            var comment = await CommentRepository.GetAsync(id);

            if (comment.CreatorId != CurrentUser.GetId())
            {
                throw new BusinessException();
            }

            await CommentRepository.DeleteAsync(id);
        }
        public void CreateEventComment()
        {
            int newCommentEventID = 0;

            try
            {
                newCommentEventID = commentRepo.PostAsync(new CreateComment()
                {
                    authorId = "1", message = "ola ola", initialDate = DateTime.Now, eventId = 1
                }).Result;
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex.InnerException, typeof(ArgumentException));
            }
            var res = commentRepo.GetByIdAsync(newCommentEventID).Result;

            Assert.AreEqual(res.message, "ola ola");
            var id = commentRepo.DeleteAsync(res).Result;
        }
Exemple #3
0
        public async Task DeleteAsync()
        {
            var comment = Builder <Comment> .CreateNew().Build();

            _commentRepositoryMock.Setup(p => p.DeleteAsync(1))
            .Returns(Task.CompletedTask);

            await _commentRepository.DeleteAsync(1);

            _commentRepositoryMock.Verify(r => r.DeleteAsync(1));
        }
        public async Task Check_DeleteNotExistCommentThrowsException()
        {
            //Arrange
            var cls = new InMemoryAppDbContext();

            using (var context = cls.GetContextWithData())
            {
                ICommentRepository repository = new CommentRepository(context);
                //Act
                Comment existComment = context.Comments.FirstOrDefault();

                await Assert.ThrowsAsync <ArgumentNullException>(() => repository.DeleteAsync(existComment.Id - 100));

                context.Database.EnsureDeleted();
            }
        }
Exemple #5
0
        private async Task <OperationResult <int> > Delete(CreateComment item)
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var comment = await commentRepo.GetByIdAsync(item.id);

                if (comment == null)
                {
                    return new OperationResult <int>()
                           {
                               Success = false, Message = Messages.COMMENT_NOT_EXIST
                           }
                }
                ;
                if (comment.authorId != item.authorId)
                {
                    return new OperationResult <int>()
                           {
                               Success = false, Message = Messages.USER_NO_COMMENT
                           }
                }
                ;

                try
                {
                    var id = await commentRepo.DeleteAsync(comment);

                    scope.Complete();
                    return(new OperationResult <int>()
                    {
                        Success = true, Message = Messages.COMMENT_DELETED, Result = id
                    });
                }
                catch (Exception ex)
                {
                    return(new OperationResult <int>()
                    {
                        Success = false, Message = ex.Message
                    });
                }
            }
        }
    }
}
        public async Task Check_DeleteCommentIsCorrect()
        {
            //Arrange
            var cls = new InMemoryAppDbContext();

            using (var context = cls.GetContextWithData())
            {
                ICommentRepository repository = new CommentRepository(context);
                //Act
                Comment existComment = context.Comments.FirstOrDefault();

                await repository.DeleteAsync(existComment.Id);

                var actual = context.Comments.Find(existComment.Id);
                //Assert
                Assert.NotNull(existComment);
                Assert.Null(actual);
                context.Database.EnsureDeleted();
            }
        }
        public async Task <IActionResult> RemoveCommentById([FromRoute] int id)
        {
            // get the comment to be deleted
            Comment toBeDeleted = await Repository.FindByIdAsync(id);

            if (toBeDeleted is null)
            {
                return(NotFound());
            }

            // verify the current user
            int userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (userId != toBeDeleted.UserId)
            {
                return(BadRequest(new { Err = "You do not have permission to delete this comment!" }));
            }

            // perform deletion
            await Repository.DeleteAsync(toBeDeleted);

            return(Ok());
        }
        public void FromBltoUiDeleteAsync(Guid id)
        {
            var getFromR = _commentRepository.GetByIdAsync(id);

            _commentRepository.DeleteAsync(getFromR.Id);
        }