@POST @Produces(MediaType.APPLICATION_JSON) @Timed(name = "create-book") public Response createBook(BookLink bookRequest) { ArrayList<LinkDto> authorArray = new ArrayList(); Book newBook = new Book( bookRequest.getBookTitle(), bookRequest.getBookAuthors(), bookRequest.getBookLanguage(), bookRequest.getNoOfPages(), bookRequest.getPublication_date(), bookRequest.getBookStatus(), bookRequest.getBookReviews()); // newbook created Long new_isbn = newBook.getNewIsbn(); Map<Long, Author> authorMap = new HashMap<Long, Author>(); book_map.put(new_isbn, newBook); for (Author author : bookRequest.getBookAuthors()) { Long author_id = author.createAuthorID(); authorMap.put(author_id, author); authorArray.add( new LinkDto("view-author", "/books/" + new_isbn + "/authors/" + author_id, "GET")); } newBook.setBookAuthors(authorArray); AuthorHashMap.put(new_isbn, authorMap); LinksDto linkResponse = new LinksDto(); linkResponse.addLink(new LinkDto("view-book", "/books/" + new_isbn, "GET")); linkResponse.addLink(new LinkDto("update-book", "/books/" + new_isbn, "PUT")); linkResponse.addLink(new LinkDto("delete-book", "/books/" + new_isbn, "PUT")); linkResponse.addLink(new LinkDto("create-review", "/books/" + new_isbn + "/reviews", "POST")); return Response.created(null).entity(linkResponse).build(); }
@DELETE @Path("/{isbn}") @Timed(name = "delete-book") public Response deleteBook(@PathParam("isbn") LongParam isbn) { book_map.remove(isbn.get()); LinksDto linkResponse = new LinksDto(); linkResponse.addLink(new LinkDto("create-book", "/books/", "POST")); return Response.ok(linkResponse).build(); }
// ****DELETING THE BOOK**** @DELETE @Path("/{isbn}") @Timed(name = "delete-book") public Response deleteBook(@PathParam("isbn") long isbn) { // Delete the book from the BookRepository. bookRepository.deleteBook(isbn); LinksDto links = new LinksDto(); links.addLink(new LinkDto("add-book", "\book", "POST")); // Add other links if needed return Response.ok(links).build(); }
// ****DISPLAYING A REVIEWS**** @GET @Path("/{isbn}/reviews/{id}") @Timed(name = "view-review") public Response displayReview(@PathParam("isbn") long isbn, @PathParam("id") long id) { int i = 0; Book book = bookRepository.getBookByISBN(isbn); ReviewDto response = new ReviewDto(book.getoneReview(i)); LinksDto links = new LinksDto(); links.addLink( new LinkDto( "view-review", "/books/" + book.getIsbn() + "/reviews/" + book.getoneReview(i).getId(), "GET")); return Response.ok(links).build(); }
@POST @Path("/{isbn}/reviews") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @Timed(name = "create-book-review") public Response createReview(@PathParam("isbn") LongParam isbn, Reviews newReview) { Reviews newBookReview = new Reviews(); Long reviewId = newBookReview.createRevID(); newReview.setID(reviewId); newBookReviewMap.put(reviewId, newReview); reviewsArray.add(newReview); BookReviewMapOne.put(isbn.get(), reviewsArray); LinksDto reviewResponse = new LinksDto(); reviewResponse.addLink( new LinkDto("view-review", "/books/" + isbn + "/reviews/" + reviewId, "GET")); return Response.created(null).entity(reviewResponse).build(); }
@PUT @Path("/{isbn}") @Timed(name = "update-book") public Response updateBookStatus( @PathParam("isbn") LongParam isbn, @QueryParam("status") String status) { Book book = (Book) book_map.get(isbn.get()); book.setNewStatus(status); LinksDto response = new LinksDto(); response.addLink(new LinkDto("view-book", "/books/" + isbn, "GET")); response.addLink(new LinkDto("update-book", "/books/" + isbn, "PUT")); response.addLink(new LinkDto("delete-book", "/books/" + isbn, "DELETE")); response.addLink(new LinkDto("create-review", "/books/" + isbn + "/reviews", "POST")); if (reviewsArray.size() > 0) response.addLink(new LinkDto("view-all-reviews", "/books/" + isbn + "/reviews", "GET")); return Response.ok(response).build(); }