Service Layer
Let's code the delete task business logic in our service layer.
Update the Task Service
In TaskService we'll add our last method definition:
/**
* Deletes the specified task.
* Does not throw an exception when the specified task does not exist.
*
* @param taskId The ID of the task to delete.
*/
void deleteTask(UUID taskId);This method takes the ID of the task to delete and doesn't return anything.
You may be wondering what we should do when the user tries to delete a task that doesn't exist. In this case, we won't throw an exception.
Think of it this way, regardless of whether the task existed in the first place, after calling this method it definitely won't exist.
Perhaps a bit counterintuitive, but this is the way a lot of REST APIs implement delete, so we'll follow in our service layer too!
Now to implement the delete task method.
Implement the Delete Task Method
Over in TaskServiceImpl, let's implement our new method:
@Transactional
@Override
public void deleteTask(UUID taskId) {
taskRepository.deleteById(taskId);
}The implementation is just a single line! We're delegating all of the responsibility of deleting a task to the task repository. We sometimes call this a "pass through" implementation. Another example of all the functionality repositories offer out of the box!
That's the service layer complete, let's move on to the presentation layer.
Summary
- Added the
deleteTaskmethod to theTaskServiceinterface. - Implemented the
deleteTaskmethod.