Exemple #1
0
        public IActionResult GetById(Guid authorId, [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!MediaTypeHeaderValue.TryParse(mediaType, out MediaTypeHeaderValue parseMediaType))
            {
                return(BadRequest());
            }

            var authorFromRepo = _restApiService.GetAuthor(authorId);

            if (authorFromRepo == null)
            {
                return(NotFound());
            }

            var includeLinks = parseMediaType.SubTypeWithoutSuffix.EndsWith("hateoas", StringComparison.InvariantCultureIgnoreCase);

            IEnumerable <LinkDto> links = new List <LinkDto>();

            if (includeLinks)
            {
                links = CreateLinksForAuthor(authorId);
            }


            var authorToReturn = _mapper.Map <AuthorDto>(authorFromRepo).ShapeData() as IDictionary <string, object>;


            if (includeLinks)
            {
                authorToReturn.Add("links", links);
            }

            return(Ok(authorToReturn));
        }