Пример #1
0
        public async Task ShouldQueryProductsAsync()
        {
            Response <ProductQueryResult> response = await _client.Products().QueryProductsAsync();

            Assert.IsTrue(response.Success);

            ProductQueryResult productQueryResult = response.Result;

            Assert.NotNull(productQueryResult.Results);
            Assert.GreaterOrEqual(productQueryResult.Results.Count, 1);

            int limit = 2;

            response = await _client.Products().QueryProductsAsync(limit: limit);

            Assert.IsTrue(response.Success);

            productQueryResult = response.Result;
            Assert.NotNull(productQueryResult.Results);
            Assert.LessOrEqual(productQueryResult.Results.Count, limit);
        }
        /// <summary>
        /// Run Cart example code.
        /// </summary>
        /// <param name="client">Client</param>
        /// <param name="project">Project</param>
        public async static Task Run(Client client, Project.Project project)
        {
            string currency = project.Currencies[0];
            string country  = project.Countries[0];

            /*  CREATE CART
             *  ===================================================================================
             */
            CartDraft       cartDraft    = new CartDraft(currency);
            Response <Cart> cartResponse = await client.Carts().CreateCartAsync(cartDraft);

            Cart cart = null;

            if (cartResponse.Success)
            {
                cart = cartResponse.Result;
                Console.WriteLine("Created new cart with ID {0}.", cart.Id);
            }
            else
            {
                Helper.WriteError <Cart>(cartResponse);
            }

            /*  GET CARTS
             *  ===================================================================================
             */
            Response <CartQueryResult> cartQueryResponse = await client.Carts().QueryCartsAsync();

            List <Cart> carts = new List <Cart>();

            if (cartQueryResponse.Success)
            {
                CartQueryResult cartQueryResult = cartQueryResponse.Result;
                carts = cartQueryResult.Results;

                Console.WriteLine("Retrieved {0} carts.", carts.Count);
            }
            else
            {
                Helper.WriteError <CartQueryResult>(cartQueryResponse);
            }

            // Get a single cart by its ID.
            string cartId = carts[0].Id;

            cartResponse = await client.Carts().GetCartByIdAsync(cartId);

            if (cartResponse.Success)
            {
                cart = cartResponse.Result;
                Console.WriteLine("Retrieved cart with ID {0}.", cart.Id);
            }
            else
            {
                Helper.WriteError <Cart>(cartResponse);
            }

            /*  UPDATE A CART
             *  ===================================================================================
             *  Each change is made using its own update action object which maps to an update
             *  action call in the API. The list of update action objects are sent to the API using
             *  a single request. If there is an update action in the API that has not yet been
             *  implemented in the SDK, you can use the GenericAction class to make any request you
             *  want (as long as it is a valid update action supported by the API).
             */
            SetCountryAction setCountryAction = new SetCountryAction();

            setCountryAction.Country = country;

            // Here is how you would make the setCustomerEmail request using a GenericAction object.
            GenericAction setCustomerEmailAction = new GenericAction("setCustomerEmail");

            setCustomerEmailAction["email"] = "*****@*****.**";

            //List<UpdateAction> actions = new List<UpdateAction>() { setCountryAction, setCustomerEmailAction };
            List <UpdateAction> actions = new List <UpdateAction>()
            {
                setCustomerEmailAction
            };

            cartResponse = await client.Carts().UpdateCartAsync(cart, actions);

            if (cartResponse.Success)
            {
                cart = cartResponse.Result;
                Console.WriteLine("Updated cart with ID {0}.", cart.Id);
                Console.WriteLine("Cart country: {0}", cart.Country);
                Console.WriteLine("Cart customer email: {0}", cart.CustomerEmail);
            }
            else
            {
                Helper.WriteError <Cart>(cartResponse);
            }

            /*  ADDING, CHANGING AND REMOVING LINE ITEMS
             *  ===================================================================================
             *  Making line item requests is done using update actions.
             */

            // Find a product to add to the cart as a line item.
            Response <ProductQueryResult> productQueryResponse = await client.Products().QueryProductsAsync();

            Product product = null;

            if (productQueryResponse.Success)
            {
                ProductQueryResult productQueryResult = productQueryResponse.Result;
                product = productQueryResult.Results[0];
                Console.WriteLine("Retrieved product with ID {0}.", product.Id);
            }
            else
            {
                Helper.WriteError <ProductQueryResult>(productQueryResponse);
            }

            // Add the line item to a cart.
            AddLineItemAction addLineItemAction = new AddLineItemAction(product.Id, product.MasterData.Current.MasterVariant.Id);

            addLineItemAction.Quantity = 1;

            Console.WriteLine("addLineItemAction.Action: {0}", addLineItemAction.Action);
            Console.WriteLine("addLineItemAction.ProductId: {0}", addLineItemAction.ProductId);
            Console.WriteLine("addLineItemAction.VariantId: {0}", addLineItemAction.VariantId);
            Console.WriteLine("addLineItemAction.Quantity: {0}", addLineItemAction.Quantity);

            Console.WriteLine("cart.Id: {0}", cart.Id);
            Console.WriteLine("cart.Version: {0}", cart.Version);

            cartResponse = await client.Carts().UpdateCartAsync(cart, addLineItemAction);

            if (cartResponse.Success)
            {
                cart = cartResponse.Result;
                Console.WriteLine("Added line item to cart with ID {0}.", cart.Id);
                Console.WriteLine("Line item product ID: {0}.", cart.LineItems[0].ProductId);
                Console.WriteLine("Line item quantity: {0}.", cart.LineItems[0].Quantity);
            }
            else
            {
                Helper.WriteError <Cart>(cartResponse);
            }

            // Use another update action to change the quantity of the line item in the cart to 2.
            if (cart.LineItems.Count > 0)
            {
                ChangeLineItemQuantityAction changeLineItemQuantityAction = new ChangeLineItemQuantityAction(cart.LineItems[0].Id, 2);
                cartResponse = await client.Carts().UpdateCartAsync(cart, changeLineItemQuantityAction);

                if (cartResponse.Success)
                {
                    cart = cartResponse.Result;
                    Console.WriteLine("Changed line item quanity in cart with ID {0}.", cart.Id);
                    Console.WriteLine("Line item product ID: {0}.", cart.LineItems[0].ProductId);
                    Console.WriteLine("Line item quantity: {0}.", cart.LineItems[0].Quantity);
                }
                else
                {
                    Helper.WriteError <Cart>(cartResponse);
                }
            }

            // Finally, remove the line item from the cart.
            if (cart.LineItems.Count > 0)
            {
                RemoveLineItemAction removeLineItemAction = new RemoveLineItemAction(cart.LineItems[0].Id);
                cartResponse = await client.Carts().UpdateCartAsync(cart, removeLineItemAction);

                if (cartResponse.Success)
                {
                    cart = cartResponse.Result;
                    Console.WriteLine("Removed line item from cart with ID {0}.", cart.Id);
                    Console.WriteLine("Line items in cart: {0}.", cart.LineItems.Count);
                }
                else
                {
                    Helper.WriteError <Cart>(cartResponse);
                }
            }

            /*  DELETE A CART
             *  ===================================================================================
             *  Delete API requests return a generic response, but some return the object that was
             *  deleted. The Carts delete request returns the full representation.
             */
            cartResponse = await client.Carts().DeleteCartAsync(cart);

            if (cartResponse.Success)
            {
                cart = cartResponse.Result;
                Console.WriteLine("Deleted cart with ID {0}.", cart.Id);
            }
            else
            {
                Helper.WriteError <Cart>(cartResponse);
            }
        }
