public void ShoppingCartUpdated_WhenShoppingCartChanged()
        {
            var navigationService = new MockNavigationService();
            var accountService = new MockAccountService();
            var shoppingCartRepository = new MockShoppingCartRepository();
            shoppingCartRepository.GetShoppingCartAsyncDelegate = () =>
            {
                ShoppingCart shoppingCart = new ShoppingCart(new ObservableCollection<ShoppingCartItem>()) { Currency = "USD"};
                return Task.FromResult(shoppingCart);
            };
            var eventAggregator = new MockEventAggregator();
            var shoppingCartUpdatedEvent = new ShoppingCartUpdatedEvent();
            eventAggregator.GetEventDelegate = type => shoppingCartUpdatedEvent;
            var target = new ShoppingCartPageViewModel(shoppingCartRepository, navigationService, accountService, null, null, null, null, null, eventAggregator);
            target.OnNavigatedTo(null, NavigationMode.New, null);  

            Assert.AreEqual("$0.00", target.FullPrice);

            shoppingCartRepository.GetShoppingCartAsyncDelegate = () =>
            {
                var shoppingCartItems = new ObservableCollection<ShoppingCartItem>
                                            {
                                                new ShoppingCartItem() { Product = new Product { ListPrice = 100, ProductNumber = "p1", ImageUri = new Uri("http://image")}, Currency = "USD", Quantity = 2}, 
                                            };
                ShoppingCart shoppingCart = new ShoppingCart(new ObservableCollection<ShoppingCartItem>(shoppingCartItems)) { Currency = "USD" };
                return Task.FromResult(shoppingCart);
            };

            shoppingCartUpdatedEvent.Publish(null);

            Assert.AreEqual("$200.00", target.FullPrice);

        }
        public void VMListensToShoppingCartUpdatedEvent_ThenCalculatesTotalNumberOfItemsInCart()
        {
            var shoppingCart = new ShoppingCart(new List<ShoppingCartItem>());
            var shoppingCartRepository = new MockShoppingCartRepository();

            shoppingCartRepository.GetShoppingCartAsyncDelegate = () => Task.FromResult(shoppingCart);
            var shoppingCartUpdatedEvent = new ShoppingCartUpdatedEvent();
            var eventAggregator = new MockEventAggregator();
            eventAggregator.GetEventDelegate = type =>
            {
                if (type == typeof(ShoppingCartUpdatedEvent)) return shoppingCartUpdatedEvent;
                if (type == typeof(ShoppingCartItemUpdatedEvent)) return new ShoppingCartItemUpdatedEvent();
                return null;
            };
            var accountService = new MockAccountService();
            accountService.GetSignedInUserAsyncDelegate = () => Task.FromResult((UserInfo)null);
            var target = new ShoppingCartTabUserControlViewModel(shoppingCartRepository, eventAggregator, null, new AlertMessageService(), null, accountService);

            shoppingCart = new ShoppingCart(new List<ShoppingCartItem>()
                                               {
                                                   new ShoppingCartItem {Quantity = 1},
                                                   new ShoppingCartItem {Quantity = 2}
                                               });

            Assert.AreEqual(0, target.ItemCount);

            shoppingCartUpdatedEvent.Publish(null);

            Assert.AreEqual(3, target.ItemCount);

        }
        public void OnNavigatedTo_Fill_Properties_With_Shopping_Cart_Items()
        {
            var navigationService = new MockNavigationService();
            var shoppingCartRepository = new MockShoppingCartRepository();
            shoppingCartRepository.GetShoppingCartAsyncDelegate = () =>
            {
                ShoppingCart shoppingCart = null;
                var shoppingCartItems = new ObservableCollection<ShoppingCartItem>
                                            {
                                                new ShoppingCartItem() {Product = new Product { ListPrice = 100, ProductNumber = "p1", ImageUri = new Uri("http://image")}, Currency = "USD", Quantity = 1}, 
                                                new ShoppingCartItem() {Product = new Product { ListPrice = 100, ProductNumber = "p2", ImageUri = new Uri("http://image")}, Currency = "USD", Quantity = 1}
                                            };
                shoppingCart = new ShoppingCart(shoppingCartItems) { Currency = "USD"};

                return Task.FromResult(shoppingCart);
            };
            var eventAggregator = new MockEventAggregator();
            eventAggregator.GetEventDelegate = type => new MockShoppingCartUpdatedEvent();
            var target = new ShoppingCartPageViewModel(shoppingCartRepository, navigationService, new MockAccountService(), null, null, null, null, null, eventAggregator);
            target.OnNavigatedTo(null, NavigationMode.New, null);

            Assert.AreEqual("$200.00", target.FullPrice);
            Assert.AreEqual("$100.00", target.TotalDiscount);
            Assert.AreEqual(2, target.ShoppingCartItemViewModels.Count);
        }
        public async Task<Order> CreateOrderAsync(string userId, ShoppingCart shoppingCart, Address shippingAddress,
                                                  Address billingAddress, PaymentMethod paymentMethod, ShippingMethod shippingMethod)
        {
            Order order = new Order
                {
                    UserId = userId,
                    ShoppingCart = shoppingCart,
                    ShippingAddress = shippingAddress,
                    BillingAddress = billingAddress,
                    PaymentMethod = paymentMethod,
                    ShippingMethod = shippingMethod
                };

            order.Id = await _orderService.CreateOrderAsync(order, _accountService.ServerCookieHeader);

            return order;
        }
        async void _accountService_UserChanged(object sender, UserChangedEventArgs e)
        {
            _cachedShoppingCart = null;
            if (e.NewUserInfo != null)
            {
                if (e.OldUserInfo == null)
                {
                    await _shoppingCartService.MergeShoppingCartsAsync(_shoppingCartId, e.NewUserInfo.UserName);
                }
                _shoppingCartId = e.NewUserInfo.UserName;                
            }
            else
            {
                _shoppingCartId = Guid.NewGuid().ToString();
            }

            _suspensionManagerState.SessionState[ShoppingCartIdKey] = _shoppingCartId;
            RaiseShoppingCartUpdated();
        }
        public void OnNavigatedTo_Fill_Properties_No_Shopping_Cart_Items()
        {
            var navigationService = new MockNavigationService();
            var shoppingCartRepository = new MockShoppingCartRepository();
            shoppingCartRepository.GetShoppingCartAsyncDelegate = () =>
            {
                var shoppingCartItems = new List<ShoppingCartItem>();
                shoppingCartItems.Add(new ShoppingCartItem { Quantity = 1, Product = new Product { ListPrice = 200}, Currency = "USD" });
                ShoppingCart shoppingCart = new ShoppingCart(new ObservableCollection<ShoppingCartItem>(shoppingCartItems)) { Currency = "USD" };
                return Task.FromResult(shoppingCart);
            };
            var eventAggregator = new MockEventAggregator();
            eventAggregator.GetEventDelegate = type => new MockShoppingCartUpdatedEvent();

            var target = new ShoppingCartPageViewModel(shoppingCartRepository, navigationService, new MockAccountService(), null, null, null, null, null, eventAggregator);
            target.OnNavigatedTo(null, NavigationMode.New, null);

            Assert.AreEqual("$200.00", target.FullPrice);
            Assert.AreEqual("$100.00", target.TotalDiscount);
            Assert.AreEqual(1, target.ShoppingCartItemViewModels.Count);
        }
        public async Task<ShoppingCart> GetShoppingCartAsync()
        {
            if (_cachedShoppingCart != null) return _cachedShoppingCart;

            ShoppingCart cart = await _shoppingCartService.GetShoppingCartAsync(_shoppingCartId);
            if (cart == null) return null;

            foreach (var shoppingCartItem in cart.ShoppingCartItems)
            {
                //Update ImageName with path to local instance of image.
                var product = await _productCatalogRepository.GetProductAsync(shoppingCartItem.Product.ProductNumber);
                shoppingCartItem.Product.ImageUri = product.ImageUri;
            }
            _cachedShoppingCart = cart;
            return cart;
        }
 public async Task ClearCartAsync()
 {
     _cachedShoppingCart = null;
     await _shoppingCartService.DeleteShoppingCartAsync(_shoppingCartId);
     RaiseShoppingCartUpdated();
 }
 public async Task RemoveShoppingCartItemAsync(string itemId)
 {
     _cachedShoppingCart = null;
     await _shoppingCartService.RemoveShoppingCartItemAsync(_shoppingCartId, itemId);
     RaiseShoppingCartItemUpdated();
 }
 public async Task RemoveProductFromShoppingCartAsync(string productId)
 {
     _cachedShoppingCart = null;
     await _shoppingCartService.RemoveProductFromShoppingCartAsync(_shoppingCartId, productId);
     RaiseShoppingCartItemUpdated();
 }
 public async Task AddProductToShoppingCartAsync(string productId)
 {
     _cachedShoppingCart = null;
     await _shoppingCartService.AddProductToShoppingCartAsync(_shoppingCartId, productId);
     RaiseShoppingCartItemUpdated();
 }
 public async Task CreateBasicOrderAsync(string userId, ShoppingCart shoppingCart, Address shippingAddress, Address billingAddress, PaymentMethod paymentMethod)
 {
     var basicShippingMethod = await _shippingMethodService.GetBasicShippingMethodAsync();
     _currentOrder = await CreateOrderAsync(userId, shoppingCart, shippingAddress, billingAddress, paymentMethod, basicShippingMethod);
     _suspensionManagerState.SessionState[OrderKey] = _currentOrder;
 }
        public async void UpdateShoppingCartAsync(object notUsed)
        {
            _shoppingCart = await _shoppingCartRepository.GetShoppingCartAsync();

            if (_shoppingCart != null && _shoppingCart.ShoppingCartItems != null)
            {
                ShoppingCartItemViewModels = new ObservableCollection<ShoppingCartItemViewModel>();
                foreach (var item in _shoppingCart.ShoppingCartItems)
                {
                    var shoppingCartItemViewModel = new ShoppingCartItemViewModel(item);
                    shoppingCartItemViewModel.PropertyChanged += shoppingCartItemViewModel_PropertyChanged;
                    ShoppingCartItemViewModels.Add(shoppingCartItemViewModel);
                }
                CheckoutCommand.RaiseCanExecuteChanged();
                OnPropertyChanged("FullPrice");
                OnPropertyChanged("TotalDiscount");
                OnPropertyChanged("TotalPrice");
            }
        }
        public async Task GetShoppingCartAsync_CachesCart()
        {

            var shoppingCart = new ShoppingCart(new Collection<ShoppingCartItem>());
            var shoppingCartService = new MockShoppingCartService()
                {
                    GetShoppingCartAsyncDelegate = s => Task.FromResult(shoppingCart)
                };

            var target = new ShoppingCartRepository(shoppingCartService, null, null, null, new MockSuspensionManagerState());
            var firstCartReturned = await target.GetShoppingCartAsync();
            shoppingCartService.GetShoppingCartAsyncDelegate = s =>
            {
                Assert.Fail("Should not have called proxy second time.");
                return Task.FromResult((ShoppingCart)null);
            };

            var secondCartReturned = await target.GetShoppingCartAsync();
            Assert.AreSame(shoppingCart, firstCartReturned);
            Assert.AreSame(shoppingCart, secondCartReturned);
        }
 public Task<Order> CreateOrderAsync(string userId, ShoppingCart shoppingCart, Address shippingAddress, Address billingAddress, PaymentMethod paymentMethod, ShippingMethod shippingMethod)
 {
     return CreateOrderAsyncDelegate(userId, shoppingCart, shippingAddress, billingAddress, paymentMethod, shippingMethod);
 }