Create Task List Dto
Now that we have our TaskDto, we need to create a DTO for our TaskList. Like with TaskDto, we'll use a Java record to create an immutable data class that represents a TaskList in our API.
Let's create our TaskListDto in the same com.devtiro.tasks.domain.dto package:
package com.devtiro.tasks.domain.dto;
import java.util.List;
import java.util.UUID;
public record TaskListDto(
UUID id,
String title,
String description,
Integer count,
Double progress,
List<TaskDto> tasks
) { }Let's break down what we've included in our TaskListDto:
id- The UUID identifier of the task listtitle- The task list's titledescription- The task list's descriptioncount- The number of tasks in this listprogress- A number between 0 and 1 representing completion percentagetasks- The list of tasks belonging to this task list
Notice what we've included and excluded compared to our TaskList entity:
Included
- Basic identifiers (
id,title,description) - List of tasks using our TaskDto
- Additional computed fields (
count,progress) that aren't in the entity
Excluded
- No
createdorupdatedtimestamps - these are internal concerns - No JPA annotations - DTOs are pure data carriers
- No bidirectional relationship management - that's handled at the entity level
The count and progress fields are particularly interesting as they're computed values that we'll calculate when converting from entity to DTO. This is a good example of how DTOs can include derived data that's useful for the client but isn't directly stored in our domain model.
By using a record, Java automatically provides us with:
- All-args constructor
- Getter methods for each field
- equals() and hashCode() methods
- toString() method
- Immutability for all fields
This DTO will be used to:
- Return task list data in API responses
- Accept task list data in API requests
- Transfer task list data between application layers
Summary
- Created the
TaskListDtorecord to represent task lists in our API - Included computed fields like count and progress
- Embedded
TaskDtoobjects for the tasks collection - Used Java records for automatic getter methods and immutability
- Excluded internal implementation details like timestamps