Review Retrieve Service
In this lesson, we'll implement the functionality to retrieve individual restaurant reviews in our service layer.
We'll need this functionality for our edit review page.
Service Implementation
Let's add the a method for retrieving individual reviews to our existing review service interface:
package com.devtiro.restaurant.services;
import com.devtiro.restaurant.domain.ReviewCreateUpdateRequest;
import com.devtiro.restaurant.domain.entities.Review;
import com.devtiro.restaurant.domain.entities.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.Optional;
public interface ReviewService {
// ...
Optional<Review> getRestaurantReview(String restaurantId, String reviewId);
}Here's the implementation:
@Service
@RequiredArgsConstructor
public class ReviewServiceImpl implements ReviewService {
// ...
@Override
public Optional<Review> getRestaurantReview(String restaurantId, String reviewId) {
Restaurant restaurant = getRestaurantOrThrow(restaurantId);
return restaurant.getReviews().stream()
.filter(r -> reviewId.equals(r.getId()))
.findFirst();
}
}Summary
- Implemented
getReviewinReviewService - Returned
Optional<Review>to handle non-existent reviews