Get Ticket Dto Mapper
In this lesson, we'll implement the DTOs and mappers that we need to implement the get ticket endpoint.
Creating the Get Ticket Response DTO
The GetTicketResponseDto will combine information from the ticket, ticket type, and event entities.
Instead of nesting this information in separate objects, we'll flatten it into a single DTO for simplicity:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GetTicketResponseDto {
private UUID id;
private TicketStatusEnum status;
private Double price;
private String description;
private String eventName;
private String eventVenue;
private LocalDateTime eventStart;
private LocalDateTime eventEnd;
}Implementing the Ticket Mapper
The ticket mapper is responsible for converting between our entities and DTOs.
We'll add a new method to map a ticket entity to our response DTO:
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface TicketMapper {
@Mapping(target = "price", source = "ticket.ticketType.price")
@Mapping(target = "description", source = "ticket.ticketType.description")
@Mapping(target = "eventName", source = "ticket.ticketType.event.name")
@Mapping(target = "eventVenue", source = "ticket.ticketType.event.venue")
@Mapping(target = "eventStart", source = "ticket.ticketType.event.start")
@Mapping(target = "eventEnd", source = "ticket.ticketType.event.end")
GetTicketResponseDto toGetTicketResponseDto(Ticket ticket);
}The @Mapping annotations tell MapStruct how to navigate the relationships between our entities to get the right information.
We can verify our mapper is working by running a clean and compile, which will generate the actual implementation code.
Summary
- Created the
GetTicketResponseDtoclass - Added the
toGetTicketResponseDtomethod toTicketMapper