示例#1
0
        public async Task SubtractProductAsync(string browserId, string productId, int count)
        {
            CoreValidator.ThrowIfAnyNullOrWhitespace(browserId, productId);

            var shoppingCart = await this.shoppingCartRepository.GetAsync(browserId);

            CoreValidator.ThrowIfNull(shoppingCart);

            var orderItems = shoppingCart.Items;
            var item       = orderItems.FirstOrDefault(i => i.Product.Id == productId && i.Count > 0);

            if (item == null)
            {
                return;
            }

            item.Count -= count;
            if (item.Count <= 0)
            {
                orderItems = orderItems.Where(oi => oi.Product.Id != productId).ToList();
            }

            shoppingCart.UpdateItems(orderItems);

            await this.shoppingCartRepository.UpdateAsync(shoppingCart);
        }
示例#2
0
        public async Task AddProductAsync(string browserId, string productId, int count)
        {
            CoreValidator.ThrowIfAnyNullOrWhitespace(browserId, productId);

            var shoppingCart = await this.shoppingCartRepository.GetAsync(browserId);

            if (shoppingCart == null)
            {
                var newCart = this.shoppingCartFactory.CreateInstance(browserId);
                await this.shoppingCartRepository.AddAsync(newCart);

                shoppingCart = await this.shoppingCartRepository.GetAsync(browserId);
            }

            var product = await this.productRepository.GetAsync(productId);

            CoreValidator.ThrowIfNull(product);

            if (product.IsDeleted)
            {
                throw new InvalidOperationException($"Product with id: {productId} is deleted!");
            }

            var item = shoppingCart.Items.FirstOrDefault(i => i.Product.Id == productId);

            if (item == null)
            {
                item = new OrderItem
                {
                    Product = product,
                    Count   = 0
                };
            }

            item.Count += count;
            if (item.Count > item.Product.Availability)
            {
                return;
            }

            var newItems = shoppingCart.Items.Where(i => i.Product.Id != productId).ToList();

            newItems.Add(item);

            shoppingCart.UpdateItems(newItems);

            await this.shoppingCartRepository.UpdateAsync(shoppingCart);
        }
示例#3
0
        public async Task RemoveProductAsync(string browserId, string productId)
        {
            CoreValidator.ThrowIfAnyNullOrWhitespace(browserId, productId);

            var shoppingCart = await this.shoppingCartRepository.GetAsync(browserId);

            CoreValidator.ThrowIfNull(shoppingCart);

            var orderItems = shoppingCart.Items;
            var item       = orderItems.FirstOrDefault(i => i.Product.Id == productId);

            if (item == null)
            {
                return;
            }

            var newItems = orderItems.Where(oi => oi.Product.Id != productId).ToList();

            shoppingCart.UpdateItems(newItems);

            await this.shoppingCartRepository.UpdateAsync(shoppingCart);
        }