Exemplo n.º 1
0
        public void Process(ItemAddedToCart @event)
        {
            var cart = _carts.GetById(@event.Cart);

            cart.Add(@event.Product, @event.Quantity, new Price
            {
                Net   = @event.NetItemPrice,
                Gross = @event.GrossItemPrice
            });
            _carts.Save(cart);
        }
Exemplo n.º 2
0
        public bool Add(AddProduct addProduct, TemporaryReservation temporaryReservation)
        {
            var cart = _carts.Get(addProduct.CartId);

            if (cart.Add(addProduct.ProductId, addProduct.Quantity, temporaryReservation))
            {
                _carts.Save(cart);
                return(true);
            }
            return(false);
        }
Exemplo n.º 3
0
        public void Process_WhenCartExists_ShouldAddProductToCart()
        {
            // Arrange
            var productId = Guid.NewGuid();
            var product   = new ItemAddedToCart
            {
                GrossItemPrice = 129,
                NetItemPrice   = 99,
                Product        = productId,
                Quantity       = 1
            };

            // Act
            _cartEventProcessors.Process(product);

            // Assert
            Assert.Single(_testCart.Lines);
            Assert.Equal(1, _testCart.Lines.Single().Quantity);
            Assert.Equal(129, _testCart.Lines.Single().Price.Gross);
            Assert.Equal(99, _testCart.Lines.Single().Price.Net);
            Assert.Equal(productId, _testCart.Lines.Single().Product);

            A.CallTo(() => _fakeCarts.Save(A <Cart> ._)).MustHaveHappened(Repeated.Exactly.Once);
        }
Exemplo n.º 4
0
        public HttpResponseMessage AddProduct(AddProduct addProduct)
        {
            var productStock = _productStocks.Get(addProduct.ProductId);

            return(productStock
                   .MakeATemporaryReservation_(addProduct.Quantity)
                   .ContinueWith(temporaryReservation =>
            {
                _productStocks.Save(productStock);
                var cart = _carts.Get(addProduct.CartId);
                return cart.Add_(addProduct.ProductId, addProduct.Quantity, temporaryReservation);
            })
                   .ContinueWith(cart =>
            {
                _carts.Save(cart);
                return Either <HttpResponseMessage, Error> .Left(new HttpResponseMessage(HttpStatusCode.OK));
            })
                   .OnError(error => new HttpResponseMessage(HttpStatusCode.BadRequest))
                   .Result());
        }
Exemplo n.º 5
0
        public HttpResponseMessage AddProduct(AddProduct addProduct)
        {
            var productStock = _productStocks.Get(addProduct.ProductId);

            try
            {
                var temporaryReservation = productStock.MakeATemporaryReservation(addProduct.Quantity);
                _productStocks.Save(productStock);
                var cart = _carts.Get(addProduct.CartId);
                cart.Add(addProduct.ProductId, addProduct.Quantity, temporaryReservation);
                _carts.Save(cart);
            }
            catch (BusinessException e)
            {
                var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.Forbidden)
                {
                    Content = new StringContent(e.Message)
                };
                return(httpResponseMessage);
            }
            return(new HttpResponseMessage(HttpStatusCode.Created));
        }
Exemplo n.º 6
0
        public HttpResponseMessage AddProduct(AddProduct addProduct)
        {
            var productStock         = _productStocks.Get(addProduct.ProductId);
            var temporaryReservation = productStock.MakeATemporaryReservation(addProduct.Quantity);

            if (temporaryReservation != null)
            {
                _productStocks.Save(productStock);
                var cart = _carts.Get(addProduct.CartId);
                if (cart.Add(addProduct.ProductId, addProduct.Quantity, temporaryReservation))
                {
                    _carts.Save(cart);
                }
                else
                {
                    return(Error("Sorry, cannot add to cart"));
                }
            }
            else
            {
                return(Error("Sorry, not enough stock"));
            }
            return(new HttpResponseMessage(HttpStatusCode.Created));
        }