private Boolean IsDescendantOf(hidServices <GLaccounts> hs, string pk) { SqlHierarchyId h = Conversions.Bytes2HierarchyId(hid); SqlHierarchyId pkHid = Conversions.Bytes2HierarchyId(hs.GetHid(pk)); return((Boolean)h.IsDescendantOf(pkHid)); }
public void MovSubTree(String pk, String novaMae) { try { long tree = long.Parse(pk); long novaM = long.Parse(novaMae); T t = repo.Find(new object[] { tree }); byte[] hidAsByte = t.hid; SqlHierarchyId hid = Conversions.Bytes2HierarchyId(t.hid); byte[] novoHidAsByte = GetNextSonHid(novaMae); // novo hid da subtree SqlHierarchyId novoHid = Conversions.Bytes2HierarchyId(novoHidAsByte); t.hid = novoHidAsByte; // novo hid do nó da subtree db.Entry(t).State = EntityState.Modified; t = repo.Find(new object[] { novaM }); SqlHierarchyId novaMaehid = Conversions.Bytes2HierarchyId(t.hid); foreach (T r in repo.Get) // alterar os hids das descendentes { SqlHierarchyId h = Conversions.Bytes2HierarchyId(r.hid); if (h.IsDescendantOf(hid) && hidAsByte != r.hid) { r.hid = Conversions.HierarchyId2Bytes(h.GetReparentedValue(hid, novoHid)); db.Entry(r).State = EntityState.Modified; } } db.SaveChanges(); } catch (Exception ex) { throw new Exception("Erro ao mover nó: " + ex.Message); } }
public void delete(string pk) { try { T t = repo.Find(new object[] { long.Parse(pk) }); SqlHierarchyId hid = Conversions.Bytes2HierarchyId(t.hid); bool hasDescendant = false; foreach (var r in repo.Get) { SqlHierarchyId h = Conversions.Bytes2HierarchyId(r.hid); if (h.IsDescendantOf(hid) && (h != hid)) { hasDescendant = true; break; } } if (hasDescendant) { throw new Exception("Existem descendentes."); } db.Entry(t).State = EntityState.Deleted; db.SaveChanges(); } catch (Exception ex) { throw new Exception("Problema na exclusão: " + ex.Message); } }
public static HierarchyId CommonAncestor( SqlHierarchyId h1, HierarchyId h2 ) { while (!h1.IsDescendantOf(h2)) { h1 = h1.GetAncestor(1); } return(h1); }
public async Task <ActionResult <List <Domaintest> > > GetDescendants(string name) { object mainDomain = (from x in _context.Domains.Where(x => x.DomainName == name) select new { Id = x.DomainId, Name = x.DomainName, Type = x.DomainTypeId, Parent = x.Parentt, Path = x.Level.ToString(), HighLevel = x.HighLevel }).FirstOrDefault(); dynamic mainDyn = mainDomain; int hL = mainDyn.HighLevel; SqlHierarchyId mainNode = SqlHierarchyId.Parse(mainDyn.Path); object domain = (from x in _context.Domains.Where(x => x.HighLevel > hL) select new { Id = x.DomainId, Name = x.DomainName, Type = x.DomainTypeId, Parent = x.Parentt, Path = x.Level.ToString(), HighLevel = x.HighLevel }).ToList(); //var type = domain.GetType(); //var strNode = (string) type.GetProperty("Path").GetValue(domain, null); List <Domaintest> resultList = new List <Domaintest>(); dynamic listDyn = domain; foreach (var dyn in listDyn) { int myId = dyn.Id; string strName = dyn.Name; int iType = dyn.Type; string strParent = dyn.Parent; string strNode = dyn.Path; int hLevel = dyn.HighLevel; SqlHierarchyId node = SqlHierarchyId.Parse(dyn.Path); Domaintest result = new Domaintest(myId, strName, iType, strParent, strNode, hLevel); if (node.IsDescendantOf(mainNode)) { resultList.Add(result); } } return(resultList); }
public void DeleteTree(String pk) // remove toda uma sub árvore { try { long tree = long.Parse(pk); T t = repo.Find(new object[] { tree }); SqlHierarchyId hid = Conversions.Bytes2HierarchyId(t.hid); Int16 maxNiv = 0; foreach (var r in repo.Get) { SqlHierarchyId h = Conversions.Bytes2HierarchyId(r.hid); if (h.IsDescendantOf(hid)) { if (h.GetLevel() > maxNiv) { maxNiv = (Int16)h.GetLevel(); } } } for (int i = maxNiv; i > 0; i--) { foreach (T r in repo.Get) { SqlHierarchyId h = Conversions.Bytes2HierarchyId(r.hid); if (h.IsDescendantOf(hid)) { db.Entry(r).State = EntityState.Deleted; } } } db.SaveChanges(); } catch (Exception ex) { throw new Exception("Erro em exclusão de sub-árvore: " + ex.Message); } }
public async Task <ActionResult <Pagination <ItemToReturnDto> > > GetItems( [FromQuery] ItemSpecParams itemParams) { //Get the category choose if (itemParams.CategoryId == null) { itemParams.CategoryId = 0 /*Root*/; } if (itemParams.BrandId == null) { //Initialize the BrandId List itemParams.BrandId = new List <int?>(); } itemParams.SubCategory = new List <int?>(); itemParams.SubCategory.Add(itemParams.CategoryId); var cat = await _context.Categorys.Where(x => x.Id == itemParams.CategoryId).FirstOrDefaultAsync(); SqlHierarchyId catNode = HierarchyExtensions.ToSqlHierarchyId(cat.Node); //Get all subCategories var allCategories = await _context.Categorys.ToListAsync(); var subCategorys = new List <Category>(); // Select all subcategories foreach (var cats in allCategories) { SqlHierarchyId node = HierarchyExtensions.ToSqlHierarchyId(cats.Node); if (node.IsDescendantOf(catNode) && node != catNode) { subCategorys.Add(cats); itemParams.SubCategory.Add(cats.Id); } } var spec = new ItemsWithTypesAndBrandsSpecification(itemParams); var countSpec = new ItemWithFiltersForCountSpecificication(itemParams); var totalItems = await _itemsRepo.CountAsync(countSpec); var items = await _itemsRepo.ListAsync(spec); IList <ItemToReturnDto> itemsToReturns = new List <ItemToReturnDto>(); foreach (var elts in items) { int id = elts.Id; List <string> colorName = new List <string>(); List <string> sizeName = new List <string>(); List <ReviewDto> reviewList = new List <ReviewDto>(); List <ImageToReturnDto> imageList = new List <ImageToReturnDto>(); int ratingsCount = 0; int ratingsValue = 0; var colors = await _context.Colors.Where(x => x.ItemId == id).ToListAsync(); if (colors.Count > 0) { elts.Color = colors; foreach (var elt in elts.Color) { colorName.Add(elt.Name); } } var sizes = await _context.Sizes.Where(x => x.ItemId == id).ToListAsync(); if (sizes.Count > 0) { elts.Size = sizes; foreach (var elt in elts.Size) { sizeName.Add(elt.Name); } } var images = await _context.Images.Where(x => x.ItemId == id).ToListAsync(); if (images.Count > 0) { elts.Images = images; foreach (var elt in elts.Images) { ImageToReturnDto img = new ImageToReturnDto(); img.small = (ApiUrl + elt.UrlSmall); img.medium = (ApiUrl + elt.UrlMedium); img.big = (ApiUrl + elt.UrlBig); imageList.Add(img); } } var reviews = await _context.Reviews.Where(x => x.ItemId == id).ToListAsync(); if (reviews.Count > 0) { elts.Review = reviews; foreach (var elt in elts.Review) { ReviewDto rev = new ReviewDto(); rev.ReviewerName = elt.ReviewerName; rev.ReviewerPhoto = (ApiUrl + elt.ReviewerPhoto); rev.ReviewMessage = elt.ReviewMessage; rev.rate = elt.rate; if (elt.rate == 1) { rev.sentiment = "sentiment_very_dissatisfied"; } else if (elt.rate == 2) { rev.sentiment = "sentiment_dissatisfied"; } else if (elt.rate == 3 || elt.rate == 4) { rev.sentiment = "sentiment_satisfied"; } else if (elt.rate == 5) { rev.sentiment = "sentiment_very_satisfied"; } rev.ReviewDate = elt.ReviewDate; reviewList.Add(rev); ratingsCount++; ratingsValue += elt.rate; } } var itemsToReturn = new ItemToReturnDto { Id = id, Name = elts.Name, Description = elts.Description, OldPrice = elts.OldPrice, NewPrice = elts.NewPrice, Discount = elts.Discount, RatingsCount = ratingsCount, RatingsValue = ratingsValue, availibilityCount = elts.availibilityCount, cartCount = elts.cartCount, TechnicalDescription = elts.TechnicalDescription, AdditionalInformation = elts.AdditionalInformation, Weight = elts.Weight, Color = colorName, Size = sizeName, Images = imageList, Reviews = reviewList, CategoryId = elts.Category.Id, BrandName = elts.ProductBrand.Name, CategoryName = elts.Category.Name, PictureUrl = ApiUrl + elts.PictureUrl }; itemsToReturns.Add(itemsToReturn); } //var data = _mapper // .Map<IReadOnlyList<Item>, IReadOnlyList<ItemToReturnDto>>(items); return(Ok(new Pagination1 <ItemToReturnDto>(itemParams.PageIndex, itemParams.PageSize, totalItems, itemsToReturns)));; }
static bool IsDescendantOf(SqlHierarchyId child, SqlHierarchyId parent) { return(child.IsDescendantOf(parent).Value); }
static bool IsDescendantOf(SqlHierarchyId child, SqlHierarchyId parent) { return child.IsDescendantOf(parent).Value; }