public async Task <CocktailCommentDto> CreateAsync(CocktailCommentDto tempCocktailComment)
        {
            if (tempCocktailComment == null)
            {
                throw new BusinessLogicException(ExceptionMessages.CocktailCommentNull);
            }

            var cocktailComment = new CocktailComment
            {
                Id         = tempCocktailComment.Id,
                CocktailId = tempCocktailComment.CocktailId,
                UserId     = tempCocktailComment.UserId,
                Body       = tempCocktailComment.Body,
                CreatedOn  = tempCocktailComment.CreatedOn,
                ModifiedOn = tempCocktailComment.ModifiedOn,
                DeletedOn  = tempCocktailComment.DeletedOn,
                IsDeleted  = tempCocktailComment.IsDeleted
            };

            await this.context.CocktailComments.AddAsync(cocktailComment);

            await this.context.SaveChangesAsync();

            var cocktailCommentDto = this.dtoMapper.MapFrom(cocktailComment);

            return(cocktailCommentDto);
        }
Пример #2
0
        public async Task Create_CocktailComment_Correctly()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(Create_CocktailComment_Correctly));
            var mockDateTimeProvider = new Mock <IDateTimeProvider>();
            var mapper = new Mock <IDtoMapper <CocktailComment, CocktailCommentDto> >();

            var user = new User {
                Id = 1
            };
            var cocktail = new Cocktail {
                Id = 1
            };

            var commentDto = new CocktailCommentDto
            {
                Id          = 1,
                UserId      = 1,
                CocktailId  = 1,
                commentText = "TestComment",
            };

            mapper.Setup(x => x.MapDto(It.IsAny <CocktailComment>())).Returns(commentDto);
            using (var arrangeContext = new CocktailMagicianContext(options))
            {
                await arrangeContext.Cocktails.AddAsync(cocktail);

                await arrangeContext.Users.AddAsync(user);

                await arrangeContext.SaveChangesAsync();
            }
            //Act and Assert
            using (var assertContext = new CocktailMagicianContext(options))
            {
                var sut    = new CocktailCommentService(assertContext, mapper.Object, mockDateTimeProvider.Object);
                var result = await sut.CreateCocktailCommentAsync(commentDto);

                Assert.IsInstanceOfType(result, typeof(CocktailCommentDto));
                Assert.AreEqual(1, result.Id);
                Assert.AreEqual(1, result.CocktailId);
                Assert.AreEqual(1, result.UserId);
                Assert.AreEqual("TestComment", result.commentText);
            }
        }