Event Controller
In this lesson, we'll implement the REST API endpoint for creating events.
Create the Event Controller
The event controller is responsible for handling HTTP requests related to events. Let's create a new controller class with Spring's @RestController annotation:
@RestController
@RequestMapping("/api/v1/events")
@RequiredArgsConstructor
public class EventController {
private final EventMapper eventMapper;
private final EventService eventService;
@PostMapping
public ResponseEntity<CreateEventResponseDto> createEvent(
@AuthenticationPrincipal Jwt jwt,
@Valid @RequestBody CreateEventRequestDto createEventRequestDto) {
// Convert DTO to domain object
CreateEventRequest createEventRequest = eventMapper.fromDto(createEventRequestDto);
// Extract user ID from JWT
UUID userId = UUID.fromString(jwt.getSubject());
// Create the event
Event createdEvent = eventService.createEvent(userId, createEventRequest);
// Convert response to DTO
CreateEventResponseDto createEventResponseDto = eventMapper.toDto(createdEvent);
return new ResponseEntity<>(createEventResponseDto, HttpStatus.CREATED);
}
}Summary
- Created the
EventControllerclass - Implemented create event endpoint