Exemplo n.º 1
0
        public ExpandoObject ShapeAndAddLinkToObject <T>(T obj, string resourceName, string fields)
        {
            var shapedObj       = obj.ShapeData(fields);
            var shapedObjAsDict = shapedObj as IDictionary <string, object>;
            var linksForObj     = _hateoasHelper.CreateLinksForResource((Guid)shapedObjAsDict["Id"], fields, resourceName);

            ((IDictionary <string, object>)shapedObj).Add("links", linksForObj);
            return(shapedObj);
        }
Exemplo n.º 2
0
        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));
            }
        }