Restaurant Update Controller
In this lesson, we'll implement the REST API endpoint that allows users to update restaurant details in our application.
Implementing the Update Endpoint
The update endpoint needs to handle PUT requests and validate the incoming data.
Here's the implementation for the updateRestaurant method in our RestaurantController:
@PutMapping("/{restaurantId}")
public ResponseEntity<RestaurantDto> updateRestaurant(
@PathVariable String restaurantId,
@Valid @RequestBody RestaurantCreateUpdateRequestDto restaurantCreateUpdateRequestDto) {
// Convert DTO to domain object
RestaurantCreateUpdateRequest restaurantCreateUpdateRequest =
restaurantMapper.toRestaurantCreateUpdateRequest(restaurantCreateUpdateRequestDto);
// Update the restaurant
Restaurant updated = restaurantService.updateRestaurant(restaurantId, restaurantCreateUpdateRequest);
// Convert and return the updated restaurant
return ResponseEntity.ok(restaurantMapper.toRestaurantDto(updated));
}Error Handling
Our endpoint benefits from the error handling we set up in previous lessons.
If the restaurant isn't found, the RestaurantNotFoundException we created will trigger our exception handler.
All validation errors from the @Valid annotation will return appropriate HTTP 400 responses.
Summary
- Implemented PUT endpoint with validation in
RestaurantController - Used existing error handling for missing restaurants
- Endpoint returns updated restaurant details in response