Exemplo n.º 1
0
        public async Task <bool> AddProductToSessionCart(ISession session, string productId, int quantity)
        {
            var sessionCart = this.GetOrCreateSessionCart(session);

            if (!sessionCart.Products.Any(x => x.Id == productId))
            {
                // if a product with provided id doesn't exist get the product and add it to the cart
                var product = await this.productService.GetProductForCart(productId);

                product.Quantity = quantity;

                product.TotalPrice = (double)(product.Price * product.Quantity);

                sessionCart.Products.Add(product);
            }
            else
            {
                // if a product exists change its quantity and total price
                var product = sessionCart.Products.SingleOrDefault(p => p.Id == productId);
                sessionCart.Products.Remove(product);

                product.Quantity   += quantity;
                product.TotalPrice += (double)(product.Price * quantity);

                sessionCart.Products.Add(product);
            }

            sessionCart.TotalPrice = sessionCart.Products.Sum(p => p.TotalPrice);

            SessionExtensions.SetObjectAsJson(session, GlobalConstants.ShoppingCartSession, sessionCart);

            return(true);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public async Task <bool> RemoveProductFromSessionCart(ISession session, string productId)
        {
            var sessionCart = this.GetOrCreateSessionCart(session);

            var product = sessionCart.Products.SingleOrDefault(p => p.Id == productId);

            sessionCart.Products.Remove(product);

            sessionCart.TotalPrice = sessionCart.Products.Sum(p => p.TotalPrice);

            SessionExtensions.SetObjectAsJson(session, GlobalConstants.ShoppingCartSession, sessionCart);

            return(true);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public async Task <bool> UpdateSessionProductCount(ISession session, string productId, int quantity)
        {
            var sessionCart = this.GetOrCreateSessionCart(session);

            var product = sessionCart.Products.SingleOrDefault(p => p.Id == productId);

            sessionCart.Products.Remove(product);

            product.Quantity = quantity;

            product.TotalPrice = (double)(product.Price * product.Quantity);

            sessionCart.Products.Add(product);

            sessionCart.TotalPrice = sessionCart.Products.Sum(p => p.TotalPrice);

            SessionExtensions.SetObjectAsJson(session, GlobalConstants.ShoppingCartSession, sessionCart);

            return(true);
        }