示例#1
0
        public async Task CreateOrder_ReturnOkResult_WhenCreateOrderSucceeds()
        {
            //Arrange
            var userId      = 1;
            var basketItems = new List <BasketItem>()
            {
                new BasketItem(10, 1, 1),
                new BasketItem(20, 1, 1)
            };

            var createOrderResource = new CreateOrderResource()
            {
                BasketItems = basketItems
            };

            _mockItemService.SetupSequence(service => service.GetItemById(It.IsAny <int>()))
            .ReturnsAsync(new Item())
            .ReturnsAsync(new Item());
            _mockOrderService.Setup(service => service.CreateOrder(It.IsAny <Order>()))
            .ReturnsAsync(new Order(userId));
            var controller = new OrderController(_mockItemService.Object, _mockOrderItemService.Object, _mockOrderService.Object);

            SetupHttpContextUser(controller, _user);

            //Act
            var actionResult = await controller.CreateOrder(createOrderResource);

            //Assert
            Assert.IsType <OkObjectResult>(actionResult);
        }
示例#2
0
        public async Task <IActionResult> CreateOrder([FromBody] CreateOrderResource createOrderResource)
        {
            var userId   = getUserId();
            var order    = new Order(userId);
            var newOrder = await _orderService.CreateOrder(order);

            var basketItems = createOrderResource.BasketItems;

            foreach (BasketItem basketItem in basketItems)
            {
                var item = await _itemService.GetItemById(basketItem.ItemId);

                if (item == null)
                {
                    return(NotFound());
                }

                await _orderItemService.CreateOrderItem(new OrderItem(order.Id, item.Id, basketItem.Quantity, item.Price));

                // update item quantity
                var updatedQuantity = item.Quantity - basketItem.Quantity;
                await _itemService.UpdateQuantity(basketItem.ItemId, updatedQuantity);
            }

            return(Ok(newOrder));
        }
示例#3
0
        public async Task CreateOrder_ReturnNotFoundResult_WhenItemIsNotFound()
        {
            //Arrange
            var basketItems = new List <BasketItem>()
            {
                new BasketItem(10, 1, 1),
                new BasketItem(20, 1, 1)
            };

            var createOrderResource = new CreateOrderResource()
            {
                BasketItems = basketItems
            };

            _mockItemService.Setup(service => service.GetItemById(It.IsAny <int>()))
            .ReturnsAsync((Item)null);
            var controller = new OrderController(_mockItemService.Object, _mockOrderItemService.Object, _mockOrderService.Object);

            SetupHttpContextUser(controller, _user);

            //Act
            var actionResult = await controller.CreateOrder(createOrderResource);

            //Assert
            Assert.IsType <NotFoundResult>(actionResult);
        }