private IEnumerable <LinkDTO> CreateLinksForSupermarketStocks(StockResourceParameters stockResourceParameters, bool hasNext, bool hasPrevious) { var links = new List <LinkDTO>(); // self links.Add( new LinkDTO(CreateStockResourceUri(stockResourceParameters, ResourceUriType.Current) , "self", "GET")); if (hasNext) { links.Add( new LinkDTO(CreateStockResourceUri(stockResourceParameters, ResourceUriType.NextPage), "nextPage", "GET")); } if (hasPrevious) { links.Add( new LinkDTO(CreateStockResourceUri(stockResourceParameters, ResourceUriType.PreviousPage), "previousPage", "GET")); } return(links); }
public PagedList <SupermarketStock> GetAllStock(StockResourceParameters stockResourceParameters) { var collectionBeforePaging = _context.SupermarketStocks.ApplySort(stockResourceParameters.OrderBy, _stockPropertyMappingService.GetPropertyMapping <SupermarketStockDTO, SupermarketStock>()); return(PagedList <SupermarketStock> .Create(collectionBeforePaging, stockResourceParameters.PageNumber, stockResourceParameters.PageSize)); }
private string CreateStockResourceUri( StockResourceParameters stockResourceParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(_urlHelper.Link("GetAllStock", new { fields = stockResourceParameters.Fields, orderBy = stockResourceParameters.OrderBy, pageNumber = stockResourceParameters.PageNumber - 1, pageSize = stockResourceParameters.PageSize })); case ResourceUriType.NextPage: return(_urlHelper.Link("GetAllStock", new { fields = stockResourceParameters.Fields, orderBy = stockResourceParameters.OrderBy, pageNumber = stockResourceParameters.PageNumber + 1, pageSize = stockResourceParameters.PageSize })); default: return(_urlHelper.Link("GetAllStock", new { fields = stockResourceParameters.Fields, orderBy = stockResourceParameters.OrderBy, pageNumber = stockResourceParameters.PageNumber, pageSize = stockResourceParameters.PageSize })); } }
public IActionResult GetAllStock(StockResourceParameters stockResourceParameters, [FromHeader(Name = "Accept")] string mediaType) { // check mappings are valid if (!_stockMappingService.ValidMappingExistsFor <SupermarketStockDTO, SupermarketStock> (stockResourceParameters.OrderBy)) { return(BadRequest()); } // check all fields are valid if (!_typeHelperService.TypeHasProperties <SupermarketStockDTO> (stockResourceParameters.Fields)) { return(BadRequest()); } // get all stock from repo var stockFromRepo = _supermarketRepository.GetAllStock(stockResourceParameters); var stock = Mapper.Map <IEnumerable <SupermarketStockDTO> >(stockFromRepo); if (mediaType == "application/vnd.idp.hateoas+json") { var paginationMetadata = new { totalCount = stockFromRepo.TotalCount, pageSize = stockFromRepo.PageSize, currentPage = stockFromRepo.CurrentPage, totalPages = stockFromRepo.TotalPages, }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); var links = CreateLinksForSupermarketStocks(stockResourceParameters, stockFromRepo.HasNext, stockFromRepo.HasPrevious); var shapedStocks = stock.ShapeData(stockResourceParameters.Fields); var shapedStocksWithLinks = shapedStocks.Select(stocks => { var stockAsDictionary = stocks as IDictionary <string, object>; var stockLinks = CreateLinksForSupermarketStock( (int)stockAsDictionary["Id"], stockResourceParameters.Fields); stockAsDictionary.Add("links", stockLinks); return(stockAsDictionary); }); var linkedCollectionResource = new { value = shapedStocksWithLinks, links = links }; return(Ok(linkedCollectionResource)); } else { var previousPageLink = stockFromRepo.HasPrevious ? CreateProductResourceUri(stockResourceParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = stockFromRepo.HasNext ? CreateProductResourceUri(stockResourceParameters, ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = stockFromRepo.TotalCount, pageSize = stockFromRepo.PageSize, currentPage = stockFromRepo.CurrentPage, totalPages = stockFromRepo.TotalPages, previousPageLink, nextPageLink }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); return(Ok(stock.ShapeData(stockResourceParameters.Fields))); } }