Exemple #1
0
        public IHttpActionResult AddToCart(
            Guid id,
            int version,
            int productId,
            int count = 1)
        {
            var command = new AddCartItem(
                cartId: id,
                productId: productId,
                count: count,
                originalVersion: version);

            ServiceLocator.Bus.Send(command);

            return(Ok());
        }
        public async Task <IActionResult> Add(string userId, [FromBody] AddCartItem request)
        {
            var exception = new Exception();

            try
            {
                IUserActors actor = GetActor(userId);
                await actor.AddToCart(request.ProductId, request.Quantity);

                return(Accepted());
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return(BadRequest(exception));
        }
        public async Task <ActionResult> AddItemToList([FromBody] AddCartItem listItem)
        {
            //Need lock to prevent concurrent access to same list
            using (await AsyncLock.GetLockByKey(GetAsyncLockCartKey(WorkContext, listItem.ListName, listItem.Type)).LockAsync())
            {
                var cartBuilder = await LoadOrCreateCartAsync(listItem.ListName, listItem.Type);

                var products = await _catalogService.GetProductsAsync(new[] { listItem.ProductId }, Model.Catalog.ItemResponseGroup.Inventory | Model.Catalog.ItemResponseGroup.ItemWithPrices);

                if (products != null && products.Any())
                {
                    await cartBuilder.AddItemAsync(products.First(), 1);

                    await cartBuilder.SaveAsync();
                }
                return(Json(new { ItemsCount = cartBuilder.Cart.ItemsQuantity }));
            }
        }
        public async Task <ActionResult> AddItemToCart([FromBody] AddCartItem cartItem)
        {
            EnsureCartExists();

            //Need lock to prevent concurrent access to same cart
            using (await AsyncLock.GetLockByKey(WorkContext.CurrentCart.Value.GetCacheKey()).LockAsync())
            {
                var cartBuilder = await LoadOrCreateCartAsync();

                var products = await _catalogService.GetProductsAsync(new[] { cartItem.ProductId }, Model.Catalog.ItemResponseGroup.Inventory | Model.Catalog.ItemResponseGroup.ItemWithPrices);

                if (products != null && products.Any())
                {
                    await cartBuilder.AddItemAsync(products.First(), cartItem.Quantity);

                    await cartBuilder.SaveAsync();
                }
                return(Json(new { ItemsCount = cartBuilder.Cart.ItemsQuantity }));
            }
        }
Exemple #5
0
        public override async Task <bool> AddItemAsync(AddCartItem addCartItem)
        {
            EnsureCartExists();

            var result = await new AddCartItemValidator(Cart).ValidateAsync(addCartItem, ruleSet: Cart.ValidationRuleSet);

            if (result.IsValid)
            {
                var lineItem = addCartItem.Product.ToLineItem(Cart.Language, addCartItem.Quantity);
                lineItem.Product           = addCartItem.Product;
                lineItem.ConfiguredGroupId = addCartItem.ConfiguredGroupId;

                if (addCartItem.Price != null)
                {
                    var listPrice = new Money(addCartItem.Price.Value, Cart.Currency);
                    lineItem.ListPrice = listPrice;
                    lineItem.SalePrice = listPrice;
                }

                if (!string.IsNullOrEmpty(addCartItem.Comment))
                {
                    lineItem.Comment = addCartItem.Comment;
                }

                if (!addCartItem.DynamicProperties.IsNullOrEmpty())
                {
                    lineItem.DynamicProperties = new MutablePagedList <DynamicProperty>(addCartItem.DynamicProperties.Select(x => new DynamicProperty
                    {
                        Name   = x.Key,
                        Values = new[] { new LocalizedString {
                                             Language = Cart.Language, Value = x.Value
                                         } }
                    }));
                }

                await AddLineItemAsync(lineItem);
            }

            return(result.IsValid);
        }
        public async Task ValidateAddItem_RuleSetDefault_Invalid()
        {
            //Arrange
            var cart = GetValidCart();

            var addItem = new AddCartItem()
            {
                Quantity = 0
            };

            //Act
            var validator = new AddCartItemValidator(cart);

            var result = await validator.ValidateAsync(addItem, ruleSet : "default");

            //Assertion
            Assert.False(result.IsValid);
            Assert.NotEmpty(result.Errors);
            Assert.Collection(result.Errors, x => { Assert.Equal(nameof(addItem.Quantity), x.PropertyName); Assert.Equal(nameof(GreaterThanValidator), x.ErrorCode); }
                              , x => { Assert.Equal(nameof(addItem.Product), x.PropertyName); Assert.Equal(nameof(NotNullValidator), x.ErrorCode); }
                              );
        }
Exemple #7
0
 public async Task <IActionResult> AddToCart(AddCartItem addCartItem)
 {
     return(View());
 }