Skip to content

Commit

Permalink
feat(Tag): Implement delete tag endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreykwan committed Apr 5, 2024
1 parent 70a5cb7 commit 8d4a2ec
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,4 +63,13 @@ protected ResponseEntity<Map<String, Object>> updateTag(Authentication auth,
userTagsService.updateTag(user, userTag);
return ResponseEntityGenerator.createSuccess(userTag.toMap());
}

@DeleteMapping("/user/tag/{tagId}")
protected ResponseEntity<Map<String, Object>> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface UserTagsService {
List<UserTag> getTags(User user);

void updateTag(User user, UserTag tag);

void deleteTag(User user, UserTag tag);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +27,9 @@ public class UserTagsServiceImpl implements UserTagsService {
@Autowired
private AclTargetObjectIdentityDao<MutableAclTargetObjectIdentity> aclTargetObjectIdentityDao;

@Autowired
private ProjectService projectService;

@Autowired
private UserTagsDao<UserTag> userTagsDao;

Expand Down Expand Up @@ -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<Project> projects, UserTag userTag) {
for (Project project : projects) {
MutableAclTargetObjectIdentity mutableObjectIdentity = getMutableObjectIdentity(project);
mutableObjectIdentity.getTags().remove(userTag);
aclTargetObjectIdentityDao.save(mutableObjectIdentity);
}
}
}

0 comments on commit 8d4a2ec

Please sign in to comment.