public async Task <ActionResult <IEnumerable <CategoryToReturnDto> > > GetCategorys() { var categorys = await _context.Categorys.Where(x => x.Name != "Root").ToListAsync(); var toReturn = new List <CategoryToReturnDto>(); foreach (var item in categorys) { var alt = new CategoryToReturnDto(); alt.Id = item.Id; alt.Name = item.Name; alt.hasSubCategory = item.hasSubCategory; alt.parentId = item.parentId; toReturn.Add(alt); } return(toReturn); }
public async Task <ActionResult <IEnumerable <CategoryToReturnDto> > > GetCategory(int id, int parentId) { var categorys = await _context.Categorys.Where(x => (x.Name != "Root" && x.Name != "All Categories" && x.Name != "Motors")).ToListAsync(); var toReturn = new List <CategoryToReturnDto>(); if (parentId != 0) { var parent = await _context.Categorys.FindAsync(parentId); var parentNode = HierarchyExtensions.ToSqlHierarchyId(parent.Node); foreach (var item in categorys) { var node = HierarchyExtensions.ToSqlHierarchyId(item.Node); if (node.GetLevel().ToSqlInt32() == id && node.GetAncestor(1) == parentNode) { var alt = new CategoryToReturnDto(); alt.Id = item.Id; alt.Name = item.Name; alt.hasSubCategory = item.hasSubCategory; alt.parentId = item.parentId; toReturn.Add(alt); } } } else { foreach (var item in categorys) { var node = HierarchyExtensions.ToSqlHierarchyId(item.Node); if (node.GetLevel().ToSqlInt32() == id) { var alt = new CategoryToReturnDto(); alt.Id = item.Id; alt.Name = item.Name; alt.hasSubCategory = item.hasSubCategory; alt.parentId = item.parentId; toReturn.Add(alt); } } } return(toReturn); }