public async Task <string> AddAsync(Core.Entities.Content.Node node) { node.Id = Guid.NewGuid().ToString(); node.CreatedDate = DateTimeOffset.UtcNow.ToString("s"); if (node.CustomFields != null) { node.CustomFields.Id = Guid.NewGuid().ToString(); node.CustomFields.NodeId = node.Id; } await _nodeRepository.AddAsync(node); var path = node.ParentId; if (!string.IsNullOrEmpty(node.ParentId)) { var parent = await GetAsync(node.ParentId); parent.ChildCount++; await _nodeRepository.UpdateAsync(parent); if (!string.IsNullOrEmpty(parent.Path)) { path = $"{parent.Path},{node.ParentId}"; } } node.Path = path; if (!string.IsNullOrEmpty(node.Path)) { var nodes = await GetAncestors(node); foreach (var ancestorNode in nodes) { ancestorNode.DescendantCount++; await _nodeRepository.UpdateAsync(ancestorNode); } } await _nodeRepository.SaveChangesAsync(); return(node.Id); }
public async Task <string> AddAsync(Core.Entities.Content.Node node) { node.Id = Guid.NewGuid().ToString(); node.CreatedDate = DateTimeOffset.UtcNow.ToString("s"); if (node.CustomFields != null) { node.CustomFields.Id = Guid.NewGuid().ToString(); node.CustomFields.EntityId = node.Id; } _nodeRepository.Add(node); var rootId = node.ParentId; if (!string.IsNullOrEmpty(node.ParentId)) { var parent = await _nodeRepository.GetAsync(node.ParentId); parent.ChildCount += 1; _nodeRepository.Update(parent); if (!string.IsNullOrEmpty(parent.RootId)) { rootId = parent.RootId; } } node.RootId = rootId; if (!string.IsNullOrEmpty(node.RootId)) { var root = await _nodeRepository.GetAsync(node.RootId); root.DescendantCount += 1; _nodeRepository.Update(root); } await _nodeRepository.SaveChangesAsync(); return(node.Id); }