private string CreateProductsResourceUri(OrderDtoParameter parameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(Url.Link(nameof(GetOrders), new { UserID = parameters.UserID, PageNumber = parameters.PageNumber - 1, PageSize = parameters.PageSize })); case ResourceUriType.NextPage: return(Url.Link(nameof(GetOrders), new { UserID = parameters.UserID, PageNumber = parameters.PageNumber + 1, PageSize = parameters.PageSize })); default: return(Url.Link(nameof(GetOrders), new { UserID = parameters.UserID, PageNumber = parameters.PageNumber, PageSize = parameters.PageSize })); } }
public async Task <ActionResult <IEnumerable <OrderDto> > > GetOrders([FromQuery] OrderDtoParameter parameter) { var role = User.FindFirst(ClaimTypes.Role)?.Value; if (role != "Admin") { parameter.UserID = new Guid(User.FindFirst(ClaimTypes.NameIdentifier)?.Value); } var queryable = orderRepository.GetQueryableOrder() .Include(o => o.OrderItems) .Where(o => !o.IsDeleted); if (parameter.UserID != null) { queryable = queryable.Where(o => o.UserID.Equals(parameter.UserID)); } if (parameter.Status != null) { queryable = queryable.Where(o => o.Status.Equals(parameter.Status)); } queryable = queryable.OrderByDescending(o => o.OrderTime); var pagedOrders = await PagedList <Order> .Create(queryable, parameter.PageNumber, parameter.PageSize); var previousPageLink = pagedOrders.HasPrevious ? CreateProductsResourceUri(parameter, ResourceUriType.PreviousPage) : null; var nextPageLink = pagedOrders.HasNext ? CreateProductsResourceUri(parameter, ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = pagedOrders.TotalCount, pageSize = pagedOrders.PageSize, currentPage = pagedOrders.CurrentPage, totalPages = pagedOrders.TotalPages, previousPageLink, nextPageLink }; Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(paginationMetadata, new JsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping })); var orderDtos = mapper.Map <IEnumerable <OrderDto> >(pagedOrders); return(Ok(orderDtos)); }