コード例 #1
0
        public static CategoryDraft DefaultCategoryDraftWithParent(CategoryDraft draft, Category parent)
        {
            var categoryDraft = DefaultCategoryDraft(draft);

            categoryDraft.Parent = parent.ToReference();
            return(categoryDraft);
        }
コード例 #2
0
        public static CategoryDraft DefaultCategoryDraftWithKey(CategoryDraft draft, string key)
        {
            var categoryDraft = DefaultCategoryDraft(draft);

            categoryDraft.Key = key;
            return(categoryDraft);
        }
コード例 #3
0
        /// <summary>
        /// Gets a test category draft.
        /// </summary>
        /// <param name="project">Project</param>
        /// <returns>CategoryDraft</returns>
        public static CategoryDraft GetTestCategoryDraft(Project.Project project)
        {
            LocalizedString name            = new LocalizedString();
            LocalizedString slug            = new LocalizedString();
            LocalizedString description     = new LocalizedString();
            LocalizedString metaTitle       = new LocalizedString();
            LocalizedString metaDescription = new LocalizedString();
            LocalizedString metaKeywords    = new LocalizedString();

            foreach (string language in project.Languages)
            {
                string randomPostfix = Helper.GetRandomString(10);
                name.SetValue(language, string.Concat("Test Category ", language, " ", randomPostfix));
                slug.SetValue(language, string.Concat("test-category-", language, "-", randomPostfix));
                description.SetValue(language, string.Concat("Created by commercetools.NET ", language));
                metaTitle.SetValue(language, string.Concat("Category Meta Title ", language));
                metaDescription.SetValue(language, string.Concat("Category Meta Description ", language));
                metaKeywords.SetValue(language, string.Concat("Category Meta Keywords ", language));
            }

            CategoryDraft categoryDraft = new CategoryDraft(name, slug);

            categoryDraft.Description     = description;
            categoryDraft.MetaTitle       = metaTitle;
            categoryDraft.MetaDescription = metaDescription;
            categoryDraft.MetaKeywords    = metaKeywords;

            return(categoryDraft);
        }
コード例 #4
0
        /// <summary>
        /// Test setup
        /// </summary>
        public CategoryManagerTest()
        {
            _client = new Client(Helper.GetConfiguration());

            Task <Response <Project.Project> > projectTask = _client.Project().GetProjectAsync();

            projectTask.Wait();
            Assert.True(projectTask.Result.Success);
            _project = projectTask.Result.Result;

            _testCategories = new List <Category>();

            for (int i = 0; i < 5; i++)
            {
                CategoryDraft categoryDraft = Helper.GetTestCategoryDraft(_project);
                Task <Response <Category> > categoryTask = _client.Categories().CreateCategoryAsync(categoryDraft);
                categoryTask.Wait();
                Assert.True(categoryTask.Result.Success);

                Category category = categoryTask.Result.Result;
                Assert.NotNull(category.Id);

                _testCategories.Add(category);
            }
        }
コード例 #5
0
        public Category CreateCategory(CategoryDraft categoryDraft)
        {
            IClient  commerceToolsClient = this.GetService <IClient>();
            Category category            = commerceToolsClient.ExecuteAsync(new CreateCommand <Category>(categoryDraft)).Result;

            return(category);
        }
コード例 #6
0
        public void CreateCategoryWithCustomFields()
        {
            IClient       commerceToolsClient = this.categoryFixture.GetService <IClient>();
            CategoryDraft categoryDraft       = this.categoryFixture.GetCategoryDraftWithCustomFields();
            Category      category            = this.categoryFixture.CreateCategory(categoryDraft);

            this.categoryFixture.CategoriesToDelete.Add(category);
            Assert.Equal(categoryDraft.Custom.Fields.Count, category.Custom.Fields.Count);
        }
コード例 #7
0
        public void CreateCategory()
        {
            IClient       commerceToolsClient = this.categoryFixture.GetService <IClient>();
            CategoryDraft categoryDraft       = this.categoryFixture.GetCategoryDraft();
            Category      category            = commerceToolsClient.ExecuteAsync(new CreateCommand <Category>(categoryDraft)).Result;

            this.categoryFixture.CategoriesToDelete.Add(category);
            Assert.Equal(categoryDraft.Key, category.Key.ToString());
        }
