Exemplo n.º 1
0
        public async Task AddProductCommentCommandHandle_AddsShopComment()
        {
            //Arrange
            var productCategory = new AllMarkt.Entities.ProductCategory {
                Name = "category", Description = "description"
            };

            AllMarktContextIM.ProductCategories.Add(productCategory);
            AllMarktContextIM.SaveChanges();

            var user = new AllMarkt.Entities.User {
                Email       = "*****@*****.**",
                Password    = "******",
                DisplayName = "UserTest"
            };

            var product = new AllMarkt.Entities.Product
            {
                Name            = "testProduct",
                Description     = "testDescription",
                Price           = 20,
                ImageURI        = "",
                State           = true,
                ProductCategory = productCategory
            };

            AllMarktContextIM.Users.Add(user);
            AllMarktContextIM.Products.Add(product);
            await AllMarktContextIM.SaveChangesAsync();

            var addProductCommentCommand = new AddProductCommentCommand
            {
                Rating        = 5,
                Text          = "cel mai bun produs",
                ProductId     = product.Id,
                AddedByUserId = user.Id
            };

            //Act
            await _addProductCommentCommandHandler.Handle(addProductCommentCommand, CancellationToken.None);

            //Assert
            AllMarktContextIM.ProductComments
            .Should()
            .Contain(productComment =>
                     productComment.Rating == addProductCommentCommand.Rating &&
                     productComment.Text == addProductCommentCommand.Text &&
                     productComment.AddedBy.Id == addProductCommentCommand.AddedByUserId &&
                     productComment.Product.Id == addProductCommentCommand.ProductId);
        }
Exemplo n.º 2
0
        public async Task <AddCommentValidationResult> Validate(AddProductCommentCommand comment)
        {
            if (comment == null)
            {
                throw new ArgumentNullException(nameof(comment));
            }

            var validationResult = Validate(comment.ProductId, comment.Content, comment.Score, Constants.DtoNames.Comment.ProductId);

            if (!validationResult.IsValid)
            {
                return(validationResult);
            }

            ProductAggregate product = null;

            // Check shop exists && 1 comment.
            if (!string.IsNullOrWhiteSpace(comment.ProductId))
            {
                product = await _productRepository.Get(comment.ProductId);

                if (product == null)
                {
                    return(new AddCommentValidationResult(ErrorDescriptions.TheShopDoesntExist));
                }

                var comments = await _productRepository.Search(new SearchProductCommentsParameter
                {
                    ProductId = comment.ProductId,
                    Subject   = comment.Subject
                });

                if (comments.TotalResults > 0)
                {
                    return(new AddCommentValidationResult(ErrorDescriptions.TheCommentAlreadyExists));
                }
            }

            return(new AddCommentValidationResult());
        }