Implement Get Task List
Now that we can list and create task lists, let's implement the ability to retrieve a single task list by its ID.
This functionality is essential for viewing and editing individual task lists. We'll need to add methods to both our service and controller layers.
Updating the Service Interface
First, let's add the get method to our TaskListService interface:
public interface TaskListService {
List<TaskList> listTaskLists();
TaskList createTaskList(TaskList taskList);
Optional<TaskList> getTaskList(UUID id); // Add this method
}We're returning an Optional<TaskList> because the requested task list might not exist.
Implementing the Service Method
Now let's implement the get functionality in TaskListServiceImpl:
@Service
public class TaskListServiceImpl implements TaskListService {
private final TaskListRepository taskListRepository;
public TaskListServiceImpl(TaskListRepository taskListRepository) {
this.taskListRepository = taskListRepository;
}
@Override
public Optional<TaskList> getTaskList(UUID id) {
return taskListRepository.findById(id);
}
}This implementation is straightforward:
- Takes a UUID parameter representing the task list ID
- Delegates directly to the repository's
findByIdmethod - Returns an
Optional<TaskList>which will be empty if no task list exists with that ID
Updating the Controller
Now let's add the get 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;
}
@GetMapping(path = "/{task_list_id}")
public Optional<TaskListDto> getTaskList(
@PathVariable("task_list_id") UUID taskListId) {
return taskListService.getTaskList(taskListId)
.map(taskListMapper::toDto);
}
}Let's break down the key components of this endpoint:
-
URL Path:
@GetMapping(path = "/{task_list_id}")maps this to GET requests with a task list ID in the path- The ID is captured as a path variable
-
Path Variable:
@PathVariable("task_list_id")extracts the ID from the URL- Spring automatically converts the string ID to a UUID
-
Implementation:
- Calls the service to get the task list
- Uses
mapto transform the TaskList to TaskListDto if present - Returns an empty Optional if no task list is found
How It Works Together
When a GET request arrives at /task-lists/{id}:
- Spring extracts the ID from the URL path
- The controller calls
taskListService.getTaskList() - The service delegates to
taskListRepository.findById() - If found:
- The repository returns the TaskList entity
- The controller converts it to a DTO
- Spring serializes the DTO to JSON
- If not found:
- An empty Optional is returned
- Spring converts this to a 200 OK with null body
For example, a GET request to /task-lists/123e4567-e89b-12d3-a456-426614174000 might return:
{
"id": "123e4567-e89b-12d3-a456-426614174000",
"title": "My Task List",
"description": "Important tasks",
"count": 2,
"progress": 0.5,
"tasks": [
{
"id": "789e4567-e89b-12d3-a456-426614174001",
"title": "First Task",
"description": "Do something important",
"dueDate": "2024-11-01T09:00:00",
"priority": "HIGH",
"status": "OPEN"
}
]
}Summary
- Added get functionality to the
TaskListService - Implemented the get endpoint in the controller
- Used
Optionalto handle cases where the task list isn't found - Connected all layers for a complete get operation
- Maintained consistent error handling through our global exception handler