public ExpandoObject AddLinksToCollection(IEnumerable <IDictionary <string, object> > collection, RequestParameters requestParameters, bool hasNext, bool hasPrevious, string resourceName) { var linkedCollection = new ExpandoObject(); var links = _hateoasHelper.CreateLinksForResources(requestParameters, hasNext, hasPrevious, resourceName); ((IDictionary <string, object>)linkedCollection).Add("records", collection); ((IDictionary <string, object>)linkedCollection).Add("links", links); return(linkedCollection); }
public async Task <IActionResult> GetArtistsWithHateoas([FromQuery] RequestParameters requestParameters) { if (string.IsNullOrWhiteSpace(requestParameters.OrderBy)) { requestParameters.OrderBy = "StageName"; } if (!_typeHelperService.TypeHasProperties <ArtistDto>(requestParameters.Fields)) { return(BadRequest()); } var artistPagedList = await GetPagedListOfArtists(requestParameters); var paginationMetaData = _controllerHelper.CreatePaginationMetadataObject(artistPagedList, requestParameters, "GetArtists"); Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetaData)); var artists = Mapper.Map <IEnumerable <ArtistDto> >(artistPagedList); var shapedArtists = artists.ShapeData(requestParameters.Fields); var shapedArtistsWithLinks = shapedArtists.Select(artist => { var artistDict = artist as IDictionary <string, object>; var linksForArtist = _hateoasHelper.CreateLinksForResource((Guid)artistDict["Id"], requestParameters.Fields, "Artist"); artistDict.Add("links", linksForArtist); return(artistDict); }); var links = _hateoasHelper.CreateLinksForResources(requestParameters, artistPagedList.HasNext, artistPagedList.HasPrevious, "Artist"); if (requestParameters.IncludeMetadata) { var linkedArtistsWithMetadata = _controllerHelper.CreateLinkedentityWithmetadataObject(paginationMetaData, shapedArtistsWithLinks, links); return(Ok(linkedArtistsWithMetadata)); } else { var linkedResourceCollection = new ExpandoObject(); ((IDictionary <string, object>)linkedResourceCollection).Add("records", shapedArtistsWithLinks); ((IDictionary <string, object>)linkedResourceCollection).Add("links", links); return(Ok(linkedResourceCollection)); } }