public async Task Order_has_been_placed_for_customer() { var currency = Currency.CanadianDollar; var productPrice = 12.5; var productQuantity = 10; var customerEmail = "*****@*****.**"; var productMoney = Money.Of(Convert.ToDecimal(productPrice), currency.Code); _currencyConverter.Convert(currency, Money.Of(Convert.ToDecimal(productPrice * productQuantity), currency.Code)) .Returns(productMoney); var customerUniquenessChecker = Substitute.For <ICustomerUniquenessChecker>(); customerUniquenessChecker.IsUserUnique(customerEmail).Returns(true); var customerId = new CustomerId(Guid.NewGuid()); var customer = Customer.CreateNew(customerEmail, "Customer X", customerUniquenessChecker); _customers.GetById(Arg.Any <CustomerId>()).Returns(customer); var product = Product.CreateNew("Product X", productMoney); _products.GetById(Arg.Any <ProductId>()).Returns(product); var productData = new QuoteItemProductData(product.Id, product.Price, productQuantity); var quote = Quote.CreateNew(customerId); quote.AddItem(productData); List <Product> products = new List <Product>() { product }; _quotes.GetById(quote.Id).Returns(quote); _products.GetByIds(Arg.Any <List <ProductId> >()).Returns(products); var placeOrderCommandHandler = new PlaceOrderCommandHandler(_unitOfWork, _currencyConverter); var placeOrderCommand = new PlaceOrderCommand(quote.Id.Value, customerId.Value, currency.Code); var orderResult = await placeOrderCommandHandler.Handle(placeOrderCommand, CancellationToken.None); await _unitOfWork.Received(1).CommitAsync(Arg.Any <CancellationToken>()); orderResult.Should().NotBe(Guid.Empty); }
public QuoteItem AddItem(QuoteItemProductData productData) { if (productData == null) { throw new BusinessRuleException("The quote item must have a product."); } if (productData.Quantity == 0) { throw new BusinessRuleException("The product quantity must be at last 1."); } var quoteItem = new QuoteItem(Guid.NewGuid(), productData.ProductId, productData.Quantity); _items.Add(quoteItem); return(quoteItem); }
public override async Task <Guid> ExecuteCommand(CreateQuoteCommand command, CancellationToken cancellationToken) { var customerId = new CustomerId(command.CustomerId); var customer = await _unitOfWork.Customers .GetById(customerId, cancellationToken); var productId = new ProductId(command.Product.Id); var product = await _unitOfWork.Products .GetById(productId, cancellationToken); if (customer == null) { throw new ApplicationDataException("Customer not found."); } if (product == null) { throw new ApplicationDataException("Product not found."); } var quantity = command.Product.Quantity; var quotetemProductData = new QuoteItemProductData( product.Id, product.Price, quantity ); var quote = Quote.CreateNew(customerId); quote.AddItem(quotetemProductData); await _unitOfWork.Quotes .Add(quote, cancellationToken); await _unitOfWork.CommitAsync(); return(quote.Id.Value); }
public QuoteItem ChangeItem(QuoteItemProductData productData) { if (productData == null) { throw new BusinessRuleException("The quote item must have a product."); } var quoteItem = _items.FirstOrDefault((Func <QuoteItem, bool>)(i => i.ProductId.Value == productData.ProductId.Value)); if (quoteItem == null) { quoteItem = AddItem(productData); // Add item } else if (productData.Quantity == 0) { RemoveItem(quoteItem.Id); // Remove Item } else { quoteItem.ChangeQuantity(productData.Quantity); // Change item quantity } return(quoteItem); }