Service Layer

In this lesson, we'll implement the get published event functionality in our service layer.

Service Layer Implementation

Let's add the getPublishedEvent method to our event service interface:

public interface EventService { // Other methods... Optional<Event> getPublishedEvent(UUID id); }

In the implementation class, we'll use our repository to find events that match both the ID and published status:

@Override public Optional<Event> getPublishedEvent(UUID id) { // Only return events that are both published and match the ID return eventRepository.findByIdAndStatus(id, EventStatusEnum.PUBLISHED); }

Repository Extension

To support this functionality, we need to add a custom query method to our repository:

public interface EventRepository extends JpaRepository<Event, UUID> { // Other methods... Optional<Event> findByIdAndStatus(UUID id, EventStatusEnum status); }

This method follows Spring Data JPA's method naming conventions, creating a query that filters by both ID and status.

Summary

  • Added the findByIdAndStatus method to the EventRepository
  • Added the getPublishedEvent method to the EventService interface
  • Implemented the getPublishedEvent method in the EventServiceImpl class
© 2026 Devtiro Ltd. All rights reserved