@GET @Path("/{reviewId}") @Timed(name = "view-book-by-reviewId") public Response viewBookReview( @PathParam("isbn") LongParam isbn, @PathParam("reviewId") IntParam reviewId) { Book book = bookRepository.getBookByISBN(isbn.get()); ReviewDto reviewResponse = null; List<Review> reviewList = book.getReview(); List<Review> tempList = new ArrayList<Review>(); for (Review reviewObj : reviewList) { if (reviewObj.getId() == reviewId.get()) tempList.add(reviewObj); } reviewResponse = new ReviewDto(tempList); String location = "/books/" + book.getIsbn() + "/reviews/"; HashMap<String, Object> map = new HashMap<String, Object>(); Review review = reviewResponse.getReviewList().get(0); map.put("review", review); reviewResponse.addLink(new LinkDto("view-review", location + reviewId.get(), "GET")); map.put("links", reviewResponse.getLinks()); return Response.status(200).entity(map).build(); }
// ****CREATING REVIEWS**** @POST @Path("/{isbn}/reviews") @Timed(name = "create-review") public Response createReview(@Valid Review reviews, @PathParam("isbn") long isbn) { Book retrieveBook = bookRepository.getBookByISBN(isbn); reviews.setId((int) reviewID); retrieveBook.getReviews().add(reviews); reviewID++; ReviewDto reviewResponse = new ReviewDto(); reviewResponse.addLink( new LinkDto( "view-review", "/books/" + retrieveBook.getIsbn() + "/reviews/" + reviews.getId(), "GET")); return Response.status(201).entity(reviewResponse.getLinks()).build(); }
@POST @Timed(name = "create-review") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Response createReview(@PathParam("isbn") LongParam isbn, Review request) { // int rating = request.getRating(); if (request.getComment() == null) { return Response.status(400).type("text/plain").entity(" Comment is a required field").build(); } if (request.getRating() == 0) { return Response.status(400) .type("text/plain") .entity(" Rating is a required field !!!!") .build(); } final List<Integer> ratingList = Arrays.asList(1, 2, 3, 4, 5); if (!(ratingList.contains(request.getRating()))) { return Response.status(400) .type("text/plain") .entity("Invalid value for Rating. Ratings can be between 1 - 5 stars") .build(); } Review reviewObject = bookRepository.createReview(isbn.get(), request); Book book = bookRepository.getBookByISBN(isbn.get()); String location = "/books/" + book.getIsbn() + "/reviews/" + reviewObject.getId(); BookDto bookResponse = new BookDto(book); bookResponse.addLink(new LinkDto("view-review", location, "GET")); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("links", bookResponse.getLinks()); return Response.status(201).entity(map).build(); }