Delete Event Service

In this lesson, we'll implement the deleteEventForOrganizer method in our service layer, allowing event organizers to delete their events from the system.

Service Implementation

Let's break down the implementation of the delete event functionality:

void deleteEventForOrganizer(UUID organizerId, UUID eventId);
@Override @Transactional public void deleteEventForOrganizer(UUID organizerId, UUID id) { // Get the event and delete it if found getEventForOrganizer(organizerId, id).ifPresent(eventRepository::delete); }

This implementation:

  • Uses the @Transactional annotation to ensure database operations are atomic
  • Leverages the existing getEventForOrganizer method to verify ownership
  • Only deletes the event if it exists and belongs to the specified organizer
  • Returns void, silently handling cases where the event doesn't exist

The code follows a simple but effective pattern:

  1. Reuses the existing getEventForOrganizer method which checks both existence and ownership
  2. Uses the ifPresent method to only execute the delete operation if an event is found
  3. Passes a method reference to eventRepository::delete for clean, functional programming style

Security Considerations

The delete operation is secure because:

  • It verifies the organizer owns the event before deletion
  • Uses the same authorization check as other event operations
  • Operates within a transaction to maintain data consistency
  • Prevents unauthorized users from deleting events they don't own

Error Handling

The current implementation takes a silent failure approach when:

  • The event doesn't exist
  • The organizer doesn't own the event
  • The event ID is invalid

This approach might need to be revisited if explicit error feedback becomes a requirement.

Summary

  • Added the deleteEventForOrganizer method to the service layer
© 2026 Devtiro Ltd. All rights reserved