public void AddPromotionPrice_ShouldAddPriceToProductInShopAndSetPropertyIsPromotionOnTrue()
        {
            var product       = new Product("Test", "TestVendor", 1, Enums.SizeUnits.l, 1);
            var productInShop = ProductInShop.CreateProductInShop(product, 1);

            var price = new Price();

            productInShop.AddPromotionPrice(price);

            productInShop.Prices.Should().NotBeEmpty();
            productInShop.Prices.Should().Contain(price);
            price.IsPromotionPrice.Should().BeTrue();
        }
        public void CreateProductInShop_ShouldCreateProductInShop()
        {
            var product = new Product("Test", "TestVendor", 1, Enums.SizeUnits.l, 1);
            var shopId  = 1;

            var productInShop = ProductInShop.CreateProductInShop(product, shopId);

            productInShop.Should().NotBeNull();
            productInShop.Product.Should().NotBeNull();
            productInShop.Product.Should().BeSameAs(product);
            productInShop.ShopId.Should().Be(shopId);

            product.ProductInShops.Should().NotBeEmpty();
            product.ProductInShops.Should().Contain(productInShop);
        }
예제 #3
0
        private static async Task CreateProducts(ShoppingHelperDbContext dbContext, ILogger <ShoppingHelperContextSeed> logger)
        {
            if (await dbContext.Products.AnyAsync())
            {
                logger.LogInformation("Products are already inserted.");
                return;
            }

            var laptop     = new Product("Laptop", "Asus", null, null, 1);
            var appleJuice = new Product("Apple juice", "AppleVendor", 2, Domain.Enums.SizeUnits.l, 2);

            dbContext.Products.Add(laptop);
            dbContext.Products.Add(appleJuice);

            await dbContext.SaveChangesAsync();

            var laptopInAmazon = ProductInShop.CreateProductInShop(laptop, 1);

            laptopInAmazon.AddBasePrice(new Price {
                PriceValue = 3333
            });

            var laptopInSuperShop = ProductInShop.CreateProductInShop(laptop, 2);

            laptopInSuperShop.AddBasePrice(new Price {
                PriceValue = 3000
            });

            var appleJuiceInSuperShop = ProductInShop.CreateProductInShop(appleJuice, 2);

            appleJuiceInSuperShop.AddBasePrice(new Price {
                PriceValue = 4, PricePerSizeUnit = 2, SizeUnit = Domain.Enums.SizeUnits.l
            });
            appleJuiceInSuperShop.AddPromotionPrice(new Price {
                PriceValue = 2.50m, PricePerSizeUnit = 1.25m, SizeUnit = Domain.Enums.SizeUnits.l, PromotionConstraints = "If you use special coupon."
            });

            dbContext.ProductsInShops.Add(laptopInAmazon);
            dbContext.ProductsInShops.Add(laptopInSuperShop);
            dbContext.ProductsInShops.Add(appleJuiceInSuperShop);

            await dbContext.SaveChangesAsync();

            logger.LogInformation("Products created.");
        }
예제 #4
0
            public async Task <int> Handle(CreateProductCommand request, CancellationToken cancellationToken)
            {
                await this.validator.ValidateAndThrowAsync(request, cancellationToken);

                if (await this.categoryRepository.GetByIdAsync(request.CategoryId) == null)
                {
                    throw new NotFoundException(nameof(Category));
                }

                if (await this.shopRepository.GetByIdAsync(request.ShopId) == null)
                {
                    throw new NotFoundException(nameof(Shop));
                }

                var product = new Product(request.Name, request.Vendor, request.Size?.Value, request.Size?.Unit.ToEnum <SizeUnits>(), request.CategoryId);

                if (await this.productRepository.DoesProductExist(product))
                {
                    throw new BadRequestException($"Product '{product.Name}({product.Vendor})' already exist.");
                }

                if (await this.productInShopRepository.DoesProductExistInShop(product, request.ShopId))
                {
                    throw new BadRequestException($"Product '{product.Name}({product.Vendor})' already exist in shop.");
                }

                var productInShop = ProductInShop.CreateProductInShop(product, request.ShopId);

                productInShop.AddBasePrice(mapper.Map <Price>(request.BasePrice));

                if (request.PromotionPrice != null)
                {
                    productInShop.AddPromotionPrice(mapper.Map <Price>(request.PromotionPrice));
                }

                await this.productInShopRepository.AddAsync(productInShop);

                return(product.Id);
            }