示例#1
0
        public virtual PagedList <TDto> Search(
            IncludeSpecification <TDto> includeSpecification,
            FilterSpecification <TDto> filterSpecification,
            OrderBySpecification <TDto> orderBySpecification,
            int?pageNo        = null,
            int?pageSize      = null,
            bool getFullGraph = false)
        {
            var filterConverted = filterSpecification.Map <TEntity>(Mapper);

            if (!OrderByMapper.ValidOrderByFor <TDto, TEntity>(orderBySpecification.OrderByString))
            {
                throw new BadRequestException(Messages.OrderByInvalid);
            }

            //Ideally OrderByMapper could also convert Expressions but it only handles strings.
            var orderByConverted = string.IsNullOrEmpty(orderBySpecification.OrderByString) ? orderBySpecification.Map <TEntity>(Mapper) : orderBySpecification.Map <TEntity>(OrderByMapper);

            var includesConverted = includeSpecification.Map <TEntity>(Mapper);

            int?skip = null;

            if (pageNo.HasValue && pageSize.HasValue)
            {
                skip = (pageNo.Value - 1) * pageSize.Value;
            }

            var entityList = Repository.SpecificationQuery(includesConverted, filterConverted, orderByConverted, skip, pageSize, GetFullGraph || getFullGraph).ToCountList();

            var entities = entityList.ToList();

            var dtoList = new PagedList <TDto>(entities.Select(Mapper.Map <TEntity, TDto>).ToList(), entityList.TotalCount, pageNo ?? 1, pageSize ?? entityList.TotalCount);

            return(dtoList);
        }
        public virtual async Task <PagedList <TDto> > SearchAsync(
            IncludeSpecification <TDto> includeSpecification,
            FilterSpecification <TDto> filterSpecification,
            OrderBySpecification <TDto> orderBySpecification,
            int?pageNo        = null,
            int?pageSize      = null,
            bool getFullGraph = false,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            await AuthorizeResourceOperationAsync(ResourceCollectionsCore.CRUD.Operations.Read, ResourceCollectionsCore.CRUD.Operations.ReadOwner);

            var filterConverted = filterSpecification.Map <TEntity>(Mapper);

            if (!OrderByMapper.ValidOrderByFor <TDto, TEntity>(orderBySpecification.OrderByString))
            {
                throw new BadRequestError(Messages.OrderByInvalid);
            }

            //Ideally OrderByMapper could also convert Expressions but it only handles strings.
            var orderByConverted = string.IsNullOrEmpty(orderBySpecification.OrderByString) ? orderBySpecification.Map <TEntity>(Mapper) : orderBySpecification.Map <TEntity>(OrderByMapper);

            var includesConverted = includeSpecification.Map <TEntity>(Mapper);

            int?skip = null;

            if (pageNo.HasValue && pageSize.HasValue)
            {
                skip = (pageNo.Value - 1) * pageSize.Value;
            }

            var entityList = await Repository.SpecificationQuery(includesConverted, filterConverted, orderByConverted, skip, pageSize, GetFullGraph || getFullGraph).ToCountListAsync(cancellationToken).ConfigureAwait(false);

            var entities = entityList.ToList();

            foreach (var entity in entities)
            {
                await AuthorizeResourceOperationAsync(entity, ResourceCollectionsCore.CRUD.Operations.Read, ResourceCollectionsCore.CRUD.Operations.ReadOwner);
            }

            var dtoList = new PagedList <TDto>(entities.Select(Mapper.Map <TEntity, TDto>).ToList(), entityList.TotalCount, pageNo ?? 1, pageSize ?? entityList.TotalCount);

            return(dtoList);
        }