Skip to content

Commit

Permalink
added all important endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerstock committed Jun 25, 2019
1 parent 3956640 commit 352b11f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.List;

@RestController
@RequestMapping(value = "/book")
@RequestMapping(value = "/books")
public class BooksController {

private static final Logger logger = LoggerFactory.getLogger(Book.class);
Expand Down Expand Up @@ -51,7 +51,7 @@ public ResponseEntity<?> listAllBooks(@PageableDefault(page = 0, size = 10) Page
@ApiResponse(code = 200, message = "book found", response = Book.class),
@ApiResponse(code = 500, message = "failed to find book", response = ErrorDetail.class)
})
@GetMapping(value = "books/{id}")
@GetMapping(value = "/{id}")
public ResponseEntity<?> getBook(@PathVariable long id) {

Book book = bookService.findBookById(id);
Expand All @@ -63,10 +63,10 @@ public ResponseEntity<?> getBook(@PathVariable long id) {
}


@PostMapping(value = "addbook")
@PostMapping(value = "/add")
public ResponseEntity<?> addBook(@RequestBody Book addBook) {
bookService.addbook(addBook);
logger.info("/addbook POST endpoint accessed");
logger.info("books//add POST endpoint accessed");
return new ResponseEntity<>(HttpStatus.OK);

}
Expand All @@ -76,15 +76,15 @@ public ResponseEntity<?> addBook(@RequestBody Book addBook) {
@ApiResponse(code = 200, message = "book successfully updated", response = Book.class),
@ApiResponse(code = 500, message = "failed to update book", response = ErrorDetail.class)
})
@PutMapping(value = "/data/books/{id}")
@PutMapping(value = "/{id}")
public ResponseEntity<?> updateBook(@RequestBody Book updateBook, @PathVariable long id) {

bookService.updateBook(updateBook, id);

if (bookService.updateBook(updateBook, id) == null) {
throw new ResourceNotFoundException("could not update book");
} else {
logger.info("/data/books/{id} UPDATE endpoint accessed");
logger.info("books/{id} UPDATE endpoint accessed");
}

return new ResponseEntity<>(HttpStatus.OK);
Expand All @@ -96,13 +96,13 @@ public ResponseEntity<?> updateBook(@RequestBody Book updateBook, @PathVariable
@ApiResponse(code = 200, message = "Book successfully deleted", response = Book.class),
@ApiResponse(code = 500, message = "failed to delete Book", response = ErrorDetail.class)
})
@DeleteMapping("/data/books/{id}")
@DeleteMapping("/delete/{id}")
public ResponseEntity<?> deleteBookById(@PathVariable long id) {

if (bookService.findBookById(id) == null) {
throw new ResourceNotFoundException("could not find book with id of " + id);
} else {
logger.info("/data/books/{id} DELETE endpoint accessed");
logger.info("/books/delete/{id} DELETE endpoint accessed");
bookService.delete(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,41 @@ public ResponseEntity<?> listAllReviews(@PageableDefault(page = 0, size = 10) Pa
@ApiImplicitParam(name = "size", dataType = "integer", paramType = "query", value = "specifies the page size"),
@ApiImplicitParam(name = "sort", dataType = "string", allowMultiple = true, paramType = "query", value = "Sorts result [name, address, etc]")
})
@GetMapping(value = "/books/{bookid}", produces = {"application/json"})
@GetMapping(value = "/bybook/{bookid}", produces = {"application/json"})
public ResponseEntity<?> listReviewsByBook(@PathVariable long bookid, @PageableDefault(page = 0, size = 10) Pageable pageable)
{

List<Review> myReviews = reviewService.findReviewsByBook(pageable, bookid);

if (myReviews == null) {
throw new ResourceNotFoundException("no reviews found");
throw new ResourceNotFoundException("no reviews found for this book");
} else{
logger.info("/reviews/books/{bookid} accessed");
logger.info("/reviews/books/{bookid} GET accessed");
}

return new ResponseEntity<>(myReviews, HttpStatus.OK);
}

@PutMapping(value = "/byreview/{reviewid}")
public ResponseEntity<?> editReview(@PathVariable long reviewid, @RequestBody Review review) {
reviewService.updateReview(review, reviewid);
logger.info("/reviews/byreview/{reviewid} PUT accessed");
return new ResponseEntity<>(HttpStatus.OK);
}


@ApiOperation(value = "adds a review to a book", response = Book.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "review successfully added", response = Review.class),
@ApiResponse(code = 500, message = "failed to add review", response = ErrorDetail.class)
})
@PostMapping(value = "/addreview/book/{id}")
public ResponseEntity<?> postReview(@RequestBody Review review, @PathVariable long id) {
@PostMapping(value = "/add}")
public ResponseEntity<?> postReview(@RequestBody Review review) {


if (!bookService.addReview(review, id)) {
throw new ResourceNotFoundException("could not add review");
}
logger.info("/data/books/{id} UPDATE endpoint accessed");
reviewService.save(review);

logger.info("/reviews/add POST endpoint accessed");

return new ResponseEntity<>(HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public interface BookService {

Book findBookById(long id);

boolean addReview(Review review, long id);

void addbook(Book addBook);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public Book updateBook(Book book, long id) {
Book currentBook = repo.findById(id).orElseThrow(EntityNotFoundException::new);
if(book.getBooktitle() != null){
currentBook.setBooktitle(book.getBooktitle());
}//TODO: Add the rest of the fields to be updated
currentBook.setAuthor(book.getAuthor());
currentBook.setImageurl(book.getImageurl());
currentBook.setLicense(book.getLicense());
currentBook.setPublisher(book.getPublisher());
currentBook.setUrl(book.getUrl());
}
repo.save(currentBook);
return currentBook;
}
Expand Down Expand Up @@ -62,11 +67,7 @@ public Book findBookById(long id) {
return repo.findById(id).orElseThrow(EntityNotFoundException::new);
}

@Override
public boolean addReview(Review review, long id) {
Book currentBook = repo.findById(id).orElseThrow(EntityNotFoundException::new);
return currentBook.getReviews().add(review);
}


@Override
public void addbook(Book addBook) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface ReviewService {
List<Review> findAll(Pageable pageable);

List<Review> findReviewsByBook(Pageable pageable, Long id);

Review updateReview(Review review, long reviewid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -36,4 +38,18 @@ public List<Review> findReviewsByBook(Pageable pageable, Long id) {
repo.findReviewsById(id).iterator().forEachRemaining(bookList::add);
return bookList;
}

@Transactional
@Override
public Review updateReview(Review review, long reviewid) {
Review currentReview = repo.findById(reviewid).orElseThrow(EntityNotFoundException::new);
if(review.getReview() != null){
currentReview.setBook(review.getBook());
currentReview.setRating(review.getRating());
currentReview.setReview(review.getReview());
currentReview.setUser(review.getUser());
}
repo.save(currentReview);
return currentReview;
}
}

0 comments on commit 352b11f

Please sign in to comment.