コード例 #1
0
        public IActionResult UpdateBookForAuthor(Guid authorId, Guid bookId, BookForUpdateDto bookForUpdateDto)
        {
            if (!_bookLibraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var bookFromRepo = _bookLibraryRepository.GetBook(authorId, bookId);

            if (bookFromRepo == null)
            {
                var bookEntity = _mapper.Map <Book>(bookForUpdateDto);
                bookEntity.Id = bookId;
                _bookLibraryRepository.AddBook(authorId, bookEntity);
                _bookLibraryRepository.Save();


                var bookToReturn = _mapper.Map <BookDto>(bookEntity);
                return(CreatedAtRoute("GetBookForAuthor", new { authorId, bookId = bookToReturn.Id },
                                      bookToReturn));
            }

            _mapper.Map(bookForUpdateDto, bookFromRepo);
            _bookLibraryRepository.UpdateBook(bookFromRepo);
            _bookLibraryRepository.Save();

            return(NoContent());
        }