public IActionResult GetCountries([FromBody] CountryQueryFilter filters) { if (!User.Identity.IsAuthenticated) { throw new AuthenticationException(); } var countries = _countryService.GetCountries(filters); var countriesDto = _mapper.Map <IEnumerable <CountryDto> >(countries); var metadata = new Metadata { TotalCount = countries.TotalCount, PageSize = countries.PageSize, CurrentPage = countries.CurrentPage, TotalPages = countries.TotalPages, HasNextPage = countries.HasNextPage, HasPreviousPage = countries.HasPreviousPage, NextPageUrl = _uriService.GetPostPaginationUri(filters, Url.RouteUrl(nameof(GetCountries))).ToString(), PreviousPageUrl = _uriService.GetPostPaginationUri(filters, Url.RouteUrl(nameof(GetCountries))).ToString() }; var response = new ApiResponse <IEnumerable <CountryDto> >(countriesDto) { Meta = metadata }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(metadata)); return(Ok(response)); }
public PagedList <Country> GetCountries(CountryQueryFilter filters) { filters.PageNumber = filters.PageNumber == 0 ? _paginationOptions.DefaultPageNumber : filters.PageNumber; filters.PageSize = filters.PageSize == 0 ? _paginationOptions.DefaultPageSize : filters.PageSize; var countries = _unitOfWork.CountryRepository.GetAll(); if (filters.filter != null) { countries = countries.Where(x => x.name.ToLower().Contains(filters.filter.ToLower())); } if (filters.Name != null) { countries = countries.Where(x => x.name.ToLower().Contains(filters.Name.ToLower())); } if (filters.orderedBy != null && filters.orderedBy.Count() > 0) { foreach (var sortM in filters.orderedBy) { countries = countries.OrderBy(sortM.PairAsSqlExpression); } } var pagedPosts = PagedList <Country> .Create(countries, filters.PageNumber, filters.PageSize); return(pagedPosts); }
public Uri GetPostPaginationUri(CountryQueryFilter filter, string actionUrl) { string baseUrl = $"{_baseUri}{actionUrl}"; return(new Uri(baseUrl)); }