public IActionResult GetCompanies(CompaniesQueryParameters companiesQueryParameters) { PagedList <Company> pagedList = _companiesRepository.GetCompanies(companiesQueryParameters); var previousPageLink = pagedList.HasPrevious ? CreateCompaniesResourceUri(companiesQueryParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = pagedList.HasNext ? CreateCompaniesResourceUri(companiesQueryParameters, ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = pagedList.TotalCount, pageSize = pagedList.PageSize, currentPage = pagedList.CurrentPage, totalPages = pagedList.TotalPages, previousPageLink = previousPageLink, nextPageLink = nextPageLink }; Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata)); var response = new { metadata = paginationMetadata, data = pagedList.ToMappedPagedList <Company, CompanyDto>() }; return(Ok(response)); }
private string CreateCompaniesResourceUri(CompaniesQueryParameters companiesQueryParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(_urlHelper.Link("GetCompanies", new { pageNumber = companiesQueryParameters.PageNumber - 1, pageSize = companiesQueryParameters.PageSize, name = companiesQueryParameters.Name, nip = companiesQueryParameters.Nip, voivodeship = companiesQueryParameters.Voivodeship, city = companiesQueryParameters.City })); case ResourceUriType.NextPage: return(_urlHelper.Link("GetCompanies", new { pageNumber = companiesQueryParameters.PageNumber + 1, pageSize = companiesQueryParameters.PageSize, name = companiesQueryParameters.Name, nip = companiesQueryParameters.Nip, voivodeship = companiesQueryParameters.Voivodeship, city = companiesQueryParameters.City })); default: return(_urlHelper.Link("GetCompanies", new { pageNumber = companiesQueryParameters.PageNumber, pageSize = companiesQueryParameters.PageSize, name = companiesQueryParameters.Name, nip = companiesQueryParameters.Nip, voivodeship = companiesQueryParameters.Voivodeship, city = companiesQueryParameters.City })); } }
public PagedList <Company> GetCompanies(CompaniesQueryParameters companiesQueryParameters) { var collectionBeforePaging = _dbContext.Companies.OrderBy(x => x.Name).AsQueryable(); if (!string.IsNullOrEmpty(companiesQueryParameters.Name)) { string nameForWhereClause = companiesQueryParameters.Name.Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging .Where(x => x.Name.ToLowerInvariant().Contains(nameForWhereClause)); } if (!string.IsNullOrEmpty(companiesQueryParameters.Nip)) { string nipForWhereClause = companiesQueryParameters.Nip.Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging .Where(x => x.Nip != null && x.Nip.ToLowerInvariant().Contains(nipForWhereClause)); } if (!string.IsNullOrEmpty(companiesQueryParameters.Voivodeship)) { string voivodeshipForWhereClause = companiesQueryParameters.Voivodeship.Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging .Where(x => x.Voivodeship != null && x.Voivodeship.ToLowerInvariant().Contains(voivodeshipForWhereClause)); } if (!string.IsNullOrEmpty(companiesQueryParameters.City)) { string cityForWhereClause = companiesQueryParameters.City.Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging .Where(x => x.City != null && x.City.ToLowerInvariant().Contains(cityForWhereClause)); } PagedList <Company> pagedList = PagedList <Company> .Create(collectionBeforePaging, companiesQueryParameters.PageNumber, companiesQueryParameters.PageSize); return(pagedList); }