Beispiel #1
0
        GetRoles(ResourceParameters resourceParameters)
        {
            // Change return data type to pagedlist
            if (!_propertyMappingService
                .ValidMappingExistsFor(resourceParameters.OrderBy, PropertiesMappingProfiles._userPropertyMinInfoMapping))
            {
                return(null, null);
            }

            var collectionBeforePaging = _roleRepository.GetRoles();

            if (collectionBeforePaging == null)
            {
                return((null, null));
            }

            var sortingCollection = _applySort.ApplySort(collectionBeforePaging,
                                                         resourceParameters.OrderBy,
                                                         PropertiesMappingProfiles._rolePropertyMapping);

            // Applying querying
            if (!string.IsNullOrEmpty(resourceParameters.SearchQuery))
            {
                // Trim and ignore casing
                var searchQueryForWhereClause = resourceParameters.SearchQuery
                                                .Trim().ToLowerInvariant();

                sortingCollection = sortingCollection
                                    .Where(w => w.RoleName.ToLowerInvariant().Contains(searchQueryForWhereClause));
            }

            var sortedCollection = (IEnumerable <Entities.Role>)sortingCollection.ToList();

            var sortedCollectionDto = _mapper.Map <IEnumerable <Models.RoleDtoModels.RoleDto> >(sortedCollection);

            var sortedCollectionIQuerableDto = sortedCollectionDto.AsQueryable <Models.RoleDtoModels.RoleDto>();

            var pagedListDto = PagedList <Models.RoleDtoModels.RoleDto> .Create(sortedCollectionIQuerableDto,
                                                                                resourceParameters.pageNumber,
                                                                                resourceParameters.PageSize);

            // Generating PagedList metadata
            // Creating links to send in the response with _createResourceUri service
            // 1st param: ResourceParameters
            // 2nd and 3rd param: boolean properties of PagedList
            // 4th param: ActionName (without "Get"), used to reference its controller (Get)Users

            var previousPageLink = pagedListDto.HasPrevious ?
                                   _createResourceUri.CreateResource(resourceParameters,
                                                                     ResourceUriType.PreviousPage, "Roles") : null;

            var nextPageLink = pagedListDto.HasNext ?
                               _createResourceUri.CreateResource(resourceParameters,
                                                                 ResourceUriType.NextPage, "Roles") : null;

            var paginationMetadata = new
            {
                previousPageLink,
                nextPageLink,
                totalCount  = pagedListDto.TotalCount,
                pageSize    = pagedListDto.PageSize,
                currentPage = pagedListDto.CurrentPage,
                totalPages  = pagedListDto.TotalPages
            };

            // Returning a tuple of: object metadata and PagedList<T>
            return((paginationMetadata, pagedListDto));
        }
Beispiel #2
0
        public (object, PagedList <ModelsDto.UsuarioDto>) GetUsersPagedList(ResourceParameters resourceParameters)
        {
            // Change return data type to pagedlist
            if (!_propertyMappingService
                .ValidMappingExistsFor(resourceParameters.OrderBy, PropertiesMappingProfiles._userPropertyMinInfoMapping))
            {
                return(null, null);
            }

            // Getting an IQuerable obj of Entities.Usuario
            var collectionBeforePaging = _userRepository.GetUsersList();

            if (collectionBeforePaging == null)
            {
                return((null, null));
            }

            // Using sorting service into IQuerable obj
            var sortingCollection =
                _applySort.ApplySort(collectionBeforePaging,
                                     resourceParameters.OrderBy,
                                     PropertiesMappingProfiles._userPropertyMinInfoMapping);

            // Applying querying
            if (!string.IsNullOrEmpty(resourceParameters.SearchQuery))
            {
                // Trim and ignore casing
                var searchQueryForWhereClause = resourceParameters.SearchQuery
                                                .Trim().ToLowerInvariant();

                sortingCollection = sortingCollection
                                    .Where(w => w.Email.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                           w.ApellidoPaterno.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                           w.ApellidoMaterno.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                           w.Nombre.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                           w.CodigoPostal.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                           w.TelefonoCasa.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                           w.TelefonoCelular.ToLowerInvariant().Contains(searchQueryForWhereClause));
            }

            // Parsing IQuerable obj to a IEnumerable obj in order to map them
            var sortedCollection = (IEnumerable <Entities.Usuario>)sortingCollection.ToList();

            // Mapping from IEnumerable<Entities.Usuario> to a UsuarioDto model
            var sortedCollectionDto = _mapper.Map <IEnumerable <ModelsDto.UsuarioDto> >(sortedCollection);

            // Parsing them again in order to add them to a PagedList<T>
            var sortedCollectionIQuerableDto = sortedCollectionDto.AsQueryable <ModelsDto.UsuarioDto>();

            var pagedListDto = PagedList <ModelsDto.UsuarioDto> .Create(sortedCollectionIQuerableDto,
                                                                        resourceParameters.pageNumber,
                                                                        resourceParameters.PageSize);

            // Generating PagedList metadata
            // Creating links to send in the response with _createResourceUri service
            // 1st param: ResourceParameters
            // 2nd and 3rd param: boolean properties of PagedList
            // 4th param: ActionName (without "Get"), used to reference its controller (Get)Users

            var previousPageLink = pagedListDto.HasPrevious ?
                                   _createResourceUri.CreateResource(resourceParameters,
                                                                     ResourceUriType.PreviousPage, "Users") : null;

            var nextPageLink = pagedListDto.HasNext ?
                               _createResourceUri.CreateResource(resourceParameters,
                                                                 ResourceUriType.NextPage, "Users") : null;

            var paginationMetadata = new
            {
                previousPageLink,
                nextPageLink,
                totalCount  = pagedListDto.TotalCount,
                pageSize    = pagedListDto.PageSize,
                currentPage = pagedListDto.CurrentPage,
                totalPages  = pagedListDto.TotalPages
            };


            // Returning a tuple of: object metadata and PagedList<T>
            return((paginationMetadata, pagedListDto));
        }