public async Task <ActionResult <Category> > PostCategory(Category category) { if (category.CategoryUp == "Root") { category.NodePath = "/1/"; category.parentId = 0; } if (category.CategoryUp == "N/A") { category.NodePath = "/1/"; category.parentId = -1; } else { category.parentId = _context.Categorys.FirstOrDefault(x => x.Name == category.CategoryUp).Id; var parentNode = new byte[2147483591]; var lastChild = new Category(); try { parentNode = _context.Categorys.FirstOrDefault(x => x.Name == category.CategoryUp).Node; } catch (Exception ex) { return(BadRequest(new { message = "Parent does not exist. Please verified the spelling or Add this as a parent first. => Error message: " + ex.Message })); } try { lastChild = _context.Categorys.Where(x => x.CategoryUp == category.CategoryUp) .OrderByDescending(x => x.Node) .FirstOrDefault(); SqlHierarchyId lastSqlNode = HierarchyExtensions.ToSqlHierarchyId(lastChild.Node); category.Node = HierarchyExtensions.ToByteArray(HierarchyExtensions.ToSqlHierarchyId(parentNode).GetDescendant(lastSqlNode, new SqlHierarchyId())); if (category.CategoryUp == "N/A") { category.NodePath = "/1/"; category.parentId = -1; } } catch (Exception ex) { category.Node = HierarchyExtensions.ToByteArray(HierarchyExtensions.ToSqlHierarchyId(parentNode).GetDescendant(new SqlHierarchyId(), new SqlHierarchyId())); } } _context.Categorys.Add(category); await _context.SaveChangesAsync(); return(CreatedAtAction("GetCategory", new { id = category.Id }, category)); }
public async Task <ActionResult <NewDomain> > PostNewDomain(NewDomain newDomain) { //Check if the Domain Type is valid if (newDomain.DomainTypeId > 7 || newDomain.DomainTypeId < 1) { return(BadRequest(new { message = @"The Domain Type Id is not valid. Valid numbers => 1: Root, 2: Company, 3: Sales Division, 4: Sales District, 5: Profit Center, 6: Distribution Center, 7: Operation Center" })); } if (newDomain.Parentt == "N/A") { newDomain.NodePath = "/1/"; } else { var parentNode = new byte[2147483591]; var lastChild = new NewDomain(); try { parentNode = _context.NewDomains.FirstOrDefault(x => x.DomainName == newDomain.Parentt).Node; } catch (Exception ex) { return(BadRequest(new { message = "Parent does not exist. Please verified the spelling or Add this as a parent first. => Error message: " + ex.Message })); } try { lastChild = _context.NewDomains.Where(x => x.Parentt == newDomain.Parentt) .OrderByDescending(x => x.Node) .FirstOrDefault(); SqlHierarchyId lastSqlNode = HierarchyExtensions.ToSqlHierarchyId(lastChild.Node); newDomain.Node = HierarchyExtensions.ToByteArray(HierarchyExtensions.ToSqlHierarchyId(parentNode).GetDescendant(lastSqlNode, new SqlHierarchyId())); } catch (Exception ex) { newDomain.Node = HierarchyExtensions.ToByteArray(HierarchyExtensions.ToSqlHierarchyId(parentNode).GetDescendant(new SqlHierarchyId(), new SqlHierarchyId())); } } _context.NewDomains.Add(newDomain); await _context.SaveChangesAsync(); return(CreatedAtAction("GetNewDomain", new { id = newDomain.DomainId }, newDomain)); }
private static Category setCategory(StoreContext _context, Category category) { var parentNode = new byte[2147483591]; var lastChild = new Category(); parentNode = _context.Categorys.FirstOrDefault(x => x.Name == category.CategoryUp).Node; try { lastChild = _context.Categorys.Where(x => x.CategoryUp == category.CategoryUp) .OrderByDescending(x => x.Node) .FirstOrDefault(); SqlHierarchyId lastSqlNode = HierarchyExtensions.ToSqlHierarchyId(lastChild.Node); category.Node = HierarchyExtensions.ToByteArray(HierarchyExtensions.ToSqlHierarchyId(parentNode).GetDescendant(lastSqlNode, new SqlHierarchyId())); } catch (Exception ex) { category.Node = HierarchyExtensions.ToByteArray(HierarchyExtensions.ToSqlHierarchyId(parentNode).GetDescendant(new SqlHierarchyId(), new SqlHierarchyId())); } return(category); }