public IActionResult CreateAuthor([FromBody] AuthorCreationDto author)
        {
            if (author == null || !ModelState.IsValid)
            {
                return(BadRequest());
            }

            var authorEntity = Mapper.Map <Author>(author);

            _libraryRepository.AddAuthor(authorEntity);
            if (!_libraryRepository.Save())
            {
                throw new Exception("Creating an author failed on save");
                //return StatusCode(500, "A problem happened with handling your request");
            }

            var authorToReturn = Mapper.Map <AuthorDto>(authorEntity);

            var links = CreateLinksForAuthor(authorToReturn.Id, null);

            var linkedResourceToReturn = authorToReturn.ShapeData(null)
                                         as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetAuthor", new { id = linkedResourceToReturn["Id"] }, linkedResourceToReturn));
        }
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

            if (book.Description == book.Title)
            {
                ModelState.AddModelError(nameof(BookCreationDto), "The provided desciption should be different from the title");
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var bookEntity = Mapper.Map <Book>(book);

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);
            if (!_libraryRepository.Save())
            {
                throw new BadImageFormatException($"Creating a book for {authorId} failed on save");
            }

            var bookToReturn = Mapper.Map <BookDto>(bookEntity);

            return(CreatedAtRoute("GetBookForAuthor", new { authorId, id = bookToReturn.Id }, CreateLinksForBook(bookToReturn)));
        }
        public IActionResult CreateAuthorCollection([FromBody] IEnumerable <AuthorCreationDto> authorCollection)
        {
            if (authorCollection == null)
            {
                return(BadRequest());
            }

            var authorEntities = Mapper.Map <IEnumerable <Author> >(authorCollection);

            foreach (var author in authorEntities)
            {
                libraryRepository.AddAuthor(author);
            }

            if (!libraryRepository.Save())
            {
                throw new Exception("Creation of author collection failed");
            }
            var authorCollectionToReturn = Mapper.Map <IEnumerable <AuthorDto> >(authorEntities);
            var idsAsString = string.Join(",", authorCollectionToReturn.Select(a => a.Id));

            return(CreatedAtRoute("GetAuthorCollection", new { ids = idsAsString }, authorCollectionToReturn));
        }