public async Task <IActionResult> Get([FromQuery] RequestResourceParameters resourceParameters) { //task: add dto and links to previous next apges var requests = _requestRepo.GetAllPagination(resourceParameters); //map requests to requestsDto var mappedRequests = _mapper.Map <IEnumerable <RequestDto> >(requests); //Construct links to previous+ next page var previousPage = requests.HasPrevious ? _linkService.CreateResourceUri(resourceParameters, ResourceType.PreviousPage) : null; var nextPage = requests.HasNext ? _linkService.CreateResourceUri(resourceParameters, ResourceType.NextPage) : null; //construct further links for every request mappedRequests = mappedRequests.Select(request => { request = _linkService.CreateLinks(request); return(request); }); var paginationMetadata = new { totalCount = requests.TotalCount, pageSize = requests.PageSize, currentPage = requests.CurrentPage, totalPages = requests.TotalPages, previousPageLink = previousPage, nextPageLink = nextPage }; return(Ok(new { Values = mappedRequests, Metadata = paginationMetadata })); }
public PagedList <Request> GetAllPagination(RequestResourceParameters resourceParams) { var collectionBeforPaging = _context.Requests .Include(r => r.UserAgent) .OrderBy(r => r.DateTime) .AsQueryable(); //filter by Method if type exists if (!string.IsNullOrEmpty(resourceParams.Method)) { collectionBeforPaging = collectionBeforPaging .Where(a => a.Method == resourceParams.Method); } //filter by Destination if type exists if (!string.IsNullOrEmpty(resourceParams.Destination)) { collectionBeforPaging = collectionBeforPaging .Where(a => a.Destination == resourceParams.Destination); } //filter by Source if type exists if (!string.IsNullOrEmpty(resourceParams.Source)) { collectionBeforPaging = collectionBeforPaging .Where(a => a.Source == resourceParams.Source); } //searh if exists if (!string.IsNullOrEmpty(resourceParams.SearchQuery)) { collectionBeforPaging = collectionBeforPaging .Where(a => a.Method.Contains(resourceParams.SearchQuery) || a.Destination.Contains(resourceParams.SearchQuery) || a.Source.Contains(resourceParams.SearchQuery)); } return(PagedList <Request> .Create(collectionBeforPaging, resourceParams.PageNumber, resourceParams.PageSize)); }