//todo ////////////////////////////////////////////////////////////////

        //todo *************** Create Links for a book ************************
        private BookDto CreateLinksForBook(BookDto book)
        {
            book.Links.Add(new LinkDto(_urlHelper.Link("GetBookForAuthor",
                                                       new { id = book.Id }),
                                       "self", "GET"));
            book.Links.Add(new LinkDto(_urlHelper.Link("DeleteBookForAuthor",
                                                       new { id = book.Id }),
                                       "delete_book", "DELETE"));
            book.Links.Add(new LinkDto(_urlHelper.Link("UpdateBookForAuthor",
                                                       new { id = book.Id }),
                                       "update_book", "PUT"));
            book.Links.Add(new LinkDto(_urlHelper.Link("PartiallyUpdateBookForAuthor",
                                                       new { id = book.Id }),
                                       "partially_update_book", "PATCH"));
            return(book);
        }
コード例 #2
0
        private BookDto CreateBookLinks(BookDto bookDto)
        {
            bookDto.Links.Add(new LinkDto(UrlHelper.Link(
                                              "GetBook", new { id = bookDto.Id }), "self", "GET"));

            bookDto.Links.Add(new LinkDto(UrlHelper.Link(
                                              "DeleteBook", new { id = bookDto.Id }), "delete_book", "DELETE"));

            bookDto.Links.Add(new LinkDto(UrlHelper.Link(
                                              "UpdateBook", new { id = bookDto.Id }), "update_book", "PUT"));

            bookDto.Links.Add(new LinkDto(UrlHelper.Link(
                                              "PartiallyUpdateBook", new { id = bookDto.Id }), "partially_update_book", "PATCH"));

            return(bookDto);
        }
コード例 #3
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDto bookDto)
        {
            if (bookDto == null)
            {
                return(BadRequest());
            }

            if (bookDto.Description == bookDto.Title)
            {
                ModelState.AddModelError(nameof(BookForCreationDto), "The provided description should be different from the title.");
            }

            if (!ModelState.IsValid)
            {
                // return 422;
                return(new UnprocessableEntityObjectResult(ModelState));
            }

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

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

            c_libraryRepository.AddBookForAuthor(authorId, bookEntity);

            if (!c_libraryRepository.Save())
            {
                throw new Exception($"Creating a book for author {authorId} failed on save.");
            }

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

            return(CreatedAtRoute(
                       RouteNames.GetBookForAuthor,
                       new
            {
                authorId = authorId,
                id = bookToReturn.Id
            },
                       bookDtoWithLinks));
        }
コード例 #4
0
        public IActionResult AddBook(Guid authorId, BookForCreationDto bookForCreationDto)
        {
            if (!AuthorRepository.IsAuthorExists(authorId))
            {
                return(NotFound());
            }

            var newBook = new BookDto
            {
                Id          = Guid.NewGuid(),
                Title       = bookForCreationDto.Title,
                Description = bookForCreationDto.Description,
                Pages       = bookForCreationDto.Pages,
                AuthorId    = authorId,
            };

            BookRepository.AddBook(newBook);
            return(CreatedAtRoute(nameof(GetBook), new { bookId = newBook.Id }, newBook));
        }
