Implement Update Task List

Now that we can create and retrieve task lists, let's implement the ability to update existing task lists. This will allow users to modify the title and description of their task lists.

We'll need to add methods to both our service and controller layers.

Updating the Service Interface

First, let's add the update method to our TaskListService interface:

public interface TaskListService { List<TaskList> listTaskLists(); TaskList createTaskList(TaskList taskList); Optional<TaskList> getTaskList(UUID id); TaskList updateTaskList(UUID taskListId, TaskList taskList); // Add this method }

Implementing the Service Method

Now let's implement the update functionality in TaskListServiceImpl:

@Service public class TaskListServiceImpl implements TaskListService { private final TaskListRepository taskListRepository; public TaskListServiceImpl(TaskListRepository taskListRepository) { this.taskListRepository = taskListRepository; } @Override public TaskList updateTaskList(UUID taskListId, TaskList taskList) { if (null == taskList.getId()) { throw new IllegalArgumentException("Task list must have an ID!"); } if (!Objects.equals(taskList.getId().toString(), taskListId.toString())) { throw new IllegalArgumentException( "Attempting to change task list ID, this is not permitted!"); } TaskList existingTaskList = taskListRepository.findById(taskListId) .orElseThrow(() -> new IllegalStateException("Task list not found!") ); existingTaskList.setTitle(taskList.getTitle()); existingTaskList.setDescription(taskList.getDescription()); existingTaskList.setUpdated(LocalDateTime.now()); return taskListRepository.save(existingTaskList); } }

Let's examine the key aspects of this implementation:

  1. Input Validation:

    • Checks that the task list has an ID
    • Verifies the ID in the path matches the ID in the task list
    • Ensures the task list exists in the database
  2. Update Logic:

    • Updates only the modifiable fields (title and description)
    • Updates the 'updated' timestamp
    • Preserves other fields like 'created' timestamp and tasks
  3. Error Handling:

    • Throws IllegalArgumentException for invalid input
    • Throws IllegalStateException if task list doesn't exist
    • These will be caught by our global exception handler

Updating the Controller

Now let's add the update endpoint to our TaskListController:

@RestController @RequestMapping(path = "/task-lists") public class TaskListController { private final TaskListService taskListService; private final TaskListMapper taskListMapper; public TaskListController(TaskListService taskListService, TaskListMapper taskListMapper) { this.taskListService = taskListService; this.taskListMapper = taskListMapper; } @PutMapping(path = "/{task_list_id}") public TaskListDto updateTaskList( @PathVariable("task_list_id") UUID taskListId, @RequestBody TaskListDto taskListDto) { TaskList updatedTaskList = taskListService.updateTaskList( taskListId, taskListMapper.fromDto(taskListDto) ); return taskListMapper.toDto(updatedTaskList); } }

Let's break down the key components:

  1. URL Mapping:

    • @PutMapping maps this to HTTP PUT requests
    • Path variable captures the task list ID from the URL
  2. Parameters:

    • @PathVariable extracts the ID from the URL
    • @RequestBody deserializes the request body into a TaskListDto
  3. Implementation:

    • Converts the incoming DTO to an entity
    • Calls the service to update the task list
    • Converts the result back to a DTO for the response

How It Works Together

When a PUT request arrives at /task-lists/{id}:

  1. Spring extracts the ID from the URL path
  2. The request body is deserialized into a TaskListDto
  3. The controller converts the DTO to a TaskList entity
  4. The service validates the input and updates the task list
  5. The repository saves the changes to the database
  6. The controller converts the updated entity back to a DTO
  7. Spring serializes the DTO to JSON for the response

For example, a PUT request to /task-lists/123e4567-e89b-12d3-a456-426614174000 might look like:

{ "id": "123e4567-e89b-12d3-a456-426614174000", "title": "Updated Title", "description": "Updated description" }

And receive a response like:

{ "id": "123e4567-e89b-12d3-a456-426614174000", "title": "Updated Title", "description": "Updated description", "count": 2, "progress": 0.5, "tasks": [...] }

Summary

  • Added update functionality to the TaskListService
  • Implemented comprehensive validation in the service layer
  • Added the PUT endpoint to update task lists
  • Maintained data integrity by validating IDs match
© 2026 Devtiro Ltd. All rights reserved