public async Task ShouldChangeNameSetDescriptionCartDiscountAsync()
        {
            // Arrange
            var cartDiscount = await Helper.CreateTestCartDiscount(this._project, this._client);

            LocalizedString name        = new LocalizedString();
            LocalizedString description = new LocalizedString();

            foreach (string language in this._project.Languages)
            {
                string randomPostfix = Helper.GetRandomString(10);
                name.SetValue(language, string.Concat("change-cart-discount-name", language, " ", randomPostfix));
                description.SetValue(language, string.Concat("change-cart-discount-description", language, "-", randomPostfix));
            }
            var changeNameAction     = new ChangeNameAction(name);
            var setDescriptionAction = new SetDescriptionAction(description);

            // Act
            var updatedCartDiscountResponse = await this._client.CartDiscounts()
                                              .UpdateCartDiscountAsync(cartDiscount, new List <UpdateAction> {
                changeNameAction, setDescriptionAction
            });


            // Assert
            var updatedCartDiscount = updatedCartDiscountResponse.Result;

            Assert.IsNotNull(updatedCartDiscount);
            Assert.IsNotNull(updatedCartDiscount.Id);
            Assert.AreEqual(updatedCartDiscount.Name.Values, name.Values);
            Assert.AreEqual(updatedCartDiscount.Description.Values, description.Values);

            // Cleanup
            await _client.CartDiscounts().DeleteCartDiscountAsync(updatedCartDiscount);
        }
        public async Task ShouldUpdateCategoryAsync()
        {
            LocalizedString newName = new LocalizedString();
            LocalizedString newSlug = new LocalizedString();

            foreach (string language in _project.Languages)
            {
                newName.SetValue(language, string.Concat("New Name ", language));
                newSlug.SetValue(language, string.Concat("slug-updated-", language));
            }

            ChangeNameAction changeNameAction = new ChangeNameAction(newName);

            GenericAction changeSlugAction = new GenericAction("changeSlug");

            changeSlugAction.SetProperty("slug", newSlug);

            List <UpdateAction> actions = new List <UpdateAction>();

            actions.Add(changeNameAction);
            actions.Add(changeSlugAction);

            Response <Category> response = await _client.Categories().UpdateCategoryAsync(_testCategories[2], actions);

            Assert.True(response.Success);

            _testCategories[2] = response.Result;
            Assert.NotNull(_testCategories[2].Id);

            foreach (string language in _project.Languages)
            {
                Assert.Equal(_testCategories[2].Name[language], newName[language]);
                Assert.Equal(_testCategories[2].Slug[language], newSlug[language]);
            }
        }
        public async Task ShouldUpdateCustomerGroupAsync()
        {
            string newName = string.Concat(_testCustomerGroups[0].Name, " Updated");
            string newKey  = Helper.GetRandomString(10);

            var changeNameAction = new ChangeNameAction(newName);
            var setKeyAction     = new SetKeyAction(newKey);

            List <UpdateAction> actions = new List <UpdateAction>();

            actions.Add(changeNameAction);
            actions.Add(setKeyAction);

            var response = await _client.CustomerGroups().UpdateCustomerGroupAsync(_testCustomerGroups[0], actions);

            Assert.IsTrue(response.Success);

            _testCustomerGroups[0] = response.Result;
            Assert.NotNull(_testCustomerGroups[0].Id);
            Assert.AreEqual(_testCustomerGroups[0].Name, newName);
            Assert.AreEqual(_testCustomerGroups[0].Key, newKey);
        }
Exemplo n.º 4
0
        public async Task ShouldUpdateProductByKeyAsync()
        {
            LocalizedString newName = new LocalizedString();
            LocalizedString newSlug = new LocalizedString();

            foreach (string language in _project.Languages)
            {
                newName.SetValue(language, string.Concat("Updated Product ", language, " ", Helper.GetRandomString(10)));
                newSlug.SetValue(language, string.Concat("updated-product-", language, "-", Helper.GetRandomString(10)));
            }

            ChangeNameAction changeNameAction = new ChangeNameAction(newName);

            GenericAction changeSlugAction = new GenericAction("changeSlug");

            changeSlugAction.SetProperty("slug", newSlug);
            changeSlugAction.SetProperty("staged", true);

            List <UpdateAction> actions = new List <UpdateAction>();

            actions.Add(changeNameAction);
            actions.Add(changeSlugAction);

            Response <Product> response = await _client.Products().UpdateProductByKeyAsync(_testProducts[1].Key, _testProducts[1].Version, actions);

            Assert.IsTrue(response.Success);

            _testProducts[1] = response.Result;
            Assert.NotNull(_testProducts[1].Id);

            foreach (string language in _project.Languages)
            {
                Assert.AreEqual(_testProducts[1].MasterData.Staged.Name[language], newName[language]);
                Assert.AreEqual(_testProducts[1].MasterData.Staged.Slug[language], newSlug[language]);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Run Category 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 CATEGORIES
             *  ===================================================================================
             */
            Response <CategoryQueryResult> categoryQueryResponse = await client.Categories().QueryCategoriesAsync();

            List <Category> categories = new List <Category>();

            if (categoryQueryResponse.Success)
            {
                CategoryQueryResult categoryQueryResult = categoryQueryResponse.Result;
                categories = categoryQueryResult.Results;

                Console.WriteLine("Retrieved {0} categories.", categories.Count);
            }
            else
            {
                Helper.WriteError <CategoryQueryResult>(categoryQueryResponse);
            }

            // Get a single category by its ID.
            string categoryId = categories[0].Id;
            Response <Category> categoryResponse = await client.Categories().GetCategoryByIdAsync(categoryId);

            Category category = null;

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

            /*  CREATE CATEGORY
             *  ===================================================================================
             */
            LocalizedString categoryName = new LocalizedString();

            categoryName[language] = "My New Category";

            LocalizedString categorySlug = new LocalizedString();

            categorySlug[language] = "mynewcategory";

            CategoryDraft categoryDraft = new CategoryDraft(categoryName, categorySlug);

            categoryResponse = await client.Categories().CreateCategoryAsync(categoryDraft);

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

            /*  UPDATE A CATEGORY
             *  ===================================================================================
             *  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).
             */
            categoryName[language] = "Updated Category";
            ChangeNameAction changeNameAction = new ChangeNameAction(categoryName);

            // Here is how you would make the setDescription request using a GenericAction object.
            LocalizedString categoryDescription = new LocalizedString();

            categoryDescription[language] = "This is a new category created by the commercetools.NET SDK.";

            GenericAction setDescriptionAction = new GenericAction("setDescription");

            setDescriptionAction["description"] = categoryDescription;

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

            categoryResponse = await client.Categories().UpdateCategoryAsync(category, actions);

            if (categoryResponse.Success)
            {
                category = categoryResponse.Result;
                Console.WriteLine("Updated category with ID {0}.", category.Id);
                Console.WriteLine("Updated category name: {0}", category.Name[language]);
                Console.WriteLine("Category description: {0}", category.Description[language]);
            }
            else
            {
                Helper.WriteError <Category>(categoryResponse);
            }

            /*  DELETE A CATEGORY
             *  ===================================================================================
             *  Delete API requests return a generic response, but some return the object that was
             *  deleted. The Categories delete request returns the full representation.
             */
            categoryResponse = await client.Categories().DeleteCategoryAsync(category);

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