Dto Mapper
Let's create the DTO and mapper method we'll need for the update task endpoint.
Create the Update Task Request DTO
We've already created the UpdateTaskRequest class in the service layer.
Let's create its counterpart DTO for use in the presentation layer.
We create the UpdateTaskRequestDto class in com.devtiro.task.domain.dto:
/**
* A DTO modelling a request to update an existing task.
* This class is owned by the presentation layer.
*
* @param title The title of the task to update.
* @param description The description of the task to update.
* @param dueDate The date the task is due.
* @param status The status of the task to update.
* @param priority The priority of the task to update.
*/
public record UpdateTaskRequestDto(
@NotBlank(message = ERROR_MESSAGE_TITLE_LENGTH)
@Length(max = 255, message = ERROR_MESSAGE_TITLE_LENGTH)
String title,
@Length(max = 1000, message = ERROR_MESSAGE_DESCRIPTION_LENGTH)
@Nullable
String description,
@FutureOrPresent(message = ERROR_MESSAGE_DUE_DATE_FUTURE)
@Nullable
LocalDate dueDate,
@NotNull(message = ERROR_MESSAGE_STATUS)
TaskStatus status,
@NotNull(message = ERROR_MESSAGE_PRIORITY)
TaskPriority priority) {
private static final String ERROR_MESSAGE_TITLE_LENGTH =
"Title must be between 1 and 255 characters";
private static final String ERROR_MESSAGE_DESCRIPTION_LENGTH =
"Description must be less than 1000 characters";
private static final String ERROR_MESSAGE_DUE_DATE_FUTURE =
"Due date must be in the future";
private static final String ERROR_MESSAGE_STATUS =
"Status must be provided";
private static final String ERROR_MESSAGE_PRIORITY =
"Task priority must be provided";
}The UpdateTaskRequestDto class has all the same fields as UpdateTaskRequest.
However, the DTO has validation annotations where the service layer class does
not.
Now for the mapper method.
Update the Task Mapper Interface
We need to add just a single method to the TaskMapper interface:
UpdateTaskRequest fromDto(UpdateTaskRequestDto dto);This method is responsible for mapping the DTO we just created,
UpdateTaskRequestDto, into an UpdateTaskRequest we can pass to the service
layer.
Let's implement this new method!
Implement the Task Mapper Interface
Over in TaskMapperImpl we have our new method to implement:
@Override
public UpdateTaskRequest fromDto(UpdateTaskRequestDto dto) {
return new UpdateTaskRequest(
dto.title(),
dto.description(),
dto.dueDate(),
dto.status(),
dto.priority()
);
}With that, we now have everything we need to implement the update task endpoint.
Summary
- Created the
UpdateTaskRequestDtoclass. - Updated the
TaskMapperinterface. - Implemented the new
TaskMappermethod.