Esempio n. 1
0
        public void InsertCategoryMapping(ErplyCategoryMapping categoryMapping)
        {
            if (categoryMapping == null)
            {
                throw new ArgumentNullException(nameof(categoryMapping));
            }

            _categoryMappingRepository.Insert(categoryMapping);
        }
        private void SaveCategoriesRecursive(
            List <ErplyProductGroupsResponseRecord> erplyProductGroups,
            List <ErplyCategoryMapping> categoryMappings,
            IList <Category> allCategories,
            int parentCategoryId = 0)
        {
            foreach (var erplyProductGroup in erplyProductGroups)
            {
                var categoryMapping = categoryMappings.FirstOrDefault(mapping => mapping.ErplyProductGroupID == erplyProductGroup.ProductGroupID);

                Category category      = allCategories.FirstOrDefault(c => c.Id == categoryMapping?.CategoryID);
                bool     isNewCategory = category == null;
                category ??= new Category();
                category = GetUpdatedCategoryFromErplyProductGroup(category, erplyProductGroup, parentCategoryId, isNewCategory);

                if (isNewCategory)
                {
                    _categoryService.InsertCategory(category);
                    ErplyCategoryMapping newMapping = new ErplyCategoryMapping()
                    {
                        CategoryID          = category.Id,
                        ErplyProductGroupID = erplyProductGroup.ProductGroupID
                    };
                    _erplyMappingService.InsertCategoryMapping(newMapping);
                }
                else
                {
                    _categoryService.UpdateCategory(category);
                }

                _urlRecordService.SaveSlug(category, _urlRecordService.ValidateSeName(category, "", category.Name, true), 0);

                if (erplyProductGroup.SubGroups.Count > 0)
                {
                    SaveCategoriesRecursive(erplyProductGroup.SubGroups, categoryMappings, allCategories, category.Id);
                }
            }
        }