コード例 #5
0
        public IActionResult GetBook(Guid authorId, Guid bookId)
        {
            if (!c_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            Book book = c_libraryRepository.GetBookForAuthor(authorId, bookId);

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

            var     bookDto          = Mapper.Map <BookDto>(book);
            BookDto bookDtoWithLinks = CreateLinksForBook(bookDto);

            return(Ok(bookDtoWithLinks));
        }
コード例 #6
0
        private BookDto CreateLinksForBook(BookDto book)
        {
            book.Links.Add(new Link(HttpMethods.Get,
                                    "self",
                                    Url.Link(nameof(GetBookAsync), new { bookId = book.Id })));

            book.Links.Add(new Link(HttpMethods.Delete,
                                    "delete book",
                                    Url.Link(nameof(DeleteBookAsync), new { bookId = book.Id })));

            book.Links.Add(new Link(HttpMethods.Put,
                                    "update book",
                                    Url.Link(nameof(BookController.UpdateBookAsync), new { bookId = book.Id })));

            book.Links.Add(new Link(HttpMethods.Patch,
                                    "update book partially",
                                    Url.Link(nameof(BookController.PartiallyUpdateBookAsync), new { bookId = book.Id })));

            return(book);
        }
コード例 #7
0
        public IActionResult GetBookForAuthor(Guid authorId, Guid id)
        {
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var book = _libraryRepository.GetBookForAuthor(authorId, id);

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

            var bookDto = new BookDto(book);

            bookDto = CreateLinksForBook(bookDto);

            return(Ok(bookDto));
        }
コード例 #8
0
        private BookDto CreateLinksForBook(BookDto book)
        {
            string getBookForAuthorUrl = c_urHhelper.Link(
                "GetBookForAuthor",
                new
            {
                id = book.Id
            });

            book.Links.Add(new LinkDto(getBookForAuthorUrl, "self", WebRequestMethods.Http.Get));

            string deleteBookForAuthorUrl = c_urHhelper.Link(
                "DeleteBookForAuthor",
                new
            {
                id = book.Id
            });

            book.Links.Add(new LinkDto(deleteBookForAuthorUrl, "delete_book", "DELETE"));

            string updateBookForAuthorUrl = c_urHhelper.Link(
                "UpdateBookForAuthor",
                new
            {
                id = book.Id
            });

            book.Links.Add(new LinkDto(updateBookForAuthorUrl, "update_book", WebRequestMethods.Http.Put));

            string partiallyUpdateBookForAuthorUrl = c_urHhelper.Link(
                "PartiallyUpdateBookForAuthor",
                new
            {
                id = book.Id
            });

            book.Links.Add(new LinkDto(partiallyUpdateBookForAuthorUrl, "partially_update_book", "PATCH"));

            return(book);
        }
コード例 #9
0
        private BookDto CreateLinkForBook(Guid authorId, BookDto book)
        {
            book.Links.Clear();

            book.Links.Add(new Link(HttpMethods.Get.ToString(),
                                    "self",
                                    Url.Link(nameof(GetBookAsync), new { authorId, bookId = book.Id })));

            book.Links.Add(new Link(HttpMethods.Delete.ToString(),
                                    "delete book",
                                    Url.Link(nameof(DeleteBookAsync), new { authorId, bookId = book.Id })));

            book.Links.Add(new Link(HttpMethods.Put.ToString(),
                                    "update book",
                                    Url.Link(nameof(UpdateBookAsync), new { authorId, bookId = book.Id })));

            book.Links.Add(new Link(HttpMethods.Patch.ToString(),
                                    "partially update book",
                                    Url.Link(nameof(PartiallyUpdateBookAsync), new { authorId, bookId = book.Id })));

            return(book);
        }
コード例 #10
0
        private BookDto CreateLinksForBook(BookDto book)
        {
            var bookId = new { id = book.Id };

            var urlParams = new
            {
                authorId = book.AuthorId,
                id       = bookId
            };

            book.Links.Add(
                new LinkDto(new Uri(_urlHelper.Link(ApiNames.GetBookForAuthor, urlParams)),
                            LinksMetadata.Self,
                            LinksMetadata.HttpGet));

            book.Links.Add(new LinkDto(new Uri(_urlHelper.Link(ApiNames.DeleteBookForAuthor, urlParams)),
                                       "delete_book",
                                       "DELETE"));

            book.Links.Add(
                new LinkDto(
                    new Uri(_urlHelper.Link(ApiNames.UpdateBookForAuthor, urlParams)),
                    "update_book",
                    "UPDATE"));

            book.Links.Add(
                new LinkDto(
                    new Uri(_urlHelper.Link(
                                ApiNames.PartiallyUpdateBookForAuthor,
                                new {
                authorId = book.AuthorId,
                id       = bookId
            }
                                )),
                    "partially_update_book",
                    "PATCH"));

            return(book);
        }
コード例 #11
0
        private BookDto CreateLinksForBook(BookDto book)
        {
            book.Links.Add(new LinkDto(
                               href: _linkGenerator.GetUriByRouteValues(HttpContext, "GetBookForAuthor", new { bookId = book.Id }),
                               rel: "self",
                               method: "GET"));

            book.Links.Add(new LinkDto(
                               href: _linkGenerator.GetUriByRouteValues(HttpContext, "DeleteBookForAuthor", new { bookId = book.Id }),
                               rel: "delte_book",
                               method: "DELETE"));

            book.Links.Add(new LinkDto(
                               href: _linkGenerator.GetUriByRouteValues(HttpContext, "UpdateBookForAuthor", new { bookId = book.Id }),
                               rel: "update_book",
                               method: "PUT"));

            book.Links.Add(new LinkDto(
                               href: _linkGenerator.GetUriByRouteValues(HttpContext, "PartiallyUpdateBookForAuthor", new { bookId = book.Id }),
                               rel: "partially_update_book",
                               method: "PATCH"));

            return(book);
        }
コード例 #12
0
        public async Task <IActionResult> Add([FromBody] BookDto bookDto)
        {
            var createdBook = await _bookService.AddBook(bookDto); // why is this using a service not a repo?

            return(Ok());                                          // why not return the createdBook?
        }