From 00b2aae808fb639f802f0aa9ba654e4a4a5641b7 Mon Sep 17 00:00:00 2001 From: ylongtay <145927097+ylongtay@users.noreply.github.com> Date: Sat, 30 Mar 2024 01:04:16 +0800 Subject: [PATCH] SIS-41, add more review endpoints --- ThunderClient/EndpointTests.json | 57 +++++++++++++-- .../controllers/ReviewController.java | 72 +++++++++---------- .../serviceImpls/ReviewServiceImpl.java | 19 +++-- .../services/ReviewServiceImplTest.java | 16 ++--- 4 files changed, 106 insertions(+), 58 deletions(-) diff --git a/ThunderClient/EndpointTests.json b/ThunderClient/EndpointTests.json index f411345..88ace4e 100644 --- a/ThunderClient/EndpointTests.json +++ b/ThunderClient/EndpointTests.json @@ -1,7 +1,7 @@ { "client": "Thunder Client", "collectionName": "SmartInventory", - "dateExported": "2024-03-29T15:11:15.315Z", + "dateExported": "2024-03-29T17:03:34.803Z", "version": "1.1", "folders": [ { @@ -154,7 +154,7 @@ "method": "POST", "sortNum": 80000, "created": "2024-03-29T06:19:27.794Z", - "modified": "2024-03-29T15:08:54.672Z", + "modified": "2024-03-29T16:52:10.180Z", "headers": [], "params": [ { @@ -170,7 +170,7 @@ ], "body": { "type": "json", - "raw": "{\n \"category\": \"Books\",\n \"reviewContent\": \"Expected more from the ending, felt rushed.\",\n \"rating\": 4\n}", + "raw": "{\n \"category\": \"Books\",\n \"reviewContent\": \"Kind of Boring\",\n \"rating\": 2\n}", "form": [] }, "tests": [] @@ -262,6 +262,26 @@ ], "tests": [] }, + { + "_id": "e47d6714-1722-4dc6-8d35-7bcaecb75dd2", + "colId": "e34b9201-d6f7-4614-a21c-21c245c1032e", + "containerId": "0db7fba9-c06d-462f-a621-ce608d52ea78", + "name": "Get Ratings", + "url": "http://localhost:9090/reviews/ratings?rating=2", + "method": "GET", + "sortNum": 117500, + "created": "2024-03-29T16:19:42.921Z", + "modified": "2024-03-29T16:54:37.993Z", + "headers": [], + "params": [ + { + "name": "rating", + "value": "2", + "isPath": false + } + ], + "tests": [] + }, { "_id": "24969b8e-21da-4985-ab11-120cc26e3949", "colId": "e34b9201-d6f7-4614-a21c-21c245c1032e", @@ -302,6 +322,31 @@ ], "tests": [] }, + { + "_id": "9595267f-b4aa-4b75-8b79-d06b1a2b55df", + "colId": "e34b9201-d6f7-4614-a21c-21c245c1032e", + "containerId": "0db7fba9-c06d-462f-a621-ce608d52ea78", + "name": "Update Review", + "url": "http://localhost:9090/reviews/{id}", + "method": "PUT", + "sortNum": 125000, + "created": "2024-03-29T16:08:12.228Z", + "modified": "2024-03-29T16:56:50.688Z", + "headers": [], + "params": [ + { + "name": "id", + "value": "4", + "isPath": true + } + ], + "body": { + "type": "json", + "raw": "{\n \"category\": \"Electronics\",\n \"reviewContent\": \"Fast delivery, product works as expected. Like!\",\n \"rating\": 3\n}", + "form": [] + }, + "tests": [] + }, { "_id": "284be03e-7cea-4fb1-b181-593b29ebf95d", "colId": "e34b9201-d6f7-4614-a21c-21c245c1032e", @@ -351,17 +396,17 @@ "_id": "fb7039d9-56e5-4883-995c-d06a7eea82b0", "colId": "e34b9201-d6f7-4614-a21c-21c245c1032e", "containerId": "0db7fba9-c06d-462f-a621-ce608d52ea78", - "name": "Delete Product", + "name": "Delete Review", "url": "http://localhost:9090/reviews/{id}", "method": "DELETE", "sortNum": 140000, "created": "2024-03-29T12:28:07.295Z", - "modified": "2024-03-29T12:38:39.979Z", + "modified": "2024-03-29T16:58:08.373Z", "headers": [], "params": [ { "name": "id", - "value": "9", + "value": "10", "isPath": true } ], diff --git a/src/main/java/sg/com/smartinventory/controllers/ReviewController.java b/src/main/java/sg/com/smartinventory/controllers/ReviewController.java index 94460ea..7e2d0e8 100644 --- a/src/main/java/sg/com/smartinventory/controllers/ReviewController.java +++ b/src/main/java/sg/com/smartinventory/controllers/ReviewController.java @@ -21,47 +21,47 @@ @RestController @RequestMapping("/reviews") public class ReviewController { - private ReviewService reviewService; + private ReviewService reviewService; - // @Autowired - public ReviewController(ReviewService reviewService) { - this.reviewService = reviewService; - } + // @Autowired + public ReviewController(ReviewService reviewService) { + this.reviewService = reviewService; + } - // - - - NO POST HERE, due to data integrity with Customer FK - - - - // @PostMapping("")... + // - - - NO POST HERE, due to data integrity with Customer FK - - - + // @PostMapping("")... - // READ (GET ALL). - @GetMapping("") - public ResponseEntity> getAllReviews() { - ArrayList allReviews = reviewService.getAllReviews(); - return new ResponseEntity<>(allReviews, HttpStatus.OK); - } + // READ (GET ALL). + @GetMapping("") + public ResponseEntity> getAllReviews() { + ArrayList allReviews = reviewService.getAllReviews(); + return new ResponseEntity<>(allReviews, HttpStatus.OK); + } - // READ (GET ONE). - @GetMapping("/{id}") - public ResponseEntity getReview(@PathVariable long id) { - Review foundReview = reviewService.getReview(id); - return new ResponseEntity<>(foundReview, HttpStatus.OK); - } + // READ (GET ONE). + @GetMapping("/{id}") + public ResponseEntity getReview(@PathVariable long id) { + Review foundReview = reviewService.getReview(id); + return new ResponseEntity<>(foundReview, HttpStatus.OK); + } - // Read (Get ratings) - @GetMapping("/ratings") - public ArrayList getRatings(@Valid @RequestParam int rating) { - return reviewService.getRatings(rating); - } + // Read (Get ratings) + @GetMapping("/ratings") + public ArrayList getRatings(@Valid @RequestParam int rating) { + return reviewService.getRatings(rating); + } - // UPDATE. - @PutMapping("/{id}") - public ResponseEntity updateReview(@PathVariable long id, @RequestBody Review review) { - Review updatedReview = reviewService.updateReview(id, review); - return new ResponseEntity<>(updatedReview, HttpStatus.OK); - } + // UPDATE. + @PutMapping("/{id}") + public ResponseEntity updateReview(@PathVariable long id, @RequestBody Review review) { + Review updatedReview = reviewService.updateReview(id, review); + return new ResponseEntity<>(updatedReview, HttpStatus.OK); + } - // DELETE. - @DeleteMapping("/{id}") - public ResponseEntity deleteReview(@PathVariable long id) { - reviewService.deleteReview(id); - return new ResponseEntity<>(HttpStatus.NO_CONTENT); - } + // DELETE. + @DeleteMapping("/{id}") + public ResponseEntity deleteReview(@PathVariable long id) { + reviewService.deleteReview(id); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } } \ No newline at end of file diff --git a/src/main/java/sg/com/smartinventory/serviceImpls/ReviewServiceImpl.java b/src/main/java/sg/com/smartinventory/serviceImpls/ReviewServiceImpl.java index cef7e4b..77eba74 100644 --- a/src/main/java/sg/com/smartinventory/serviceImpls/ReviewServiceImpl.java +++ b/src/main/java/sg/com/smartinventory/serviceImpls/ReviewServiceImpl.java @@ -6,26 +6,30 @@ import org.springframework.stereotype.Service; +import sg.com.smartinventory.entities.Product; import sg.com.smartinventory.entities.Review; +import sg.com.smartinventory.exceptions.ProductNotFoundException; import sg.com.smartinventory.exceptions.RatingNotFoundException; import sg.com.smartinventory.exceptions.ReviewNotFoundException; +import sg.com.smartinventory.repositories.ProductRepository; import sg.com.smartinventory.repositories.ReviewRepository; import sg.com.smartinventory.services.ReviewService; @Service public class ReviewServiceImpl implements ReviewService { private ReviewRepository reviewRepository; + private ProductRepository productRepository; // @Autowired public ReviewServiceImpl(ReviewRepository reviewRepository) { this.reviewRepository = reviewRepository; + this.productRepository = productRepository; } // - - - POST - - - @Override public Review createReview(Review review) { Review newReview = reviewRepository.save(review); - return newReview; } @@ -38,17 +42,18 @@ public ArrayList searchCustomerReviews(long customerId) { @Override public ArrayList searchProductReviews(long productId) { + Product product = productRepository.findById(productId).orElseThrow(() -> new ProductNotFoundException(productId)); List foundReviews = reviewRepository.findByProductId(productId); return (ArrayList) foundReviews; } @Override - public ArrayList getRatings(int id) { - if (reviewRepository.findByRating(id).isEmpty()) { - throw new RatingNotFoundException(id); + public ArrayList getRatings(int rating) { + if (reviewRepository.findByRating(rating).isEmpty()) { + throw new RatingNotFoundException(rating); } - List reviews = reviewRepository.findByRating(id); - return (ArrayList) reviews; + List foundRating = reviewRepository.findByRating(rating); + return (ArrayList) foundRating; } @Override @@ -72,7 +77,7 @@ public Review updateReview(Long id, Review review) { reviewToUpdate.setCategory(review.getCategory()); reviewToUpdate.setReviewContent(review.getReviewContent()); reviewToUpdate.setRating(review.getRating()); - reviewToUpdate.setProduct(review.getProduct()); + // reviewToUpdate.setProduct(review.getProduct()); // Save the updated review back to the database. return reviewRepository.save(reviewToUpdate); diff --git a/src/test/java/sg/com/smartinventory/services/ReviewServiceImplTest.java b/src/test/java/sg/com/smartinventory/services/ReviewServiceImplTest.java index 773a676..38b7c57 100644 --- a/src/test/java/sg/com/smartinventory/services/ReviewServiceImplTest.java +++ b/src/test/java/sg/com/smartinventory/services/ReviewServiceImplTest.java @@ -22,11 +22,7 @@ import org.springframework.boot.test.context.SpringBootTest; -import sg.com.smartinventory.entities.Product; import sg.com.smartinventory.entities.Review; -import sg.com.smartinventory.exceptions.CustomerNotFoundException; -import sg.com.smartinventory.exceptions.ProductNotFoundException; -import sg.com.smartinventory.repositories.ProductRepository; import sg.com.smartinventory.repositories.ReviewRepository; import sg.com.smartinventory.serviceImpls.ReviewServiceImpl; @@ -34,10 +30,12 @@ public class ReviewServiceImplTest { @Mock private ReviewRepository reviewRepository; - private ProductRepository productRepository; + @Mock + // private ProductRepository productRepository; @InjectMocks ReviewServiceImpl reviewService; + // ProductServiceImpl productService; /// Name this according to your class name. // The Logback library defines 5 log levels in order of priority: TRACE, DEBUG, @@ -72,11 +70,11 @@ public void createReviewTest() { // 1. SETUP. // Create a new review. - long id = 2; - Product product = productRepository.findById(id).orElseThrow(() -> new ProductNotFoundException(id)); + // long id = 2; + // Product product = productRepository.findById(id).orElseThrow(() -> new + // ProductNotFoundException(id)); - NOT needed Review review = Review.builder().category("Electronics") - .reviewContent("Great smartphone with excellent features. ").rating(5) - .product(product).build(); + .reviewContent("Great smartphone with excellent features. ").rating(5).build(); // Mock the save method of the review repository. when((reviewRepository.save(review))).thenReturn(review);