Exemplo n.º 1
0
        //todo: not allow modify children
        public async override Task <TEntity> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
        {
            var oldEntity = await this.FindAsync(entity.Id);

            if (oldEntity.ParentId == entity.Id)
            {
                await base.UpdateAsync(entity, autoSave, cancellationToken);

                return(entity);
            }
            var parentId = entity.ParentId;
            //Should find children before Code change
            var children = await FindChildrenAsync(entity.Id, true);

            //Store old code of Tree
            var oldCode = oldEntity.Code;

            //Move Tree
            var code = await GetNextChildCodeAsync(parentId);

            entity.SetCode(code);

            //Update Children Codes
            foreach (var child in children)
            {
                var childCode = TreeCodeGenerator.AppendCode(entity.Code, TreeCodeGenerator.GetRelativeCode(child.Code, oldCode));
                child.SetCode(childCode);
            }
            await base.UpdateAsync(entity, autoSave, cancellationToken);

            return(entity);
        }
Exemplo n.º 2
0
        protected virtual async Task <string> GetNextChildCodeAsync(Guid?parentId)
        {
            var lastChild = await GetLastChildOrNullAsync(parentId);

            if (lastChild == null)
            {
                var parentCode = parentId != null ? await GetCodeAsync(parentId.Value) : null;

                return(TreeCodeGenerator.AppendCode(parentCode, TreeCodeGenerator.CreateCode(1)));
            }

            return(TreeCodeGenerator.CalculateNextCode(lastChild.Code));
        }