private int insertCategory(N11DataService dataService, CategoryModel category) { Debug.WriteLine("Inserting " + category.ToString()); dataService.InsertCategory(category, N11_STORE_ID); var count = 1; foreach (var subCategory in category.Children) { count += insertCategory(dataService, subCategory); if (bgwReloadCategories.IsBusy) bgwReloadCategories.ReportProgress(count); } return count; }
private List<CategoryModel> getSubCategories(CategoryServicePortClient service, CategoryModel cat) { Debug.WriteLine("{0}\t{1}\t{2}\t{3}", categoryCount, cat.ID, cat.Parent == null ? 0 : cat.Parent.ID, cat.Name); categoryCount++; var response = service.GetSubCategories(new GetSubCategoriesRequest { categoryId = cat.ID }); if (response.result.status != "success") { Debug.WriteLine("Kategori bulunamadı: {0} - {1}", cat.ID, cat.ToString()); return new List<CategoryModel>(); } if (response.category == null || response.category.Length == 0) return new List<CategoryModel>(); var list = response.category[0].subCategoryList; if (list == null) { // En alt seviye //var attResponse = service.GetCategoryAttributes(new GetCategoryAttributesRequest // { // categoryId = cat.ID // }); //cat.attributes = attResponse.category.attributeList // .Select(x => new AttributeModel // { // id = x.id, // name = x.name, // values = x.valueList.Select(y => new KeyValuePair<long, string>(y.id, y.name)).ToList() // }) // .ToList(); var obj = callGet("https://api.n11.com/rest/secure/category/getCategoryAttributes.json", new[] {"id"}, new[] {cat.ID.ToString()}); var attArr = obj["response"]["data"]["category"]["attributeList"] as JArray; if (attArr == null) cat.Attributes = new List<AttributeModel>(); else cat.Attributes = attArr.Select(x => new AttributeModel { id = (long)x["id"], name = (string)x["name"], inputMethod = (string)x["inputMethod"], mandatory = (bool)x["mandatory"], multipleSelect = (bool)x["multipleSelect"], code = (string)x["code"], //values = x.valueList.Select(y => new KeyValuePair<long, string>(y.id, y.name)).ToList() values = ((JArray)x["valueList"]).Select(y => new KeyValuePair<long, string>((long)y["id"], (string)y["name"])).ToList() }) .ToList(); return new List<CategoryModel>(); } var subCatList = list.Select(y => new CategoryModel { ID = y.id, Name = y.name, Parent = cat, Code = y.id.ToString() }).ToList(); subCatList.ForEach(y => y.Children = getSubCategories(service, y)); return subCatList; }
private List<CategoryModel> getCategoryListFromTextFile() { var catList = new List<CategoryModel>(); var sr = new System.IO.StreamReader(Path.Combine(Application.StartupPath, "catList.txt")); var line = ""; var catTable = new Dictionary<long, CategoryModel>(); while (!String.IsNullOrEmpty(line = sr.ReadLine())) { if (line.StartsWith("Kategori bulunamad")) continue; var s = line.Split('\t'); var cat = new CategoryModel { ID = long.Parse(s[1]), Name = s[3], Parent = s[2] == "0" ? null : catTable[long.Parse(s[2])], Children = new List<CategoryModel>() }; catTable.Add(cat.ID, cat); if (cat.Parent != null) cat.Parent.Children.Add(cat); else catList.Add(cat); } sr.Close(); return catList; }
private TreeNode categoryToTreeNode(CategoryModel cat, bool getSubCategories) { var node = new TreeNode(String.Format("{0} - {1}", cat.ID, cat.Name)); node.Tag = cat; categoryTable.Add(cat.ID, cat); if (getSubCategories) { foreach (var subCat in cat.Children) { var subNode = categoryToTreeNode(subCat, true); node.Nodes.Add(subNode); } } else { if (cat.Attributes == null || cat.Attributes.Count == 0) node.Nodes.Add(new TreeNode("dummy")); } return node; }
public List<CategoryModel> GetAllCategories(int storeId) { var context = new StorManEntities(); var catList = context.Categories.Where(x => x.StoreID == storeId) .OrderBy(x => x.ParentID).ThenBy(x => x.ID) .ToList(); var attList = context.Attributes.Select(x => new { x.ID, x.IsMandatory, x.IsMultipleSelect, x.Name, CategoryID = x.Categories.Select(y => y.ID).FirstOrDefault() }) .OrderBy(x => x.CategoryID).ThenBy(x => x.ID) //.ToDictionary(x => x.CategoryID, x => x); .ToList(); var attValueList = context.AttributeValues.OrderBy(x => x.AttributeID).ThenBy(x => x.ID) .ToList(); var catTable = catList.ToDictionary(x => x.ID, x => x); var modelTable = new Dictionary<long, CategoryModel>(); foreach (var category in catList) { var model = new CategoryModel { ID = category.ID, Name = category.Name, Code = category.Code, Attributes = new List<AttributeModel>(), Children = new List<CategoryModel>() }; modelTable.Add(model.ID, model); if (category.ParentID != null) { var parent = modelTable[category.ParentID.Value]; if (parent == null) { model.GetType(); // What the!!! } else { model.Parent = parent; parent.Children.Add(model); } } } var leafNodes = modelTable.Values.Where(x => !x.Children.Any()).ToList(); foreach (var subCategory in leafNodes) { var thisAttList = attList.Where(x => x.CategoryID == subCategory.ID); foreach (var att in thisAttList) { var attModel = new AttributeModel { id = att.ID, name = att.Name, code = att.ID.ToString(), multipleSelect = att.IsMultipleSelect, mandatory = att.IsMandatory, values = new List<KeyValuePair<long, string>>() }; attModel.values = attValueList.Where(x => x.AttributeID == attModel.id) .Select(x => new KeyValuePair<long, string>(x.ID, x.Name)) .ToList(); subCategory.Attributes.Add(attModel); } } var retList = modelTable.Values.Where(x => x.Parent == null).ToList(); return retList; }
public void InsertCategory(CategoryModel catModel, int storeId) { var context = new StorManEntities(); var cat = new Category { Code = catModel.Code, Name = catModel.Name, CrDate = DateTime.Now, StoreID = storeId, }; context.Categories.Add(cat); if (catModel.Parent != null) { var parentCat = context.Categories.FirstOrDefault(x => x.StoreID == storeId && x.Code == catModel.Parent.Code); if (parentCat != null) { cat.ParentID = parentCat.ID; } else { cat.GetType(); } } context.SaveChanges(); if (catModel.Attributes != null) { foreach (var attModel in catModel.Attributes) { var att = context.Attributes.FirstOrDefault(x => x.ID == attModel.id); if (att == null) { att = new Attribute { ID = (int) attModel.id, Name = attModel.name, IsMandatory = attModel.mandatory, IsMultipleSelect = attModel.multipleSelect, //inputMethod = attModel.inputMethod }; context.Attributes.Add(att); context.SaveChanges(); var i = 0; foreach (var keyValuePair in attModel.values) { var attValue = new AttributeValue { Attribute = att, Name = keyValuePair.Value }; context.AttributeValues.Add(attValue); i++; if (i%50 == 0) { var rowCount = context.SaveChanges(); System.Diagnostics.Debug.WriteLine("Saved {0} rows, i={1}", rowCount, i); } } context.SaveChanges(); } cat.Attributes.Add(att); context.SaveChanges(); } } }
public void InsertCategory(CategoryModel catModel, int storeId) { new N11Repository().InsertCategory(catModel, storeId); }