コード例 #1
0
        public IActionResult BlockAuthorAdd(int id)
        {
            if (Respository.AuthorExists(id))
            {
                return(new StatusCodeResult(StatusCodes.Status409Conflict));
            }

            return(NotFound());
        }
コード例 #2
0
        public IActionResult GetBooksForAuthor(int authorId)
        {
            if (!Respository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var booksFromRepo = Respository.GetBookList(authorId);

            var booksForAuthor = Mapper.Map <IEnumerable <BookDto> >(booksFromRepo);

            return(Ok(booksForAuthor));
        }
コード例 #3
0
        public IActionResult GetBookForAuthor(int authorId, int id)
        {
            if (!Respository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var book = Respository.GetBook(authorId, id);

            if (book == null)
            {
                return(NotFound());  // 404
            }
            var bookDto = Mapper.Map <BookDto>(book);

            return(Ok(bookDto));
        }
コード例 #4
0
        public IActionResult CreateBookForAuthor(int authorId, [FromBody] BookForAddDto bookForAdd)  // FromBody signals that it should be deserialized from the request body
        {
            if (bookForAdd == null)
            {
                return(BadRequest());
            }

            if (!Respository.AuthorExists(authorId))
            {
                return(NotFound());
            }

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

            Respository.AddBookForAuthor(authorId, bookEntity);

            if (!Respository.Save())
            {
                throw new Exception("Failed to create the book.");  // Throw exception so the middleware handler does all of the error handling.
            }
            var bookToReturn = Mapper.Map <BookDto>(bookEntity);

            return(CreatedAtRoute("GetBookForAuthor", new { authorId, id = bookToReturn.Id }, bookToReturn));   // Puts Location http://localhost:5000/api/Authors/53 in header
        }