public async Task ShouldUpdateCustomerAsync() { string newKey = Helper.GetRandomString(15); string newEmail = string.Concat(Helper.GetRandomString(10), "@example.com"); string newExternalId = Helper.GetRandomNumber(10000, 99999).ToString(); SetKeyAction setKeyAction = new SetKeyAction(); setKeyAction.Key = newKey; SetExternalIdAction setExternalIdAction = new SetExternalIdAction(); setExternalIdAction.ExternalId = newExternalId; GenericAction changeEmailAction = new GenericAction("changeEmail"); changeEmailAction.SetProperty("email", newEmail); List <UpdateAction> actions = new List <UpdateAction>(); actions.Add(setKeyAction); actions.Add(setExternalIdAction); actions.Add(changeEmailAction); Response <Customer> response = await _client.Customers().UpdateCustomerAsync(_testCustomer, actions); Assert.True(response.Success); _testCustomer = response.Result; Assert.NotNull(_testCustomer.Id); Assert.Equal(_testCustomer.Key, newKey); Assert.Equal(_testCustomer.Email, newEmail); Assert.Equal(_testCustomer.ExternalId, newExternalId); }
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); }
public async Task ShouldUpdateProductAsync() { string newKey = Helper.GetRandomString(15); LocalizedString newSlug = new LocalizedString(); foreach (string language in _project.Languages) { newSlug.SetValue(language, string.Concat("updated-product-", language, "-", Helper.GetRandomString(10))); } SetKeyAction setKeyAction = new SetKeyAction(); setKeyAction.Key = newKey; GenericAction changeSlugAction = new GenericAction("changeSlug"); changeSlugAction.SetProperty("slug", newSlug); changeSlugAction.SetProperty("staged", true); List <UpdateAction> actions = new List <UpdateAction>(); actions.Add(setKeyAction); actions.Add(changeSlugAction); Response <Product> response = await _client.Products().UpdateProductAsync(_testProducts[2], actions); Assert.IsTrue(response.Success); _testProducts[2] = response.Result; Assert.NotNull(_testProducts[2].Id); Assert.AreEqual(_testProducts[2].Key, newKey); foreach (string language in _project.Languages) { Assert.AreEqual(_testProducts[2].MasterData.Staged.Slug.GetValue(language), newSlug.GetValue(language)); } }
/// <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); } }