public IActionResult GetSupermarkets(SupermarketResourceParameters supermarketResourceParameters, [FromHeader(Name = "Accept")] string mediaType) { // check mappings are valid if (!_propertyMappingService.ValidMappingExistsFor <SupermarketDTO, Supermarket> (supermarketResourceParameters.OrderBy)) { return(BadRequest()); } // check all fields are valid if (!_typeHelperService.TypeHasProperties <SupermarketDTO> (supermarketResourceParameters.Fields)) { return(BadRequest()); } var supermarketsFromRepo = _supermarketRepository.GetAllSupermarkets(supermarketResourceParameters); var supermarkets = Mapper.Map <IEnumerable <SupermarketDTO> >(supermarketsFromRepo); if (mediaType == "application/vnd.idp.hateoas+json") { // Pagination var paginationMetadata = new { totalCount = supermarketsFromRepo.TotalCount, pageSize = supermarketsFromRepo.PageSize, currentPage = supermarketsFromRepo.CurrentPage, totalPages = supermarketsFromRepo.TotalPages }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); var links = CreateLinksForSupermarkets(supermarketResourceParameters, supermarketsFromRepo.HasNext, supermarketsFromRepo.HasPrevious); var shapedSupermarkets = supermarkets.ShapeData(supermarketResourceParameters.Fields); var shapedSupermarketsWithLinks = shapedSupermarkets.Select(supermarket => { var supermarketAsDictionary = supermarket as IDictionary <string, object>; var supermarketLinks = CreateLinksForSupermarket( (int)supermarketAsDictionary["SupermarketId"], supermarketResourceParameters.Fields); supermarketAsDictionary.Add("links", supermarketLinks); return(supermarketAsDictionary); }); var linkedCollectionResource = new { value = shapedSupermarketsWithLinks, links = links }; return(Ok(linkedCollectionResource)); } else { var previousPageLink = supermarketsFromRepo.HasPrevious ? CreateSupermarketResourceUri(supermarketResourceParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = supermarketsFromRepo.HasNext ? CreateSupermarketResourceUri(supermarketResourceParameters, ResourceUriType.NextPage) : null; var paginationMetadata = new { previousPageLink, nextPageLink, totalCount = supermarketsFromRepo.TotalCount, pageSize = supermarketsFromRepo.PageSize, currentPage = supermarketsFromRepo.CurrentPage, totalPages = supermarketsFromRepo.TotalPages }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); return(Ok(supermarkets.ShapeData(supermarketResourceParameters.Fields))); } }