コード例 #8
0
        public static CategoryDraft DefaultCategoryDraftWithAsset(CategoryDraft draft)
        {
            var categoryDraft = DefaultCategoryDraft(draft);
            var assetDraft    = TestingUtility.GetAssetDraft();

            categoryDraft.Assets = new List <AssetDraft> {
                assetDraft
            };
            return(categoryDraft);
        }
コード例 #9
0
        public CategoryDraft GetCategoryDraftWithParent(Category parentCategory)
        {
            CategoryDraft categoryDraft = this.GetCategoryDraft();

            categoryDraft.Parent = new Reference <Category>()
            {
                Id = parentCategory.Id
            };
            return(categoryDraft);
        }
コード例 #10
0
        public static CategoryDraft DefaultCategoryDraftWithMultipleAssets(CategoryDraft draft, int assetsCount = 3)
        {
            var categoryDraft = DefaultCategoryDraft(draft);

            categoryDraft.Assets = new List <AssetDraft>();
            for (int i = 1; i <= assetsCount; i++)
            {
                var assetDraft = TestingUtility.GetAssetDraft();
                categoryDraft.Assets.Add(assetDraft);
            }
            return(categoryDraft);
        }
コード例 #11
0
        public static CategoryDraft DefaultCategoryDraft(CategoryDraft categoryDraft)
        {
            var randomInt = TestingUtility.RandomInt();

            categoryDraft.Name = new LocalizedString {
                { "en", $"Name{randomInt}" }
            };
            categoryDraft.Slug = new LocalizedString {
                { "en", $"Slug{randomInt}" }
            };
            categoryDraft.Key       = $"Key{randomInt}";
            categoryDraft.OrderHint = TestingUtility.RandomSortOrder();
            return(categoryDraft);
        }
コード例 #12
0
        public static CategoryDraft DefaultCategoryDraftWithCustomType(CategoryDraft draft, Type type, Fields fields)
        {
            var customFieldsDraft = new CustomFieldsDraft
            {
                Type   = type.ToKeyResourceIdentifier(),
                Fields = fields
            };

            var customerDraft = DefaultCategoryDraft(draft);

            customerDraft.Custom = customFieldsDraft;

            return(customerDraft);
        }
コード例 #13
0
        public CategoryDraft GetCategoryDraft(LocalizedString name, LocalizedString slug, string key = null, string orderHint = null, string externalId = null, LocalizedString description = null, IReference <Category> parent = null)
        {
            var categoryDraft = new CategoryDraft
            {
                Name        = name,
                Slug        = slug,
                Key         = key,
                OrderHint   = orderHint,
                Description = description,
                Parent      = parent,
                ExternalId  = externalId
            };

            return(categoryDraft);
        }
コード例 #14
0
        public static CategoryDraft DefaultCategoryDraftWithAssetWithCustomType(CategoryDraft draft, Type type, Fields fields)
        {
            var customFieldsDraft = new CustomFieldsDraft
            {
                Type   = type.ToKeyResourceIdentifier(),
                Fields = fields
            };
            var categoryDraft = DefaultCategoryDraft(draft);
            var assetDraft    = TestingUtility.GetAssetDraft();

            assetDraft.Custom    = customFieldsDraft;
            categoryDraft.Assets = new List <AssetDraft> {
                assetDraft
            };
            return(categoryDraft);
        }
コード例 #15
0
        public CategoryDraft GetCategoryDraft()
        {
            CategoryDraft   categoryDraft       = new CategoryDraft();
            string          categoryName        = this.RandomString(10);
            LocalizedString localizedStringName = new LocalizedString();

            localizedStringName.Add("en", categoryName);
            categoryDraft.Name = localizedStringName;
            string          slug = this.RandomString(10);
            LocalizedString localizedStringSlug = new LocalizedString();

            localizedStringSlug.Add("en", slug);
            categoryDraft.Slug = localizedStringSlug;
            categoryDraft.Key  = this.RandomString(10);
            return(categoryDraft);
        }
