Exemplo n.º 1
0
        public virtual ActionResult Add(int id, string returnUrl)
        {
            var product = _productRepository.GetById(id);

            if (product == null)
            {
                throw new HttpException((int)HttpStatusCode.NotFound, Resources.Error_NotFound_Product);
            }

            var cartItem = _cartItemRepository.GetById(id);

            if (cartItem != null)
            {
                cartItem.Count++;
            }
            else
            {
                cartItem = new CartItemModel {
                    Id        = product.Id,
                    Title     = product.Title,
                    Count     = 1,
                    UnitPrice = product.UnitPrice
                };
            }

            _cartItemRepository.AddOrUpdate(cartItem);
            _cartItemRepository.SaveChanges();

            product.AddedToCartCount++;

            _productRepository.AddOrUpdate(product);
            _productRepository.SaveChanges();

            return(RedirectedAsync(returnUrl, CartOperationModel.Succeeded(_cartItemRepository.GetItemsCount(), Resources.Success_AddedToCart)));
        }
Exemplo n.º 2
0
        public virtual ActionResult Remove(int id, bool removeAll, string returnUrl)
        {
            var product = _productRepository.GetById(id);

            if (product == null)
            {
                throw new HttpException((int)HttpStatusCode.NotFound, Resources.Error_NotFound_Product);
            }

            var cartItem = _cartItemRepository.GetById(id);

            if (cartItem != null)
            {
                if (removeAll)
                {
                    product.AddedToCartCount--;
                    cartItem.Count = 0;
                }
                else
                {
                    product.AddedToCartCount--;
                    cartItem.Count--;
                }

                if (cartItem.Count == 0)
                {
                    _cartItemRepository.Delete(new CartItemModel {
                        Id        = product.Id,
                        Title     = product.Title,
                        UnitPrice = product.UnitPrice
                    });
                }

                _cartItemRepository.SaveChanges();

                _productRepository.AddOrUpdate(product);
                _productRepository.SaveChanges();
            }

            return(RedirectedAsync(returnUrl, CartOperationModel.Succeeded(_cartItemRepository.GetItemsCount(), Resources.Success_RemovedFromCart)));
        }