public async Task <IActionResult> Move(Guid id, [FromBody] VMMoveNodeData node) { if (id == node.DestinationId) { return(BadRequest()); } try { await _service.MoveNode(node, id); return(Ok()); } catch (Exception) { return(BadRequest()); } }
public async Task MoveNode(VMMoveNodeData node, Guid id) { var nodeList = new List <DbNode>(); var destinationLevel = _context.Nodes.Where(x => x.IdNode == node.DestinationId).Select(x => x.Level).FirstOrDefault(); var slectedName = _context.Nodes.Where(x => x.IdNode == id).Select(x => x.Name).FirstOrDefault(); var newNode = new DbNode { IdNode = id, Name = slectedName, Level = destinationLevel + 1, ParentId = node.DestinationId }; nodeList.Add(newNode); var childsList = _context.Nodes.Where(x => x.ParentId == id).ToList(); foreach (var child in childsList) { newNode = new DbNode { IdNode = child.IdNode, Name = child.Name, Level = destinationLevel + 2, ParentId = child.ParentId }; nodeList.Add(newNode); } await DeleteAsync(id); foreach (var createdNode in nodeList) { var result = await _context.Nodes.AddAsync(createdNode); } _context.SaveChanges(); }