Beispiel #1
0
        public async Task ShouldCreateFavorite()
        {
            _reviewRepositoryMock.AlreadyExists(Arg.Any <Guid>()).Returns(true);
            _favoriteRepositoryMock.GetByUserIdAndReviewId(Arg.Any <Guid>(), Arg.Any <Guid>()).Returns(null as Favorite);

            Exception exception = await Record.ExceptionAsync(() => _favoriteService.CreateOrDelete(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()));

            Assert.Null(exception);
            await _favoriteRepositoryMock.Received(1).Create(Arg.Any <Favorite>());

            await _favoriteRepositoryMock.Received(1).Save();
        }
        public async Task CreateOrDelete(string userId, string reviewId)
        {
            Guid userIdGuid   = Guid.Parse(userId);
            Guid reviewIdGuid = Guid.Parse(reviewId);

            Favorite insertedFavorite = await _favoriteRepository.GetByUserIdAndReviewId(userIdGuid, reviewIdGuid);

            if (insertedFavorite == null)
            {
                await ThrowIfReviewNotExists(reviewIdGuid);

                Favorite favorite = new Favorite(userIdGuid, reviewIdGuid);
                await _favoriteRepository.Create(favorite);

                await _favoriteRepository.Save();

                return;
            }

            _favoriteRepository.Delete(insertedFavorite);
            await _favoriteRepository.Save();
        }