public int Save(SaveNodeViewModel viewModel)
        {
            using (var context = new DataModel())
            {
                var node = !viewModel.Id.HasValue ? AddNode(context, viewModel) : SaveNode(context, viewModel);

                context.SaveChanges();

                return node.Id;
            }
        }
 private Node AddNode(DataModel context, SaveNodeViewModel viewModel)
 {
     var node = Mapper.Map<SaveNodeViewModel, Node>(viewModel);
     if (viewModel.ParentId.HasValue)
     {
         var parent = context.Nodes.FirstOrDefault(x => x.Id == viewModel.ParentId);
         if (parent == null)
             throw new ArgumentException("Parent node was not found");
         node.Parent = parent;
     }
     context.Nodes.Add(node);
     return node;
 }
        private Node SaveNode(DataModel context, SaveNodeViewModel viewModel)
        {
            var node = context.Nodes.FirstOrDefault(x => x.Id == viewModel.Id);
            if (node == null)
                throw new ArgumentException("Node was not found");

            Mapper.Map(viewModel, node);

            if (viewModel.ParentId.HasValue && (node.Parent == null || viewModel.ParentId.GetValueOrDefault(-1) != node.Parent.Id))
            {
                var parent = context.Nodes.FirstOrDefault(x => x.Id == viewModel.ParentId);
                if (parent == null)
                    throw new ArgumentException("Parent node was not found");
                node.Parent = parent;
            }
            else if (!viewModel.ParentId.HasValue && node.Parent != null)
                node.Parent = null;

            return node;
        }
 public ActionResult Save(SaveNodeViewModel viewModel)
 {
     return Json(Orchestrator.Save(viewModel));
 }