public async Task <OrderViewModel> Handle(GetOrderDetails request,
                                              CancellationToken cancellationToken)
    {
        var spec  = new OrderWithItemsByIdSpec(request.OrderId);
        var order = await _orderRepository.GetBySpecAsync(spec, cancellationToken);

        if (order == null)
        {
            return(null);
        }

        return(new OrderViewModel
        {
            OrderDate = order.OrderDate,
            OrderItems = order.OrderItems.Select(oi => new OrderItemViewModel
            {
                PictureUrl = oi.ItemOrdered.PictureUri,
                ProductId = oi.ItemOrdered.CatalogItemId,
                ProductName = oi.ItemOrdered.ProductName,
                UnitPrice = oi.UnitPrice,
                Units = oi.Units
            }).ToList(),
            OrderNumber = order.Id,
            ShippingAddress = order.ShipToAddress,
            Total = order.Total()
        });
    }
예제 #2
0
        public async Task GetOrderAndItemsByOrderIdWhenMultipleOrdersPresent()
        {
            //Arrange
            var itemOneUnitPrice = 5.50m;
            var itemOneUnits     = 2;
            var itemTwoUnitPrice = 7.50m;
            var itemTwoUnits     = 5;

            var firstOrder = OrderBuilder.WithDefaultValues();

            _catalogContext.Orders.Add(firstOrder);
            int firstOrderId = firstOrder.Id;

            var secondOrderItems = new List <OrderItem>();

            secondOrderItems.Add(new OrderItem(OrderBuilder.TestCatalogItemOrdered, itemOneUnitPrice, itemOneUnits));
            secondOrderItems.Add(new OrderItem(OrderBuilder.TestCatalogItemOrdered, itemTwoUnitPrice, itemTwoUnits));
            var secondOrder = OrderBuilder.WithItems(secondOrderItems);

            _catalogContext.Orders.Add(secondOrder);
            int secondOrderId = secondOrder.Id;

            _catalogContext.SaveChanges();

            //Act
            var spec          = new OrderWithItemsByIdSpec(secondOrderId);
            var orderFromRepo = await _orderRepository.GetBySpecAsync(spec);

            //Assert
            Assert.Equal(secondOrderId, orderFromRepo.Id);
            Assert.Equal(secondOrder.OrderItems.Count, orderFromRepo.OrderItems.Count);
            Assert.Equal(1, orderFromRepo.OrderItems.Count(x => x.UnitPrice == itemOneUnitPrice));
            Assert.Equal(1, orderFromRepo.OrderItems.Count(x => x.UnitPrice == itemTwoUnitPrice));
            Assert.Equal(itemOneUnits, orderFromRepo.OrderItems.SingleOrDefault(x => x.UnitPrice == itemOneUnitPrice).Units);
            Assert.Equal(itemTwoUnits, orderFromRepo.OrderItems.SingleOrDefault(x => x.UnitPrice == itemTwoUnitPrice).Units);
        }