List Ticket Service Layer
In this lesson, we're going to implement the list ticket service layer.
Repository Method Implementation
The first step is to add a method to the TicketRepository interface that will retrieve tickets for a specific user.
We'll add a method called findByPurchaserId that takes two parameters:
Page<Ticket> findByPurchaserId(UUID purchaserId, Pageable pageable);This method will return a page of tickets, which allows for pagination of results.
Spring Data JPA will automatically implement this method based on the method name, as it understands the relationship between a Ticket and its purchaser.
Service Interface Creation
Next, we'll define the contract for our ticket service by creating the TicketService interface:
public interface TicketService {
Page<Ticket> listTicketsForUser(UUID userId, Pageable pageable);
}This interface declares a single method that will retrieve a page of tickets for a given user ID.
Service Implementation
Finally, we'll create the implementation of our TicketService interface:
@Service
@RequiredArgsConstructor
public class TicketServiceImpl implements TicketService {
private final TicketRepository ticketRepository;
@Override
public Page<Ticket> listTicketsForUser(UUID userId, Pageable pageable) {
return ticketRepository.findByPurchaserId(userId, pageable);
}
}The implementation is straightforward - it simply delegates to the repository method we created earlier.
Summary
- Added the
findByPurchaserIdmethod toTicketRepository - Implemented the
TicketServicewithlistTicketsForUsermethod