Ejemplo n.º 1
0
        public ExpandoObject AddLinksToCollection(IEnumerable <IDictionary <string, object> > collection, string resourceName, object value)
        {
            var linkedCollection = new ExpandoObject();
            var links            = _hateoasHelper.CreateLinksForChildResources(resourceName, value);

            ((IDictionary <string, object>)linkedCollection).Add("records", collection);
            ((IDictionary <string, object>)linkedCollection).Add("links", links);
            return(linkedCollection);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetAlbumsWithHateoas([FromRoute] Guid artistId, [FromQuery] string fields)
        {
            if (!_typeHelperService.TypeHasProperties <AlbumDto>(fields))
            {
                return(BadRequest());
            }

            var albumEntities = await _albumRepository.GetAlbumsForArtistAsync(artistId);

            var albums = Mapper.Map <IEnumerable <AlbumDto> >(albumEntities);

            var shapedAlbums = albums.Select(album =>
            {
                return(_controllerHelper.ShapeAndAddLinkToObject(album, "Album", fields));
            });

            var resourceLinks = _hateoasHelper.CreateLinksForChildResources("Album", new { artistId });

            var linkedResourceCollection = new ExpandoObject();

            ((IDictionary <string, object>)linkedResourceCollection).Add("records", shapedAlbums);
            ((IDictionary <string, object>)linkedResourceCollection).Add("links", resourceLinks);
            return(Ok(linkedResourceCollection));
        }