public async Task <ActionResult <TreeModel> > CreateThematicTreeAsync([FromBody] NodeModel node) { //TEMPORARY, JUST TO CHECK THE ENDPOINT IN LIVE bool isRootCreation = node == null; if (!isRootCreation) { if (node.Name == null || node.WithChildren.HasValue || !node.ParentId.HasValue) { return(BadRequest(@"For the creation of a node, a name and a parentId are necessary and don't put the field withchildren in the payload, please.")); } } if (isRootCreation) { CurrentModel = new TreeModel(0, "root"); } else { var nodeParentFound = CurrentModel.FindNode(node.ParentId.Value); int position = node.Position.HasValue ? node.Position.Value : -1; _ = new TreeModel(0, node.Name, nodeParentFound, position); } return(Ok(await Task.FromResult(CurrentModel))); }
public async Task <ActionResult <TreeModel> > GetTreeAsync(int nodeId) { var nodeFound = CurrentModel.FindNode(nodeId); if (nodeFound == null) { return(NotFound()); } return(Ok(await Task.FromResult(nodeFound))); }
public async Task <ActionResult <TreeModel> > UpdateThematicTreeAsync(int nodeId, [FromBody] NodeModel node) { var nodeToMoveFound = CurrentModel.FindNode(nodeId); if (nodeToMoveFound == null) { return(NotFound()); } if (nodeToMoveFound.IsRoot) { return(BadRequest("The root node is not updatable.")); } var nodeParentFound = CurrentModel.FindNode(node.ParentId.Value); if (nodeParentFound == null) { return(BadRequest("The parent node does not exist.")); } if (node == null || !node.ParentId.HasValue) { return(BadRequest("For the updating of a node and a parentId are necessary, please.")); } int position = node.Position.HasValue ? node.Position.Value : -1; if (nodeToMoveFound.Parent.Id != nodeParentFound.Id || position != nodeToMoveFound.Position) { nodeParentFound.AddChild( nodeToMoveFound.RemoveNode(node.WithChildren.HasValue && node.WithChildren.Value) , position ); } if (node.Name != null) { nodeToMoveFound.Name = node.Name; } return(Ok(await Task.FromResult(CurrentModel))); }