Ticket Purchase Endpoint
In this lesson, we'll implement the ticket purchase endpoint.
Creating the Controller
Let's create a TicketTypeController that will handle ticket purchase requests:
@RestController
@RequiredArgsConstructor
@RequestMapping(path = "/api/v1/events/{eventId}/ticket-types")
public class TicketTypeController {
private final TicketTypeService ticketTypeService;
@PostMapping(path = "/{ticketTypeId}/tickets")
public ResponseEntity<Void> purchaseTicket(
@AuthenticationPrincipal Jwt jwt,
@PathVariable UUID ticketTypeId
) {
// Purchase the ticket using the service
ticketTypeService.purchaseTicket(parseUserId(jwt), ticketTypeId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}Understanding the Implementation
The purchase ticket endpoint follows RESTful conventions and includes several important components:
-
The endpoint URL structure follows the pattern
/api/v1/events/{eventId}/ticket-types/{ticketTypeId}/tickets -
We use
@PostMappingsince we're creating a new ticket resource -
The controller accepts a JWT token to identify the purchaser and a ticket type ID to specify which type of ticket to purchase
-
We return HTTP 204 (No Content) on success since we don't need to send any information back to the client
Summary
- Implemented the purchase ticket endpoint