public virtual IEnumerable<ShipmentViewModel> CreateShipmentsViewModel(ICart cart) { foreach (var shipment in cart.GetFirstForm().Shipments) { var shipmentModel = new ShipmentViewModel { ShipmentId = shipment.ShipmentId, CartItems = new List<CartItemViewModel>(), Address = _addressBookService.ConvertToModel(shipment.ShippingAddress), ShippingMethods = CreateShippingMethodViewModels(cart.Market, cart.Currency, shipment) }; shipmentModel.ShippingMethodId = shipment.ShippingMethodId == Guid.Empty ? shipmentModel.ShippingMethods.First().Id : shipment.ShippingMethodId; var variants = _contentLoader.GetItems(shipment.LineItems.Select(x => _referenceConverter.GetContentLink(x.Code)), _preferredCulture).OfType<VariationContent>(); foreach (var lineItem in shipment.LineItems) { var variant = variants.Single(x => x.Code == lineItem.Code); var productLink = variant.GetParentProducts(_relationRepository).FirstOrDefault(); if (ContentReference.IsNullOrEmpty(productLink)) { continue; } shipmentModel.CartItems.Add(_cartItemViewModelFactory.CreateCartItemViewModel(cart, lineItem, variant)); } yield return shipmentModel; } }
public void CreateShipmentsViewModel_ShouldReturnViewModel() { var viewModel = _subject.CreateShipmentsViewModel(_cart).First(); var expectedViewModel = new ShipmentViewModel { Address = _addressModel, CartItems = new List<CartItemViewModel> { _cartItem }, ShippingMethods = new List<ShippingMethodViewModel> { new ShippingMethodViewModel { Id = _shippingRate.Id, DisplayName = _shippingRate.Name, Price = _shippingRate.Money} }, ShippingMethodId = _shippingRate.Id }; viewModel.ShouldBeEquivalentTo(expectedViewModel); }