Create Restaurant Controller
Now that we have our service layer and DTOs in place, we'll create the REST API endpoint that allows users to create new restaurants in our application.
Building the Controller
Let's create our RestaurantController class in the com.devtiro.restaurant.controllers package:
package com.devtiro.restaurant.controllers;
import com.devtiro.restaurant.domain.RestaurantCreateUpdateRequest;
import com.devtiro.restaurant.domain.dtos.RestaurantCreateUpdateRequestDto;
import com.devtiro.restaurant.domain.dtos.RestaurantDto;
import com.devtiro.restaurant.domain.entities.Restaurant;
import com.devtiro.restaurant.mappers.RestaurantMapper;
import com.devtiro.restaurant.services.RestaurantService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(path = "/api/restaurants")
@RequiredArgsConstructor
public class RestaurantController {
private final RestaurantService restaurantService;
private final RestaurantMapper restaurantMapper;
@PostMapping
public ResponseEntity<RestaurantDto> createRestaurant(
@Valid @RequestBody RestaurantCreateUpdateRequestDto request
) {
RestaurantCreateUpdateRequest restaurantCreateUpdateRequest = restaurantMapper
.toRestaurantCreateUpdateRequest(request);
Restaurant restaurant = restaurantService.createRestaurant(restaurantCreateUpdateRequest);
RestaurantDto createdRestaurantDto = restaurantMapper.toRestaurantDto(restaurant);
return ResponseEntity.ok(createdRestaurantDto);
}
}Summary
- Created
RestaurantControllerto handle HTTP requests for restaurant creation - Tested the new endpoint worked using the UI