Esempio n. 1
0
        public IActionResult GetAuthor(Guid authorId, string fields,
                                       [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!MediaTypeHeaderValue.TryParse(mediaType,
                                               out MediaTypeHeaderValue parsedMediaType))
            {
                return(BadRequest());
            }

            if (!_propertycheckerService.TypeHasProperties <AuthorDto>(fields))
            {
                return(BadRequest());
            }

            var author = _bookLibraryRepository.GetAuthor(authorId);

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

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

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

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

            var primaryMediaType = includeLinks ?
                                   parsedMediaType.SubTypeWithoutSuffix
                                   .Substring(0, parsedMediaType.SubTypeWithoutSuffix.Length - 8)
                : parsedMediaType.SubTypeWithoutSuffix;

            if (primaryMediaType == "vnd.marvin.author.full")
            {
                var fullResourceToReturn = _mapper.Map <AuthorFullDto>(author)
                                           .ShapeData(fields) as IDictionary <string, object>;

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

                return(Ok(fullResourceToReturn));
            }

            var friendlyResourceToReturn = _mapper.Map <AuthorDto>(author)
                                           .ShapeData(fields) as IDictionary <string, object>;

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

            return(Ok(friendlyResourceToReturn));
        }
Esempio n. 2
0
        public IActionResult GetAuthor(int authorid, bool includeBook = false)
        {
            var author = _bookLibraryRepository.GetAuthor(authorid, includeBook);

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

            if (includeBook)
            {
                var authorResult = Mapper.Map <IEnumerable <AuthorDto> >(author);

                return(Ok(authorResult));
            }

            var authorWithoutBookResult = Mapper.Map <AuthorWithoutBookDto>(author);

            return(Ok(authorWithoutBookResult));
        }