Пример #3
0
        /// <summary>
        /// Run Product example code.
        /// </summary>
        /// <param name="client">Client</param>
        /// <param name="project">Project</param>
        public async static Task Run(Client client, Project.Project project)
        {
            string language = project.Languages[0];

            /*  GET PRODUCTS
             *  ===================================================================================
             */
            Response <ProductQueryResult> productQueryResponse = await client.Products().QueryProductsAsync();

            List <Product> products = new List <Product>();

            if (productQueryResponse.Success)
            {
                ProductQueryResult productQueryResult = productQueryResponse.Result;
                products = productQueryResult.Results;

                Console.WriteLine("Retrieved {0} products.", products.Count);
            }
            else
            {
                Helper.WriteError <ProductQueryResult>(productQueryResponse);
            }

            // Get a single product by its ID.
            string             productId       = products[0].Id;
            Response <Product> productResponse = await client.Products().GetProductByIdAsync(productId);

            Product product = null;

            if (productResponse.Success)
            {
                product = productResponse.Result;
                Console.WriteLine("Retrieved product with ID {0}.", product.Id);
            }
            else
            {
                Helper.WriteError <Product>(productResponse);
            }

            /*  CREATE PRODUCT
             *  ===================================================================================
             *  You will need to specify the type of product you are creating using a
             *  resource identifier that references an existing product type in your commercetools
             *  project. So before creating the product, we will retrieve a ProductType from the
             *  API and use it to create a ResourceIdentifier object.
             */
            Response <ProductTypeQueryResult> productTypeResponse = await client.ProductTypes().QueryProductTypesAsync();

            ProductType productType = null;

            if (productTypeResponse.Success)
            {
                ProductTypeQueryResult productTypeQueryResult = productTypeResponse.Result;
                productType = productTypeQueryResult.Results[0];
                Console.WriteLine("Retrieved product type with ID {0}.", productType.Id);
            }
            else
            {
                Helper.WriteError <ProductTypeQueryResult>(productTypeResponse);
                return;
            }

            ResourceIdentifier resourceIdentifier = new ResourceIdentifier();

            resourceIdentifier.TypeId = Common.ReferenceType.ProductType;
            resourceIdentifier.Id     = productType.Id;

            LocalizedString productName = new LocalizedString();

            productName[language] = "My New Product";

            LocalizedString productSlug = new LocalizedString();

            productSlug[language] = "mynewproduct";

            ProductDraft productDraft = new ProductDraft(productName, resourceIdentifier, productSlug);

            productResponse = await client.Products().CreateProductAsync(productDraft);

            if (productResponse.Success)
            {
                product = productResponse.Result;
                Console.WriteLine("Created new product with ID {0}.", product.Id);
                Console.WriteLine("Product name: {0}", product.MasterData.Staged.Name[language]);
                Console.WriteLine("Product slug: {0}", product.MasterData.Staged.Slug[language]);
            }
            else
            {
                Helper.WriteError <Product>(productResponse);
            }

            /*  UPDATE A PRODUCT
             *  ===================================================================================
             *  Each change is made using its own update action object which maps to an update
             *  action call in the API. The list of update action objects are sent to the API using
             *  a single request. If there is an update action in the API that has not yet been
             *  implemented in the SDK, you can use the GenericAction class to make any request you
             *  want (as long as it is a valid update action supported by the API).
             */
            SetKeyAction setKeyAction = new SetKeyAction();

            setKeyAction.Key = "ABC123";

            // Here is how you would make the changeName request using a GenericAction object.
            productName[language] = "My New Name";

            GenericAction changeNameAction = new GenericAction("changeName");

            changeNameAction["name"]   = productName;
            changeNameAction["staged"] = true;

            List <UpdateAction> actions = new List <UpdateAction>()
            {
                setKeyAction, changeNameAction
            };

            productResponse = await client.Products().UpdateProductAsync(product, actions);

            if (productResponse.Success)
            {
                product = productResponse.Result;
                Console.WriteLine("Updated product with ID {0}.", product.Id);
                Console.WriteLine("Product key: {0}", product.Key);
                Console.WriteLine("Updated product name: {0}", product.MasterData.Staged.Name[language]);
            }
            else
            {
                Helper.WriteError <Product>(productResponse);
            }

            /*  DELETE A PRODUCT
             *  ===================================================================================
             *  Delete API requests return a generic response, but some return the object that was
             *  deleted. The Products delete request returns the full representation.
             */
            productResponse = await client.Products().DeleteProductAsync(product);

            if (productResponse.Success)
            {
                product = productResponse.Result;
                Console.WriteLine("Deleted product with ID {0}.", product.Id);
            }
            else
            {
                Helper.WriteError <Product>(productResponse);
            }
        }