Implement Controller Endpoint
In this lesson, we'll implement a controller endpoint that lets event organizers retrieve the details of a specific event.
Implement the Controller Endpoint
The get event endpoint needs to handle both successful and unsuccessful requests. Let's implement this in our EventController:
@GetMapping(path = "/{eventId}")
public ResponseEntity<GetEventDetailsResponseDto> getEvent(
@AuthenticationPrincipal Jwt jwt,
@PathVariable UUID eventId
) {
// Get the user's ID from the JWT token
UUID userId = parseUserId(jwt);
// Call the service layer and transform the response
return eventService.getEventForOrganizer(userId, eventId)
.map(eventMapper::toGetEventDetailsResponseDto)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}Let's break down what's happening:
- We use
@GetMappingwith a path parameter{eventId}to capture the event identifier - The
@PathVariableannotation maps the URL path parameter to our method parameter - We extract the user ID from the JWT token using our helper method
- We use the service layer to find the event, which returns an
Optional - We chain
mapoperations to convert the event to a DTO and wrap it in a response - If no event is found, we return a 404 Not Found response
Error Handling
Our endpoint handles two main cases:
- When the event exists and belongs to the organizer, it returns HTTP 200 with the event details
- When the event doesn't exist or doesn't belong to the organizer, it returns HTTP 404
This approach follows REST best practices by using standard HTTP status codes to communicate the outcome of the request.
Summary
- Implemented a GET endpoint to retrieve event details by ID
- Used Optional to handle cases where events aren't found