private void TraverseTree(TTree parent, ICollection <TTree> children, Action <TTree> childrenAction = null)
        {
            if (children == null || !children.Any())
            {
                return;
            }

            var duplicateNames = children
                                 .GroupBy(x => x.Name)
                                 .Where(x => x.Count() > 1)
                                 .Select(x => x.Key)
                                 .ToList();

            if (duplicateNames.Any())
            {
                throw new UserFriendlyException(_generalTreeStringLocalizer["GeneralTreeNameIsDuplicate", duplicateNames.First()]);
            }

            var index = 0;

            foreach (var c in children)
            {
                c.Code     = _generalTreeCodeGenerator.MergeCode(parent.Code, _generalTreeCodeGenerator.CreateCode(++index));
                c.Level    = c.Code.Split('.').Length;
                c.FullName = parent.FullName + _generalTreeOptions.Hyphen + c.Name;

                childrenAction?.Invoke(c);

                TraverseTree(c, c.Children, childrenAction);
            }
        }
Example #2
0
        public async Task CreateAsync()
        {
            //Act
            await CreateRegion("beijing");

            //Assert
            var region = await GetRegion("beijing");

            region.ShouldNotBeNull();
            region.Name.ShouldBe("beijing");
            region.FullName.ShouldBe("beijing");
            region.Code.ShouldBe(_generalTreeCodeGenerator.CreateCode(1));
            region.Level.ShouldBe(1);
            region.ParentId.ShouldBeNull();
        }