예제 #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);
        }
예제 #2
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);
        }
예제 #3
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);
        }