Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created new getRatings endpoint, organized code #20

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jakarta.validation.Valid;
import sg.com.smartinventory.entities.Review;
import sg.com.smartinventory.services.ReviewService;
import org.springframework.web.bind.annotation.RequestParam;

@RestController
@RequestMapping("/reviews")
Expand Down Expand Up @@ -44,6 +45,12 @@ public ResponseEntity<Review> getReview(@PathVariable long id) {
return new ResponseEntity<>(foundReview, HttpStatus.OK);
}

// 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) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/sg/com/smartinventory/entities/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;

import lombok.AllArgsConstructor;
Expand All @@ -38,6 +39,8 @@ public class Review {
@Column(name = "review_content")
private String reviewContent;

@Min(value = 1, message = "Value must be between 1 and 5")
@Max(value = 5, message = "Value must be between 1 and 5")
@Column(name = "rating")
private int rating;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

@ControllerAdvice
public class GlobalExceptionHandler {
// This is handler for CustomerNotFoundException.
// This is handler for RuntimeExceptions. Flow: When a client request throws an
// exception, SB will find @ControllerAdvice and find the @ExceptionHandler that
// contains the Exception thrown.
@ExceptionHandler({ CustomerNotFoundException.class, ProductNotFoundException.class,
ReviewNotFoundException.class })
public ResponseEntity<ErrorResponse> handleResourceNotFoundException(CustomerNotFoundException ex) {
ReviewNotFoundException.class, RatingNotFoundException.class })
public ResponseEntity<ErrorResponse> handleResourceNotFoundException(RuntimeException ex) {
ErrorResponse errorResponse = new ErrorResponse(ex.getMessage(), LocalDateTime.now());

return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sg.com.smartinventory.exceptions;

public class RatingNotFoundException extends RuntimeException {
public RatingNotFoundException(int rating) {
super("No products with rating=" + rating + " found");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public interface ReviewRepository extends JpaRepository<Review, Long> {
// Custom query to find all reviews with a certain ID.
// List<Review> findById(long id);

// Custom query to find all reviews with a certain rating.
List<Review> findByRating(int rating);

// Custom query to find all reviews with a certain category.
List<Review> findByCategory(String category);

// Custom query to find all reviews with a certain review content.
List<Review> findByReviewContent(String reviewContent);

// Custom query to find all reviews with a certain rating.
List<Review> findByRating(int rating);

// Custom query to find all reviews with a certain customer ID.
List<Review> findByCustomerId(long customerId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Service;

import sg.com.smartinventory.entities.Review;
import sg.com.smartinventory.exceptions.RatingNotFoundException;
import sg.com.smartinventory.exceptions.ReviewNotFoundException;
import sg.com.smartinventory.repositories.ReviewRepository;
import sg.com.smartinventory.services.ReviewService;
Expand All @@ -19,21 +21,38 @@ public ReviewServiceImpl(ReviewRepository reviewRepository) {
this.reviewRepository = reviewRepository;
}

// - - - POST - - -
@Override
public Review createReview(Review review) {
Review newReview = reviewRepository.save(review);

return newReview;
}

// - - - GET - - -
@Override
public ArrayList<Review> searchCustomerReviews(long customerId) {
List<Review> foundReviews = reviewRepository.findByCustomerId(customerId);
return (ArrayList<Review>) foundReviews;
}

@Override
public ArrayList<Review> searchProductReviews(long 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);
}
List<Review> reviews = reviewRepository.findByRating(id);
return (ArrayList<Review>) reviews;
}

@Override
public Review getReview(Long id) {
// Optional<Review> optionalReview = reviewRepository.findById(id);
// if(optionalReview.isPresent()) {
// Review foundReview = optionalReview.get();
// return foundReview;
// }
// throw new ReviewNotFoundException(id);
return reviewRepository.findById(id).orElseThrow(() -> new ReviewNotFoundException(id));
}

Expand All @@ -43,6 +62,7 @@ public ArrayList<Review> getAllReviews() {
return (ArrayList<Review>) allReviews;
}

// - - - PUT - - -
@Override
public Review updateReview(Long id, Review review) {
// Retrieve the review from the database.
Expand All @@ -58,20 +78,9 @@ public Review updateReview(Long id, Review review) {
return reviewRepository.save(reviewToUpdate);
}

// - - - DELETE - - -
@Override
public void deleteReview(long id) {
reviewRepository.deleteById(id);
}

@Override
public ArrayList<Review> searchCustomerReviews(long customerId) {
List<Review> foundReviews = reviewRepository.findByCustomerId(customerId);
return (ArrayList<Review>) foundReviews;
}

@Override
public ArrayList<Review> searchProductReviews(long productId) {
List<Review> foundReviews = reviewRepository.findByProductId(productId);
return (ArrayList<Review>) foundReviews;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public interface ReviewService {
ArrayList<Review> searchCustomerReviews(long customerId);

ArrayList<Review> searchProductReviews(long productId);

ArrayList<Review> getRatings(int rating);
}