public async Task <IActionResult> PutNewDomain(int id, NewDomain newDomain) { if (id != newDomain.DomainId) { return(BadRequest()); } _context.Entry(newDomain).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!NewDomainExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
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)); }