コード例 #16
0
        public CategoryDraft GetCategoryDraft()
        {
            CategoryDraft   categoryDraft       = new CategoryDraft();
            string          categoryName        = TestingUtility.RandomString(10);
            LocalizedString localizedStringName = new LocalizedString();

            localizedStringName.Add("en", categoryName);
            categoryDraft.Name = localizedStringName;
            string          slug = TestingUtility.RandomString(10);
            LocalizedString localizedStringSlug = new LocalizedString();

            localizedStringSlug.Add("en", slug);
            categoryDraft.Slug      = localizedStringSlug;
            categoryDraft.Key       = TestingUtility.RandomString(10);
            categoryDraft.OrderHint = TestingUtility.RandomSortOrder();
            return(categoryDraft);
        }
コード例 #17
0
        public void IsCreateCommand()
        {
            var builder       = new CommandBuilder();
            var categoryDraft = new CategoryDraft
            {
                Name = new LocalizedString {
                    { "en", "CatName" }
                },
                Slug = new LocalizedString {
                    { "en", "CatSlug" }
                }
            };
            var command = builder.Categories().Create(categoryDraft).Build();

            Assert.IsType <CreateCommand <Category> >(command);
            Assert.NotNull(command.Entity);
        }
コード例 #18
0
        public CategoryDraft GetCategoryDraftWithCustomFields()
        {
            Category relatedCategory = this.CreateCategory(this.GetCategoryDraft());

            this.CategoriesToDelete.Add(relatedCategory);
            CategoryDraft     categoryDraft     = this.GetCategoryDraft();
            CustomFieldsDraft customFieldsDraft = new CustomFieldsDraft();
            Type type = this.typeFixture.CreateType();

            this.typeFixture.TypesToDelete.Add(type);
            customFieldsDraft.Type = new ResourceIdentifier <Type> {
                Key = type.Key
            };
            customFieldsDraft.Fields = new Fields();
            customFieldsDraft.Fields.Add("string-field", "test");
            customFieldsDraft.Fields.Add("localized-string-field",
                                         new LocalizedString()
            {
                { "en", "localized-string-field-value" }
            });
            customFieldsDraft.Fields.Add("enum-field", "enum-key-1");
            customFieldsDraft.Fields.Add("localized-enum-field", "enum-key-1");
            customFieldsDraft.Fields.Add("number-field", 3);
            customFieldsDraft.Fields.Add("boolean-field", true);
            customFieldsDraft.Fields.Add("date-field", new DateTime(2018, 11, 28));
            customFieldsDraft.Fields.Add("date-time-field", new DateTime(2018, 11, 28, 11, 01, 00));
            customFieldsDraft.Fields.Add("time-field", new TimeSpan(11, 01, 00));
            customFieldsDraft.Fields.Add("money-field", new Money()
            {
                CentAmount = 1800, CurrencyCode = "EUR"
            });
            customFieldsDraft.Fields.Add("set-field", new FieldSet <string>()
            {
                "test1", "test2"
            });
            customFieldsDraft.Fields.Add("reference-field", new Reference <Category>()
            {
                Id = relatedCategory.Id
            });
            categoryDraft.Custom = customFieldsDraft;
            return(categoryDraft);
        }
コード例 #19
0
        public async Task ShouldCreateAndDeleteCategoryAsync()
        {
            CategoryDraft       categoryDraft = Helper.GetTestCategoryDraft(_project);
            Response <Category> response      = await _client.Categories().CreateCategoryAsync(categoryDraft);

            Assert.True(response.Success);

            Category category = response.Result;

            Assert.NotNull(category.Id);

            string deletedCategoryId = category.Id;

            response = await _client.Categories().DeleteCategoryAsync(category);

            Assert.True(response.Success);

            response = await _client.Categories().GetCategoryByIdAsync(deletedCategoryId);

            Assert.False(response.Success);
        }
コード例 #20
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);
            }
        }