public IActionResult OnPost()
        {
            if (Quantity > 0)
            {
                OnGet();

                if (signInManager.IsSignedIn(User))
                {
                    var userId = User.FindFirstValue(ClaimTypes.NameIdentifier).ToString();
                    ShoppingCart = cartRepository.GetCart(userId);
                }
                else
                {
                    ShoppingCart = cartRepository.GetCart(null, HttpContext);
                }

                this.Product = productRepository.GetProduct(1);

                if (signInManager.IsSignedIn(User))
                {
                    var userId = User.FindFirstValue(ClaimTypes.NameIdentifier).ToString();
                    cartRepository.AddItem(ShoppingCart, Product.ID, Quantity, userId, null);
                }
                else
                {
                    cartRepository.AddItem(ShoppingCart, Product.ID, Quantity, null, HttpContext);
                }
                AddToCartMessage = "Your items have been added to the cart!";
            }
            return(Page());
        }
Exemple #2
0
        public Result AddItem(Product product, int quantity)
        {
            var result = new Result();

            try
            {
                if (_shoppingCartRepository.HasItemByProductID(product.ID))
                {
                    _shoppingCartRepository.IncreaseItemQuantityByProductID(product.ID, quantity);
                }
                else
                {
                    _shoppingCartRepository.AddItem(new CartItem(product, quantity));
                }

                ApplyCampaignToCart();
                ApplyCouponToCart();
                ApplyDeliveryCost();

                result.IsSucceed = true;
            }
            catch (Exception ex)
            {
                _logger.Log(ex);
            }

            return(result);
        }
        ///<inheritdoc/>
        public void Handle(AddItemToShoppingCartCommand command)
        {
            IShoppingCartRepository repository = SystemObjectFactory.GetInstance <IShoppingCartRepository>();
            ShoppingCart            basket;

            if (command.ShoppingCartId != null && command.ShoppingCartId != Guid.Empty)
            {
                basket = repository.GetById(command.ShoppingCartId.Value);
            }
            else
            {
                basket = repository.GetShoppingCartByUserId(command.UserId.Value);
            }

            ShoppingCart result = repository.AddItem(command.ProductId, command.Quantity, basket);

            command.SetResult(result);
        }
Exemple #4
0
 public void AddItem(IShoppingCart cart, IProduct product, int qty = 1)
 {
     _shoppingCartRepository.AddItem(cart, product, qty);
 }