Create Restaurant Service

In this lesson, we'll create the first implementation of the RestaurantService interface by creating a new service class that handles restaurant creation, building on our previously designed domain objects and interfaces.

Service Implementation

The service layer is where our business logic lives, bridging the gap between our controllers and data access layer.

Let's create our implementation class in the com.devtiro.restaurant.services.impl package:

@Service @RequiredArgsConstructor public class RestaurantServiceImpl implements RestaurantService { private final RestaurantRepository restaurantRepository; private final GeoLocationService geoLocationService; @Override public Restaurant createRestaurant(RestaurantCreateRequest restaurantCreateRequest) { // Implementation details to follow } }

We use @RequiredArgsConstructor to automatically create a constructor for our required dependencies.

Restaurant Creation Logic

Let's create an initial implementation of the method to save the restaurant in the database:

package com.devtiro.restaurant.services.impl; import com.devtiro.restaurant.domain.GeoLocation; import com.devtiro.restaurant.domain.RestaurantCreateUpdateRequest; import com.devtiro.restaurant.domain.entities.Address; import com.devtiro.restaurant.domain.entities.Photo; import com.devtiro.restaurant.domain.entities.Restaurant; import com.devtiro.restaurant.repositories.RestaurantRepository; import com.devtiro.restaurant.services.GeoLocationService; import com.devtiro.restaurant.services.RestaurantService; import lombok.RequiredArgsConstructor; import org.springframework.data.elasticsearch.core.geo.GeoPoint; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.List; @Service @RequiredArgsConstructor public class RestaurantServiceImpl implements RestaurantService { private final RestaurantRepository restaurantRepository; private final GeoLocationService geoLocationService; @Override public Restaurant createRestaurant(RestaurantCreateUpdateRequest request) { Address address = request.getAddress(); GeoLocation geoLocation = geoLocationService.geoLocate(address); GeoPoint geoPoint = new GeoPoint(geoLocation.getLatitude(), geoLocation.getLongitude()); List<String> photoIds = request.getPhotoIds(); List<Photo> photos = photoIds.stream().map(photoUrl -> Photo.builder() .url(photoUrl) .uploadDate(LocalDateTime.now()) .build()).toList(); Restaurant restaurant = Restaurant.builder() .name(request.getName()) .cuisineType(request.getCuisineType()) .contactInformation(request.getContactInformation()) .address(address) .geoLocation(geoPoint) .operatingHours(request.getOperatingHours()) .averageRating(0f) .photos(photos) .build(); return restaurantRepository.save(restaurant); } }

Processing Steps

The creation process follows a clear sequence:

First, we use our GeoLocationService (created in our previous Geolocation lesson) to convert the address into coordinates.

Next, we transform the provided photo URLs into Photo entities, setting the upload date to the current time.

Finally, we construct the Restaurant entity using the builder pattern, setting an initial rating of 0 since it's a new restaurant.

The ID field is intentionally omitted as Elasticsearch will generate this for us when the entity is saved.

Summary

  • Implemented RestaurantServiceImpl to handle restaurant creation
  • Used GeoLocationService to convert addresses to coordinates
  • Created Photo entities from provided URLs
  • Built complete Restaurant entity with initial 0-star rating
  • Saved restaurant using RestaurantRepository
© 2026 Devtiro Ltd. All rights reserved