public async Task <ActionResult> GetDataflows([FromQuery] DataflowResourceParameters pagedResourceParameters, CancellationToken ct) { var dataflowDtos = await _dataflowService.GetDataflowsAsync(pagedResourceParameters, ct); var previousPageLink = dataflowDtos.HasPrevious ? CreateDataflowsResourceUri(pagedResourceParameters, Enums.ResourceUriType.PreviousPage) : null; var nextPageLink = dataflowDtos.HasNext ? CreateDataflowsResourceUri(pagedResourceParameters, Enums.ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = dataflowDtos.TotalCount, pageSize = dataflowDtos.PageSize, currentPage = dataflowDtos.CurrentPage, totalPages = dataflowDtos.TotalPages, previousPageLink, nextPageLink }; if (dataflowDtos == null) { return(NotFound()); } Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); return(Ok(dataflowDtos)); }
public async Task <PagedList <DataflowDto> > GetDataflowsAsync(DataflowResourceParameters resourceParameters, CancellationToken ct) { var dataflowEntities = _uow.Dataflows.GetAll(); if (!string.IsNullOrEmpty(resourceParameters.Status)) { if (Enum.TryParse(resourceParameters.Status, true, out DataflowStatus status)) { dataflowEntities = dataflowEntities.Where(a => a.Status == status); } } if (!string.IsNullOrEmpty(resourceParameters.SearchQuery)) { var searchQueryForWhereClause = resourceParameters.SearchQuery.Trim().ToLowerInvariant(); dataflowEntities = dataflowEntities .Where(a => a.Name.ToLowerInvariant().Contains(searchQueryForWhereClause) || a.Tag.ToLowerInvariant().Contains(searchQueryForWhereClause)); } var pagedListEntities = await PagedList <Dataflow> .CreateAsync(dataflowEntities, resourceParameters.PageNumber, resourceParameters.PageSize, ct); var result = _mapper.Map <PagedList <DataflowDto> >(pagedListEntities); result.TotalCount = pagedListEntities.TotalCount; result.TotalPages = pagedListEntities.TotalPages; result.CurrentPage = pagedListEntities.CurrentPage; result.PageSize = pagedListEntities.PageSize; return(result); }
private string CreateDataflowsResourceUri(DataflowResourceParameters pagedResourceParameters, Enums.ResourceUriType type) { switch (type) { case Enums.ResourceUriType.PreviousPage: return(_linkGenerator.GetUriByName(this.HttpContext, "GetDataflows", new { status = pagedResourceParameters.Status, searchQuery = pagedResourceParameters.SearchQuery, pageNumber = pagedResourceParameters.PageNumber - 1, pageSize = pagedResourceParameters.PageSize })); // TODO get the aboslute path case Enums.ResourceUriType.NextPage: return(_linkGenerator.GetUriByName(this.HttpContext, "GetDataflows", new { status = pagedResourceParameters.Status, searchQuery = pagedResourceParameters.SearchQuery, pageNumber = pagedResourceParameters.PageNumber + 1, pageSize = pagedResourceParameters.PageSize })); default: return(_linkGenerator.GetUriByName(this.HttpContext, "GetDataflows", new { status = pagedResourceParameters.Status, searchQuery = pagedResourceParameters.SearchQuery, pageNumber = pagedResourceParameters.PageNumber, pageSize = pagedResourceParameters.PageSize })); } }