Delete Tag Endpoint
Building on our tag management functionality from previous lessons, we'll now implement the ability to delete tags from our blog platform.
This endpoint will enable content organizers to remove unused tags while preventing the deletion of tags that are still associated with posts.
Service Layer Enhancement
The tag deletion process requires careful validation to prevent orphaned content:
public interface TagService {
// ...
void deleteTag(UUID id);
} @Transactional
@Override
public void deleteTag(UUID id) {
tagRepository.findById(id).ifPresent(tag -> {
if(!tag.getPosts().isEmpty()) {
throw new IllegalStateException("Cannot delete tag with posts");
}
tagRepository.deleteById(id);
});
}Controller Implementation
The TagController handles HTTP DELETE requests with proper response codes:
@DeleteMapping(path = "/{id}")
public ResponseEntity<Void> deleteTag(@PathVariable UUID id) {
tagService.deleteTag(id);
return ResponseEntity.noContent().build();
}Summary
- Implemented tag deletion with validation
- Created DELETE endpoint returning appropriate HTTP status codes