Skip to content

Commit

Permalink
Merge pull request #23 from kiblykat/sis-41b
Browse files Browse the repository at this point in the history
SiS-41.
  • Loading branch information
nhkhai authored Mar 29, 2024
2 parents 0b1f3e3 + 0bb48a6 commit d60d1bc
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 49 deletions.
57 changes: 51 additions & 6 deletions ThunderClient/EndpointTests.json
Original file line number Diff line number Diff line change
@@ -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": [
{
Expand Down Expand Up @@ -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": [
{
Expand All @@ -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": []
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArrayList<Review>> getAllReviews() {
ArrayList<Review> allReviews = reviewService.getAllReviews();
return new ResponseEntity<>(allReviews, HttpStatus.OK);
}
// READ (GET ALL).
@GetMapping("")
public ResponseEntity<ArrayList<Review>> getAllReviews() {
ArrayList<Review> allReviews = reviewService.getAllReviews();
return new ResponseEntity<>(allReviews, HttpStatus.OK);
}

// READ (GET ONE).
@GetMapping("/{id}")
public ResponseEntity<Review> getReview(@PathVariable long id) {
Review foundReview = reviewService.getReview(id);
return new ResponseEntity<>(foundReview, HttpStatus.OK);
}
// READ (GET ONE).
@GetMapping("/{id}")
public ResponseEntity<Review> getReview(@PathVariable long id) {
Review foundReview = reviewService.getReview(id);
return new ResponseEntity<>(foundReview, HttpStatus.OK);
}

// Read (Get ratings)
@GetMapping("/ratings")
public ArrayList<Review> getRatings(@Valid @RequestParam int rating) {
return reviewService.getRatings(rating);
}
// Read (Get ratings)
@GetMapping("/ratings")
public ArrayList<Review> getRatings(@Valid @RequestParam int rating) {
return reviewService.getRatings(rating);
}

// UPDATE.
@PutMapping("/{id}")
public ResponseEntity<Review> updateReview(@PathVariable long id, @RequestBody Review review) {
Review updatedReview = reviewService.updateReview(id, review);
return new ResponseEntity<>(updatedReview, HttpStatus.OK);
}
// UPDATE.
@PutMapping("/{id}")
public ResponseEntity<Review> updateReview(@PathVariable long id, @RequestBody Review review) {
Review updatedReview = reviewService.updateReview(id, review);
return new ResponseEntity<>(updatedReview, HttpStatus.OK);
}

// DELETE.
@DeleteMapping("/{id}")
public ResponseEntity<Review> deleteReview(@PathVariable long id) {
reviewService.deleteReview(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
// DELETE.
@DeleteMapping("/{id}")
public ResponseEntity<Review> deleteReview(@PathVariable long id) {
reviewService.deleteReview(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -38,17 +42,18 @@ public ArrayList<Review> searchCustomerReviews(long customerId) {

@Override
public ArrayList<Review> searchProductReviews(long productId) {
Product product = productRepository.findById(productId).orElseThrow(() -> new ProductNotFoundException(productId));
List<Review> foundReviews = reviewRepository.findByProductId(productId);
return (ArrayList<Review>) foundReviews;
}

@Override
public ArrayList<Review> getRatings(int id) {
if (reviewRepository.findByRating(id).isEmpty()) {
throw new RatingNotFoundException(id);
public ArrayList<Review> getRatings(int rating) {
if (reviewRepository.findByRating(rating).isEmpty()) {
throw new RatingNotFoundException(rating);
}
List<Review> reviews = reviewRepository.findByRating(id);
return (ArrayList<Review>) reviews;
List<Review> foundRating = reviewRepository.findByRating(rating);
return (ArrayList<Review>) foundRating;
}

@Override
Expand All @@ -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);
Expand Down

0 comments on commit d60d1bc

Please sign in to comment.