public ActionResult <IEnumerable <Product> > GetAllProducts([FromQuery] Criterion criterion) { var products = ProductStub(); var numberOfProducts = products.Count(); var paginationMetadata = new { totalCount = numberOfProducts, pageSize = criterion.PageSize, currentPage = criterion.CurrentPage, totalPages = criterion.GetTotalPages(numberOfProducts) }; Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata)); var links = CreateLinksForCollection(criterion, numberOfProducts); var toReturn = products.Select(x => ExpandSingleFoodItem(x)); return(Ok(new { value = toReturn, links })); }
public static void AddPaginateInfo(this HttpResponse httpResponse, Criterion criterion, int numberOfItems) { var paginationMetadata = new { totalCount = numberOfItems, pageSize = criterion.PageSize, currentPage = criterion.CurrentPage, totalPages = criterion.GetTotalPages(numberOfItems) }; httpResponse.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); }
public static List <LinkItem> CreateLinksForCollection(this IUrlHelper urlHelper, string methodName, Criterion criterion, int totalCount) { var links = new List <LinkItem>(); // self links.Add( new LinkItem(urlHelper.Link(methodName, new { pagecount = criterion.PageSize, page = criterion.CurrentPage, orderby = criterion.SortBy }), "self", "GET")); links.Add(new LinkItem(urlHelper.Link(methodName, new { pagecount = criterion.PageSize, page = 1, orderby = criterion.SortBy }), "first", "GET")); links.Add(new LinkItem(urlHelper.Link(methodName, new { pagecount = criterion.PageSize, page = criterion.GetTotalPages(totalCount), orderby = criterion.SortBy }), "last", "GET")); if (criterion.HasNext(totalCount)) { links.Add(new LinkItem(urlHelper.Link(methodName, new { pagecount = criterion.PageSize, page = criterion.CurrentPage + 1, orderby = criterion.SortBy }), "next", "GET")); } if (criterion.HasPrevious()) { links.Add(new LinkItem(urlHelper.Link(methodName, new { pagecount = criterion.PageSize, page = criterion.CurrentPage - 1, orderby = criterion.SortBy }), "previous", "GET")); } return(links); }
private List <LinkItem> CreateLinksForCollection(Criterion criterion, int totalCount) { var links = new List <LinkItem>(); // self links.Add( new LinkItem(_urlHelper.Link(nameof(GetAllProducts), new { pagecount = criterion.PageSize, page = criterion.CurrentPage, orderby = criterion.SortBy }), "self", "GET")); links.Add(new LinkItem(_urlHelper.Link(nameof(GetAllProducts), new { pagecount = criterion.PageSize, page = 1, orderby = criterion.SortBy }), "first", "GET")); links.Add(new LinkItem(_urlHelper.Link(nameof(GetAllProducts), new { pagecount = criterion.PageSize, page = criterion.GetTotalPages(totalCount), orderby = criterion.SortBy }), "last", "GET")); if (criterion.HasNext(totalCount)) { links.Add(new LinkItem(_urlHelper.Link(nameof(GetAllProducts), new { pagecount = criterion.PageSize, page = criterion.CurrentPage + 1, orderby = criterion.SortBy }), "next", "GET")); } if (criterion.HasPrevious()) { links.Add(new LinkItem(_urlHelper.Link(nameof(GetAllProducts), new { pagecount = criterion.PageSize, page = criterion.CurrentPage - 1, orderby = criterion.SortBy }), "previous", "GET")); } return(links); }