Restaurant Retrieval Service
In this lesson, we'll implement the functionality to retrieve detailed restaurant information by ID, which will power the restaurant details page of our application.
Service Layer Implementation
The service layer acts as a bridge between our controllers and data access layer, providing an abstraction over the data retrieval process.
Let's start by adding the method signature to our RestaurantService interface:
public interface RestaurantService {
// ... other methods ...
Optional<Restaurant> getRestaurant(String id);
}Now let's implement this method in our RestaurantServiceImpl class:
@Service
@RequiredArgsConstructor
public class RestaurantServiceImpl implements RestaurantService {
private final RestaurantRepository restaurantRepository;
// ... other methods ...
@Override
public Optional<Restaurant> getRestaurant(String id) {
// Delegate to the repository to fetch the restaurant by ID
return restaurantRepository.findById(id);
}
}Summary
- Restaurant retrieval is implemented in the service layer via the
getRestaurantmethod - The method returns an
Optional<Restaurant>to handle cases where restaurants aren't found - Implementation delegates to the repository layer using
findById