Exemple #1
0
        public void BasketsController_Create_Basket_Order_Line_Return_Correct_Order_Line()
        {
            var baskets  = new InMemoryDataStore <Basket>();
            var products = new InMemoryDataStore <Product>();

            products.Seed();
            baskets.Seed(products);

            var orderLine = new OrderLineCreateDto()
            {
                ProductId = new Guid("3aee1758-77b9-4e86-9c57-77adbbc0956f"), //Watermelon
                Quantity  = 5
            };

            var bc           = new BasketsController(products, baskets, _mapper);
            var actionResult = bc.PostAddOrderLineToBasket(_testBasketId, orderLine);

            CreatedAtRouteResult createdresult = actionResult as CreatedAtRouteResult;

            Assert.IsNotNull(createdresult);

            OrderLineDto resultOrderLine = createdresult.Value as OrderLineDto;

            Assert.IsNotNull(resultOrderLine);

            Assert.AreEqual(resultOrderLine.ProductId, orderLine.ProductId);
        }
Exemple #2
0
        public IActionResult PostAddOrderLineToBasket(Guid id, [FromBody] OrderLineCreateDto model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_baskets.Contains(id))
            {
                return(NotFound(id));
            }

            if (!_products.Contains(model.ProductId))
            {
                return(NotFound(model.ProductId));
            }

            var basket    = _baskets.Get(id);
            var product   = _products.Get(model.ProductId);
            var orderLine = new OrderLine()
            {
                Product  = product,
                Quantity = model.Quantity
            };

            //Check to see if we already have an orderline for this product
            if (basket.OrderLines.ContainsKey(model.ProductId))
            {
                //If we already have an orderline for this product, add the new quantity to the existing order line
                basket.OrderLines[model.ProductId].Quantity += orderLine.Quantity;
                //return the updated orderline
                orderLine = basket.OrderLines[model.ProductId];
            }
            else
            {
                basket.OrderLines.Add(model.ProductId, orderLine);
            }

            return(CreatedAtRoute("GetBasketOrderLine", new { id = id, productId = model.ProductId }, _mapper.Map <OrderLineDto>(orderLine)));
        }