public async Task <IActionResult> GetAll([FromQuery] CustomerPaginationFilter filter) { var route = Request.Path.Value; var validFilter = new CustomerPaginationFilter(filter.PageNumber, filter.PageSize); var query = context.Customers.AsQueryable(); //Search by firstname and email if (!string.IsNullOrEmpty(filter.FirstName)) { query = query.Where(x => x.FirstName.ToLower().Contains(filter.FirstName.ToLower())); } if (!string.IsNullOrEmpty(filter.Email)) { query = query.Where(x => x.Email.ToLower().Contains(filter.Email.ToLower())); } var pagedData = await query.Skip((validFilter.PageNumber - 1) *validFilter.PageSize) .Take(validFilter.PageSize).ToListAsync(); // calculate total records var totalRecords = await query.CountAsync(); var pagedReponse = PaginationHelper.CreatePagedReponse(pagedData, validFilter, totalRecords, uriService, route); return(Ok(pagedReponse)); }
public Uri GetPageUri(CustomerPaginationFilter filter, string route) { var _enpointUri = new Uri(string.Concat(_baseUri, route)); var modifiedUri = QueryHelpers.AddQueryString(_enpointUri.ToString(), "pageNumber", filter.PageNumber.ToString()); modifiedUri = QueryHelpers.AddQueryString(modifiedUri, "pageSize", filter.PageSize.ToString()); return(new Uri(modifiedUri)); }
public static PagedResponse <List <T> > CreatePagedReponse <T>(List <T> pagedData, CustomerPaginationFilter validFilter, int totalRecords, IUriService uriService, string route) { var respose = new PagedResponse <List <T> >(pagedData, validFilter.PageNumber, validFilter.PageSize); var totalPages = ((double)totalRecords / (double)validFilter.PageSize); int roundedTotalPages = Convert.ToInt32(Math.Ceiling(totalPages)); respose.NextPage = validFilter.PageNumber >= 1 && validFilter.PageNumber < roundedTotalPages ? uriService.GetPageUri(new CustomerPaginationFilter(validFilter.PageNumber + 1, validFilter.PageSize), route) : null; respose.PreviousPage = validFilter.PageNumber - 1 >= 1 && validFilter.PageNumber <= roundedTotalPages ? uriService.GetPageUri(new CustomerPaginationFilter(validFilter.PageNumber - 1, validFilter.PageSize), route) : null; respose.FirstPage = uriService.GetPageUri(new CustomerPaginationFilter(1, validFilter.PageSize), route); respose.LastPage = uriService.GetPageUri(new CustomerPaginationFilter(roundedTotalPages, validFilter.PageSize), route); respose.TotalPages = roundedTotalPages; respose.TotalRecords = totalRecords; return(respose); }