Update Event Service

In this lesson, we'll implement the update event functionality in the service layer, which allows event organizers to modify existing events and their associated ticket types.

Service Layer Implementation

The update functionality needs to handle both the event details and its ticket types, ensuring data consistency and proper validation.

Let's implement the updateEventForOrganizer method in our service:

@Override @Transactional public Event updateEventForOrganizer(UUID organizerId, UUID id, UpdateEventRequest event) { if(null == event.getId()) { throw new EventUpdateException("Event ID cannot be null"); } if(!id.equals(event.getId())) { throw new EventUpdateException("Cannot update the ID of an event"); } Event existingEvent = eventRepository .findByIdAndOrganizerId(id, organizerId) .orElseThrow(() -> new EventNotFoundException( String.format("Event with ID '%s' does not exist", id)) ); existingEvent.setName(event.getName()); existingEvent.setStart(event.getStart()); existingEvent.setEnd(event.getEnd()); existingEvent.setVenue(event.getVenue()); existingEvent.setSalesStart(event.getSalesStart()); existingEvent.setSalesEnd(event.getSalesEnd()); existingEvent.setStatus(event.getStatus()); Set<UUID> requestTicketTypeIds = event.getTicketTypes() .stream() .map(UpdateTicketTypeRequest::getId) .filter(Objects::nonNull) .collect(Collectors.toSet()); existingEvent.getTicketTypes().removeIf(existingTicketType -> !requestTicketTypeIds.contains(existingTicketType.getId()) ); Map<UUID, TicketType> existingTicketTypesIndex = existingEvent.getTicketTypes().stream() .collect(Collectors.toMap(TicketType::getId, Function.identity())); for(UpdateTicketTypeRequest ticketType : event.getTicketTypes()) { if(null == ticketType.getId()) { // Create TicketType ticketTypeToCreate = new TicketType(); ticketTypeToCreate.setName(ticketType.getName()); ticketTypeToCreate.setPrice(ticketType.getPrice()); ticketTypeToCreate.setDescription(ticketType.getDescription()); ticketTypeToCreate.setTotalAvailable(ticketType.getTotalAvailable()); ticketTypeToCreate.setEvent(existingEvent); existingEvent.getTicketTypes().add(ticketTypeToCreate); } else if(existingTicketTypesIndex.containsKey(ticketType.getId())) { // Update TicketType existingTicketType = existingTicketTypesIndex.get(ticketType.getId()); existingTicketType.setName(ticketType.getName()); existingTicketType.setPrice(ticketType.getPrice()); existingTicketType.setDescription(ticketType.getDescription()); existingTicketType.setTotalAvailable(ticketType.getTotalAvailable()); } else { throw new TicketTypeNotFoundException(String.format( "Ticket type with ID '%s' does not exist", ticketType.getId() )); } } return eventRepository.save(existingEvent); }

Handle Orphaned Types

We'll also need to update our Event entity to handle orphaned types:

@Entity @Table(name = "events") @Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder public class Event { // ... @OneToMany(mappedBy = "event", cascade = CascadeType.ALL, orphanRemoval = true) private List<TicketType> ticketTypes = new ArrayList<>(); //... }

Summary

  • Implemented the updateEventForOrganizer method in the EventServiceImpl class
  • Updated the Event class to handle orphaned TicketTypes
© 2026 Devtiro Ltd. All rights reserved