Exemplo n.º 1
0
        public async Task <List <OrganizationUnit> > FindChildrenAsync(Guid?parentId, bool recursive = false)
        {
            if (!recursive)
            {
                return(await OrganizationUnitRepository.GetChildrenAsync(parentId, includeDetails : true));
            }

            if (!parentId.HasValue)
            {
                return(await OrganizationUnitRepository.GetListAsync(includeDetails : true));
            }

            var code = await GetCodeOrDefaultAsync(parentId.Value);

            return(await OrganizationUnitRepository.GetAllChildrenWithParentCodeAsync(code, parentId, includeDetails : true));
        }
        //[Authorize(OrganizationUnitPermissions.OrganizationUnitLookup.Default)]
        public virtual async Task <ListResultDto <OrganizationUnitDto> > GetChildrenAsync(Guid?parentId, bool recursive = false)
        {
            List <OrganizationUnit> organizationUnits;

            if (recursive)
            {
                if (!parentId.HasValue)
                {
                    organizationUnits = await GetAllListAsync();
                }
                else
                {
                    var code = await OrganizationUnitManager.GetCodeOrDefaultAsync(parentId.Value);

                    organizationUnits = await OrganizationUnitRepository.GetAllChildrenWithParentCodeAsync(code, parentId, includeDetails : false);
                }
            }
            else
            {
                organizationUnits = await OrganizationUnitRepository.GetChildrenAsync(parentId, includeDetails : false);
            }

            var dtoList = ObjectMapper.Map <List <OrganizationUnit>, List <OrganizationUnitDto> >(organizationUnits);

            if (recursive)
            {
                foreach (var organizationUnit in dtoList)
                {
                    organizationUnit.ChildrenCount = dtoList.Count(ou => ou.ParentId == organizationUnit.Id);
                }
            }
            else
            {
                foreach (var organizationUnit in dtoList)
                {
                    organizationUnit.ChildrenCount = (await OrganizationUnitRepository.GetChildrenAsync(organizationUnit.Id, false)).Count;
                }
            }

            return(new ListResultDto <OrganizationUnitDto>(
                       dtoList.OrderBy(ou => ou.Position)
                       .ThenBy(ou => ou.Code)
                       .ToList()
                       ));
        }
        public virtual async Task <OrganizationUnitDto> FindByCodeAsync(string code)
        {
            var allOrganizationUnits = await OrganizationUnitRepository.GetAllChildrenWithParentCodeAsync(code, Guid.NewGuid(), false);

            var organizationUnit = allOrganizationUnits.FirstOrDefault(ou => ou.Code == code);

            if (organizationUnit == null)
            {
                return(null);
            }
            else
            {
                var dto = ObjectMapper.Map <OrganizationUnit, OrganizationUnitDto>(organizationUnit);
                dto.ChildrenCount = (await OrganizationUnitRepository.GetChildrenAsync(dto.Id, false)).Count;

                return(dto);
            }
        }