Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Tag): Implement delete tag endpoint #266

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
Loading