From 8d4a2ec2e874960ff1c80084dd543ed01dc83b68 Mon Sep 17 00:00:00 2001 From: Geoffrey Kwan Date: Fri, 5 Apr 2024 15:08:05 -0400 Subject: [PATCH] feat(Tag): Implement delete tag endpoint --- .../controllers/tag/UserTagController.java | 10 +++++++++ .../service/usertags/UserTagsService.java | 2 ++ .../usertags/impl/UserTagsServiceImpl.java | 22 +++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/src/main/java/org/wise/portal/presentation/web/controllers/tag/UserTagController.java b/src/main/java/org/wise/portal/presentation/web/controllers/tag/UserTagController.java index ee204e5bb..ad3d60122 100644 --- a/src/main/java/org/wise/portal/presentation/web/controllers/tag/UserTagController.java +++ b/src/main/java/org/wise/portal/presentation/web/controllers/tag/UserTagController.java @@ -9,6 +9,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.access.annotation.Secured; import org.springframework.security.core.Authentication; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -62,4 +63,13 @@ protected ResponseEntity> updateTag(Authentication auth, userTagsService.updateTag(user, userTag); return ResponseEntityGenerator.createSuccess(userTag.toMap()); } + + @DeleteMapping("/user/tag/{tagId}") + protected ResponseEntity> deleteTag(Authentication auth, + @PathVariable("tagId") Long tagId) { + User user = userService.retrieveUserByUsername(auth.getName()); + UserTag tag = userTagsService.get(tagId); + userTagsService.deleteTag(user, tag); + return ResponseEntityGenerator.createSuccess(tag.toMap()); + } } diff --git a/src/main/java/org/wise/portal/service/usertags/UserTagsService.java b/src/main/java/org/wise/portal/service/usertags/UserTagsService.java index 35b546036..d78a7ea56 100644 --- a/src/main/java/org/wise/portal/service/usertags/UserTagsService.java +++ b/src/main/java/org/wise/portal/service/usertags/UserTagsService.java @@ -28,4 +28,6 @@ public interface UserTagsService { List getTags(User user); void updateTag(User user, UserTag tag); + + void deleteTag(User user, UserTag tag); } diff --git a/src/main/java/org/wise/portal/service/usertags/impl/UserTagsServiceImpl.java b/src/main/java/org/wise/portal/service/usertags/impl/UserTagsServiceImpl.java index 0eb890e72..ed99f01b4 100644 --- a/src/main/java/org/wise/portal/service/usertags/impl/UserTagsServiceImpl.java +++ b/src/main/java/org/wise/portal/service/usertags/impl/UserTagsServiceImpl.java @@ -18,6 +18,7 @@ import org.wise.portal.domain.user.User; import org.wise.portal.domain.usertag.UserTag; import org.wise.portal.domain.usertag.impl.UserTagImpl; +import org.wise.portal.service.project.ProjectService; import org.wise.portal.service.usertags.UserTagsService; @Service @@ -26,6 +27,9 @@ public class UserTagsServiceImpl implements UserTagsService { @Autowired private AclTargetObjectIdentityDao aclTargetObjectIdentityDao; + @Autowired + private ProjectService projectService; + @Autowired private UserTagsDao userTagsDao; @@ -99,4 +103,22 @@ public void updateTag(User user, UserTag tag) { throw new AccessDeniedException("User does not have permission to update tag."); } } + + public void deleteTag(User user, UserTag tag) { + if (user.equals(tag.getUser())) { + removeTagFromProjects(projectService.getProjectList(user), tag); + removeTagFromProjects(projectService.getSharedProjectList(user), tag); + userTagsDao.delete(tag); + } else { + throw new AccessDeniedException("User does not have permission to delete tag."); + } + } + + private void removeTagFromProjects(List projects, UserTag userTag) { + for (Project project : projects) { + MutableAclTargetObjectIdentity mutableObjectIdentity = getMutableObjectIdentity(project); + mutableObjectIdentity.getTags().remove(userTag); + aclTargetObjectIdentityDao.save(mutableObjectIdentity); + } + } }