Review Delete Service
In this lesson, we'll implement the functionality that enables the deletion of reviews from restaurants in our platform.
Service Layer Implementation
Let's add the deleteReview method to the service layer:
void deleteReview(String restaurantId, String reviewId);Now let's implement this method in our ReviewServiceImpl class:
@Override
public void deleteReview(String restaurantId, String reviewId) {
// Get the restaurant or throw an exception if not found
Restaurant restaurant = getRestaurantOrThrow(restaurantId);
// Filter out the review with the matching ID
List<Review> filteredReviews = restaurant.getReviews().stream()
.filter(review -> !reviewId.equals(review.getId()))
.toList();
// Update the restaurant's reviews
restaurant.setReviews(filteredReviews);
// Update the restaurant's average rating
updateRestaurantAverageRating(restaurant);
// Save the updated restaurant
restaurantRepository.save(restaurant);
}Summary
- Review deletion is handled through the
deleteReviewmethod inReviewServiceImpl - The implementation removes the review and updates the restaurant's average rating