public virtual async Task UpdateAsync(TTree tree, Action <TTree> childrenAction = null) { CheckSameName(tree); var oldFullName = tree.FullName; if (tree.ParentId.HasValue) { var parent = await _generalTreeRepository.FirstOrDefaultAsync(EqualId(tree.ParentId.Value)); Check.NotNull(parent, nameof(parent)); tree.FullName = parent.FullName + _generalTreeConfiguration.Hyphen + tree.Name; } else { tree.FullName = tree.Name; } var children = await GetChildrenAsync(tree.Id, true); foreach (var child in children) { child.FullName = GeneralTreeCodeGenerate.MergeFullName(tree.FullName, GeneralTreeCodeGenerate.RemoveParentCode(child.FullName, oldFullName)); childrenAction?.Invoke(child); } }
public virtual async Task MoveAsync(TPrimaryKey id, TPrimaryKey?parentId) { var tree = await _generalTreeRepository.GetAsync(id); if (tree.ParentId.Equals(parentId)) { return; } //Should find children before Code change var children = await GetChildrenAsync(id, true); //Store old code and full name of Tree var oldCode = tree.Code; var oldFullName = tree.FullName; //Move Tree tree.Code = await GetNextChildCodeAsync(parentId); tree.Level = tree.Code.Split('.').Length; tree.ParentId = parentId; tree.FullName = await GetChildFullNameAsync(parentId, tree.Name); CheckSameName(tree); //Update Children Codes and FullName foreach (var child in children) { child.Code = GeneralTreeCodeGenerate.MergeCode(tree.Code, GeneralTreeCodeGenerate.RemoveParentCode(child.Code, oldCode)); child.FullName = GeneralTreeCodeGenerate.MergeFullName(tree.FullName, GeneralTreeCodeGenerate.RemoveParentCode(child.FullName, oldFullName)); child.Level = child.Code.Split('.').Length; } }