Update Task Endpoint
Now for the penultimate REST API endpoint. Let's implement the update task endpoint!
Implement the Update Task Endpoint
Let's add the following method to the TaskController class:
/**
* Updates the specified task with the provided information.
*
* @param taskId The ID of the task to update.
* @param updateTaskRequestDto The request DTO used to update the task.
* @return A representation of the updated task with an HTTP 200.
*/
@PutMapping(path = "/{taskId}")
public ResponseEntity<TaskDto> updateTask(
@PathVariable UUID taskId,
@Valid @RequestBody UpdateTaskRequestDto updateTaskRequestDto) {
// Map the UpdateTaskRequestDto to an UpdateTaskRequest.
UpdateTaskRequest updateTaskRequest = taskMapper.fromDto(
updateTaskRequestDto
);
// Pass the UpdateTaskRequest to the TaskService's updateTask method.
Task updatedTask = taskService.updateTask(taskId, updateTaskRequest);
// Map the Task to a TaskDto.
TaskDto taskMapperDto = taskMapper.toDto(updatedTask);
// Return the TaskDto with an HTTP 200.
return ResponseEntity.ok(taskMapperDto);
}The @PutMapping annotation specifies the HTTP PUT method and its
argument allows us to extract the task's ID from the URL path.
The argument string specifies the variable {taskId}. This must match the
name of the argument annotated with a @PathVariable. The framework takes care
of extracting the value from the URL path.
You may notice we're following a similar pattern to the create task endpoint.
But we're not ready to test out the update endpoint quite yet. We must handle
the TaskNotFoundException first!
Summary
- Implemented the
updateTaskmethod on theTaskController.