Dtos And Mappers
In this lesson we implement the DTOs and mappers required to implement the list event functionality.
Understanding DTOs for Event Listing
Data Transfer Objects (DTOs) help us move data between layers while controlling exactly what information we share. When listing events, we need to consider what information is necessary for display and what should be kept private.
Let's create two DTOs:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ListEventResponseDto {
private UUID id;
private String name;
private LocalDateTime start;
private LocalDateTime end;
private String venue;
private LocalDateTime salesStart;
private LocalDateTime salesEnd;
private EventStatusEnum status;
private List<ListEventTicketTypeResponseDto> ticketTypes = new ArrayList<>();
}@Data
@AllArgsConstructor
@NoArgsConstructor
public class ListEventTicketTypeResponseDto {
private UUID id;
private String name;
private Double price;
private String description;
private Integer totalAvailable;
}Mapping Strategy
We'll update our EventMapper interface to include methods for converting our entities to these new DTOs:
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface EventMapper {
// Existing methods...
ListEventTicketTypeResponseDto toDto(TicketType ticketType);
ListEventResponseDto toListEventResponseDto(Event event);
}Data Transfer Considerations
When designing DTOs for listing events, we've made specific choices about what data to include:
- Included basic event details needed for display (ID, name, dates, venue)
- Included ticket types because they're shown in event listings
- Excluded sensitive or unnecessary data (organizer details, attendees, staff)
- Excluded audit fields (createdAt, updatedAt) as they're not displayed
- Initialized collections to empty lists to prevent null pointer issues
Summary
- Created the
ListEventResponseDto - Create the
ListEventTicketTypeResponseDto - Updated the
EventMapperto map to these DTO classes