Validate Ticket Endpoint
In this lesson we're going to implement the validate ticket endpoint.
Creating the Controller
Let's create a new controller to handle ticket validation requests. The controller will be responsible for validating tickets through two methods - QR code scanning and manual validation.
First, we'll create a new class called TicketValidationController with the required annotations:
@RestController
@RequestMapping(path = "/api/v1/ticket-validations")
@RequiredArgsConstructor
public class TicketValidationController {
private final TicketValidationService ticketValidationService;
private final TicketValidationMapper ticketValidationMapper;
@PostMapping
public ResponseEntity<TicketValidationResponseDto> validateTicket(
@RequestBody TicketValidationRequestDto ticketValidationRequestDto
){
TicketValidationMethod method = ticketValidationRequestDto.getMethod();
TicketValidation ticketValidation;
if(TicketValidationMethod.MANUAL.equals(method)) {
ticketValidation = ticketValidationService.validateTicketManually(
ticketValidationRequestDto.getId());
} else {
ticketValidation = ticketValidationService.validateTicketByQrCode(
ticketValidationRequestDto.getId()
);
}
return ResponseEntity.ok(
ticketValidationMapper.toTicketValidationResponseDto(ticketValidation)
);
}
}Securing the Endpoint
To ensure only staff members can validate tickets, we need to secure the endpoint with the appropriate role:
http
.authorizeHttpRequests(authorize ->
authorize
.requestMatchers("/api/v1/ticket-validations").hasRole("STAFF")
// Catch all rule
.anyRequest().authenticated())Summary
- Implemented the validate ticket endpoint