public PartialViewResult UpdateTree(TreeEditorViewModel treeCreatorVM, string deleteButton)
        {
            List<Node> originalNodes = db.Nodes.Where(i => i.TreeID == treeCreatorVM.Tree.ID).OrderBy(j=>j.ID).ToList();
            //check if a node is selected, returning an error if its the first node
            if(treeCreatorVM.ParentNodeID == null && originalNodes.Count>0)
            {
                ModelState.AddModelError("NoNodeSelected", "Please select a node");
                return PartialView("_Tree", originalNodes);
            }
            //check if users is trying to delete a node and a stub node is selected
            else if (deleteButton == "true" && originalNodes.SingleOrDefault(i => i.ID == int.Parse(treeCreatorVM.ParentNodeID)).NodeValue == 0)
            {
                ModelState.AddModelError("StubNodeSelected", "Cannot delete a stub node");
                return PartialView("_Tree", originalNodes);
            }
            //return an error if the users has clicked on a parent node and is trying to submit a node. Exception to this being if this is the first node in the tree
            else if(deleteButton == null && originalNodes.Count>0)
            {
                Boolean leafNode = treeRepository.IsLeafNode(int.Parse(treeCreatorVM.ParentNodeID), treeCreatorVM.Tree.ID);
                if (leafNode == false)
                {
                    ModelState.AddModelError("StubNodeSelected", "Cannot insert a node in selected location");
                    return PartialView("_Tree", originalNodes);
                }
            }


            if (deleteButton == "true")//when delete button is pressed
            {
                var node = originalNodes.Single(i => i.ID == int.Parse(treeCreatorVM.ParentNodeID));
                if(node.SolutionNode)
                {
                    treeRepository.DeleteSolutionNode(treeCreatorVM.Tree.ID, treeCreatorVM.ParentNodeID);
                    treeRepository.Save();
                }
                else
                {
                    treeRepository.DeleteRegularNode(treeCreatorVM.Tree.ID, treeCreatorVM.ParentNodeID);
                }
            }
            else if(treeCreatorVM.NodeType == "Solution")//when user is trying to input a solution node
            {
                treeRepository.EnterSolutionNode(treeCreatorVM.ParentNodeID, treeCreatorVM.Tree.ID, treeCreatorVM.Solution, treeCreatorVM.SolutionTitle);
            }
            else if(treeCreatorVM.NodeType == "Equipment")
            {
                treeRepository.EnterEquipmentNode(treeCreatorVM.ParentNodeID, treeCreatorVM.SelectedEquipment.ID, treeCreatorVM.SelectedEquipmentAttribute.ID, treeCreatorVM.Tree.ID);
            }
            else //when user is trying to input a regular node, numeric or otherwise
            {
                treeRepository.EnterAttributeNode(treeCreatorVM.ParentNodeID, treeCreatorVM.SelectedPatientAttribute.ID, treeCreatorVM.Tree.ID, treeCreatorVM.SelectedPatientAttribute.Numeric, treeCreatorVM.SelectedPatientAttributeNumericValue.Value);
            }

            List<Node> nodes = db.Nodes.Where(i => i.TreeID == treeCreatorVM.Tree.ID).OrderBy(j => j.ID).ToList();

            return PartialView("_Tree", nodes);
        }
        public TreeEditorViewModel CreateTreeEditorViewModel()
        {
            var tree = new Tree { Name = "" };
            db.Trees.Add(tree);
            db.SaveChanges();

            var treeCreator = new TreeEditorViewModel
            {
                Tree = tree,
                PatientAttributes = db.PatientAttributes.ToList(),
                Nodes = new List<Node>(),
                EquipmentAttributes = db.EquipmentAttributes.ToList(), 
                Equipment = db.Equipment.ToList()
            };
            return treeCreator;
        }
 public TreeEditorViewModel GetTreeEditorViewModel(int treeId)
 {
     var treeCreator = new TreeEditorViewModel
     {
         Tree = db.Trees.Find(treeId),
         PatientAttributes = db.PatientAttributes.ToList(),
         Nodes = db.Nodes.Where(i => i.TreeID == treeId).OrderBy(j => j.ID).ToList(),
         EquipmentAttributes = db.EquipmentAttributes.ToList(),
         Equipment = db.Equipment.ToList()
     };
     return treeCreator;
 }