Пример #1
0
        public async Task <bool> TransferSessionCartToAccountCart(string username, ISession session)
        {
            var sessionCart =
                SessionExtensions.GetObjectFromJson <ShoppingCartViewModel>(session, GlobalConstants.ShoppingCartSession);

            if (sessionCart == null)
            {
                // this happens if the cart is not being used as a session
                return(false);
            }

            string shoppingCartId = await this.GetOrCreateUserShoppingCartByUsername(username);

            var products = AutoMapper.Mapper.Map <List <ShoppingCartProduct> >(sessionCart.Products);

            products.ForEach(p => p.ShoppingCartId = shoppingCartId);

            // Remove the user previously added products to the cart and add the new session ones.
            var productToRemove = await this.context
                                  .ShoppingCartProducts
                                  .Where(x => x.ShoppingCartId == shoppingCartId)
                                  .ToArrayAsync();

            this.context.ShoppingCartProducts.RemoveRange(productToRemove);

            await this.context.ShoppingCartProducts.AddRangeAsync(products);

            var result = await this.context.SaveChangesAsync();

            return(result > 0);
        }
Пример #2
0
        public ShoppingCartViewModel GetOrCreateSessionCart(ISession session)
        {
            var sessionCart =
                SessionExtensions.GetObjectFromJson <ShoppingCartViewModel>(session, GlobalConstants.ShoppingCartSession);

            if (sessionCart == null)
            {
                sessionCart = new ShoppingCartViewModel();
            }

            if (sessionCart.Products == null)
            {
                sessionCart.Products = new List <ShoppingCartProductViewModel>();
            }

            return(sessionCart);
        }