Example #1
0
        public virtual async Task <IQueryable <IdentityUser> > CreateFilteredQueryAsync(GetIdentityUsersDetailsInput input)
        {
            var query = await _repository.WithDetailsAsync();

            if (input.OrganizationUnitId != null)
            {
                var code = await UnitManager.GetCodeOrDefaultAsync(input.OrganizationUnitId.Value);

                var organizationUnitIds = (await UnitManager.FindChildrenAsync(input.OrganizationUnitId.Value, true)).Select(x => x.Id).ToList();
                organizationUnitIds.Add(input.OrganizationUnitId.Value);
                query = query.Where(x => x.OrganizationUnits.Any(o => organizationUnitIds.Contains(o.OrganizationUnitId)));
            }
            return(query
                   .WhereIf(
                       !input.Filter.IsNullOrWhiteSpace(),
                       u =>
                       u.UserName.Contains(input.Filter) ||
                       u.Email.Contains(input.Filter) ||
                       (u.Name != null && u.Name.Contains(input.Filter)) ||
                       (u.Surname != null && u.Surname.Contains(input.Filter)) ||
                       (u.PhoneNumber != null && u.PhoneNumber.Contains(input.Filter))
                       )
                   .WhereIf(input.RoleId != null, u => u.Roles.Any(x => x.RoleId == input.RoleId))
                   //.WhereIf(input.OrganizationUnitId != null, u => u.OrganizationUnits.Any(x => x.OrganizationUnitId == input.OrganizationUnitId))
                   );
        }
        //[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()
                       ));
        }