public PagedList <CardType> GetCardTypes(CardTypeResourceParameters cardTypeResourseParameters) { if (cardTypeResourseParameters == null) { throw new ArgumentNullException(nameof(cardTypeResourseParameters)); } var collection = _context.Company as IQueryable <CardType>; if (!string.IsNullOrWhiteSpace(cardTypeResourseParameters.SearchQuery)) { var searchQuery = cardTypeResourseParameters.SearchQuery.Trim(); collection = collection.Where(a => a.Name.Contains(searchQuery)); } if (!string.IsNullOrWhiteSpace(cardTypeResourseParameters.OrderBy)) { var cardTypePropertyMappingDictionary = _propertyMappingService.GetCardTypePropertyMapping <CardTypeDto, CardType>(); collection = collection.ApplySort(cardTypeResourseParameters.OrderBy, cardTypePropertyMappingDictionary); } //Paging.... happens LAST return(PagedList <CardType> .Create(collection, cardTypeResourseParameters.PageNumber, cardTypeResourseParameters.PageSize)); }
private IEnumerable <LinkDto> CreateLinksForCardType( CardTypeResourceParameters cardTypeResourceParameters, bool hasNext, bool hasPrevious) { var links = new List <LinkDto>(); // self links.Add( new LinkDto(CreateCardTypeResourceUri( cardTypeResourceParameters, ResourceUriType.Current) , "self", "GET")); if (hasNext) { links.Add( new LinkDto(CreateCardTypeResourceUri( cardTypeResourceParameters, ResourceUriType.NextPage), "nextPage", "GET")); } if (hasPrevious) { links.Add( new LinkDto(CreateCardTypeResourceUri( cardTypeResourceParameters, ResourceUriType.PreviousPage), "previousPage", "GET")); } return(links); }
public IActionResult GetCardTypes( [FromQuery] CardTypeResourceParameters cardTypeResourceParameters) { if (!_propertyMappingService.ValidCardTypeMappingExistsFor <CardTypeDto, Entities.CardType> (cardTypeResourceParameters.OrderBy)) { return(BadRequest()); } var cardTypeFromRepo = _cardTypeRepository.GetCardTypes(cardTypeResourceParameters); var paginationMetadata = new { totalCount = cardTypeFromRepo.TotalCount, pageSize = cardTypeFromRepo.PageSize, currentPage = cardTypeFromRepo.CurrentPage, totalPages = cardTypeFromRepo.TotalPages }; Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(paginationMetadata)); var links = CreateLinksForCardType(cardTypeResourceParameters, cardTypeFromRepo.HasNext, cardTypeFromRepo.HasPrevious); var shapedCompanies = _mapper.Map <IEnumerable <CardTypeDto> >(cardTypeFromRepo) .ShapeData(cardTypeResourceParameters.Fields); var shapedCompaniesWithLinks = shapedCompanies.Select(cardType => { var cardTypeAsDictionary = cardType as IDictionary <string, object>; var cardTypeLinks = CreateLinksForCardType((string)cardTypeAsDictionary["Id"], null); cardTypeAsDictionary.Add("links", cardTypeLinks); return(cardTypeAsDictionary); }); var linkedCollectionResource = new { value = shapedCompaniesWithLinks, links }; return(Ok(linkedCollectionResource)); }
private string CreateCardTypeResourceUri( CardTypeResourceParameters cardTypeResourceParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(Url.Link("GetCardTypes", new { fields = cardTypeResourceParameters.Fields, orderBy = cardTypeResourceParameters.OrderBy, pageNumber = cardTypeResourceParameters.PageNumber - 1, pageSize = cardTypeResourceParameters.PageSize, searchQuery = cardTypeResourceParameters.SearchQuery })); case ResourceUriType.NextPage: return(Url.Link("GetCardTypes", new { fields = cardTypeResourceParameters.Fields, orderBy = cardTypeResourceParameters.OrderBy, pageNumber = cardTypeResourceParameters.PageNumber + 1, pageSize = cardTypeResourceParameters.PageSize, searchQuery = cardTypeResourceParameters.SearchQuery })); case ResourceUriType.Current: default: return(Url.Link("GetCardTypes", new { fields = cardTypeResourceParameters.Fields, orderBy = cardTypeResourceParameters.OrderBy, pageNumber = cardTypeResourceParameters.PageNumber, pageSize = cardTypeResourceParameters.PageSize, searchQuery = cardTypeResourceParameters.SearchQuery })); } }