Get Ticket Service Layer

In this lesson we'll implement the get ticket functionality in the service layer.

Adding a New Repository Method

Let's start by adding a new method to the TicketRepository interface.

The method will help us find a ticket by both its ID and the purchaser's ID.

This ensures tickets can only be retrieved by their rightful owners.

Optional<Ticket> findByIdAndPurchaserId(UUID id, UUID purchaserId);

Implementing the Service Layer

Now we'll create a method in the TicketService that uses our new repository method.

This method will act as a pass-through to the repository, maintaining the same return type and validation logic.

@Override public Optional<Ticket> getTicketForUser(UUID userId, UUID ticketId) { return ticketRepository.findByIdAndPurchaserId(ticketId, userId); }

Summary

  • Added the findByIdAndPurchaserId method to TicketRepository
  • Implemented the getTicketForUser method in TicketService
© 2026 Devtiro Ltd. All rights reserved