From 9d5d60ada66f91e5db24c967d0f58d70f04ccd3e Mon Sep 17 00:00:00 2001 From: Geoffrey Kwan Date: Thu, 11 Apr 2024 11:32:24 -0400 Subject: [PATCH] feat(Tag): Implement tag color --- .../wise/portal/domain/usertag/UserTag.java | 4 ++++ .../domain/usertag/impl/UserTagImpl.java | 7 +++++- .../archive/ArchiveProjectController.java | 2 +- .../controllers/tag/TagProjectController.java | 22 ++++++------------ .../controllers/tag/UserTagController.java | 10 ++++---- .../service/usertags/UserTagsService.java | 8 ++++--- .../usertags/impl/UserTagsServiceImpl.java | 23 +++++++++++++------ 7 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/wise/portal/domain/usertag/UserTag.java b/src/main/java/org/wise/portal/domain/usertag/UserTag.java index 7531b43b6..2164df832 100644 --- a/src/main/java/org/wise/portal/domain/usertag/UserTag.java +++ b/src/main/java/org/wise/portal/domain/usertag/UserTag.java @@ -11,6 +11,10 @@ public interface UserTag extends Persistable { void setText(String text); + String getColor(); + + void setColor(String color); + User getUser(); Map toMap(); diff --git a/src/main/java/org/wise/portal/domain/usertag/impl/UserTagImpl.java b/src/main/java/org/wise/portal/domain/usertag/impl/UserTagImpl.java index 90fa353b5..fb3f87a0d 100644 --- a/src/main/java/org/wise/portal/domain/usertag/impl/UserTagImpl.java +++ b/src/main/java/org/wise/portal/domain/usertag/impl/UserTagImpl.java @@ -47,18 +47,23 @@ public class UserTagImpl implements UserTag { @Column(name = "text") private String text; + @Column(name = "color") + private String color; + public UserTagImpl() { } - public UserTagImpl(User user, String text) { + public UserTagImpl(User user, String text, String color) { this.user = user; this.text = text; + this.color = color; } public Map toMap() { Map map = new HashMap<>(); map.put("id", this.id); map.put("text", this.text); + map.put("color", this.color); return map; } } \ No newline at end of file diff --git a/src/main/java/org/wise/portal/presentation/web/controllers/archive/ArchiveProjectController.java b/src/main/java/org/wise/portal/presentation/web/controllers/archive/ArchiveProjectController.java index dd431f02f..3a26d4426 100644 --- a/src/main/java/org/wise/portal/presentation/web/controllers/archive/ArchiveProjectController.java +++ b/src/main/java/org/wise/portal/presentation/web/controllers/archive/ArchiveProjectController.java @@ -67,7 +67,7 @@ protected ResponseEntity>> archiveProjects(Authenticati private UserTag getOrCreateArchivedTag(User user) { UserTag archivedTag = userTagsService.get(user, ARCHIVED_TAG); if (archivedTag == null) { - archivedTag = userTagsService.createTag(user, ARCHIVED_TAG); + archivedTag = userTagsService.createTag(user, ARCHIVED_TAG, null); } return archivedTag; } diff --git a/src/main/java/org/wise/portal/presentation/web/controllers/tag/TagProjectController.java b/src/main/java/org/wise/portal/presentation/web/controllers/tag/TagProjectController.java index 277d600c9..10be2bc0f 100644 --- a/src/main/java/org/wise/portal/presentation/web/controllers/tag/TagProjectController.java +++ b/src/main/java/org/wise/portal/presentation/web/controllers/tag/TagProjectController.java @@ -39,23 +39,23 @@ public class TagProjectController { @Autowired private UserTagsService userTagsService; - @PutMapping("/projects/tag/{tag}") + @PutMapping("/projects/tag/{tagId}") protected ResponseEntity>> addTagToProjects(Authentication auth, - @RequestBody List projectIds, @PathVariable("tag") String tag) throws Exception { + @RequestBody List projectIds, @PathVariable("tagId") Long tagId) throws Exception { User user = userService.retrieveUserByUsername(auth.getName()); - UserTag usertag = getOrCreateTag(user, tag); + UserTag userTag = userTagsService.get(tagId); List projects = getProjects(projectIds); for (Project project : projects) { - userTagsService.applyTag(project, usertag); + userTagsService.applyTag(project, userTag); } return ResponseEntityGenerator.createSuccess(createProjectsResponse(user, projects)); } - @DeleteMapping("/projects/tag/{tag}") + @DeleteMapping("/projects/tag/{tagId}") protected ResponseEntity>> removeTagFromProjects(Authentication auth, - @RequestParam List projectIds, @PathVariable("tag") String tag) throws Exception { + @RequestParam List projectIds, @PathVariable("tagId") Long tagId) throws Exception { User user = userService.retrieveUserByUsername(auth.getName()); - UserTag userTag = getOrCreateTag(user, tag); + UserTag userTag = userTagsService.get(tagId); List projects = getProjects(projectIds); for (Project project : projects) { userTagsService.removeTag(project, userTag); @@ -63,14 +63,6 @@ protected ResponseEntity>> removeTagFromProjects(Authen return ResponseEntityGenerator.createSuccess(createProjectsResponse(user, projects)); } - private UserTag getOrCreateTag(User user, String tag) { - UserTag archivedTag = userTagsService.get(user, tag); - if (archivedTag == null) { - archivedTag = userTagsService.createTag(user, tag); - } - return archivedTag; - } - private List getProjects(List projectIds) throws ObjectNotFoundException { List projects = new ArrayList(); for (Long projectId : projectIds) { 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 7b06e435f..98a2765a8 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 @@ -38,11 +38,12 @@ public class UserTagController { protected ResponseEntity> createTag(Authentication auth, @RequestBody Map tag) { User user = userService.retrieveUserByUsername(auth.getName()); - String tagText = ((String) tag.get("text")).trim(); - if (userTagsService.hasTag(user, tagText)) { + String text = ((String) tag.get("text")).trim(); + if (userTagsService.hasTag(user, text)) { return ResponseEntityGenerator.createError("tagAlreadyExists"); } - UserTag userTag = userTagsService.createTag(user, tagText); + String color = ((String) tag.get("color")).trim(); + UserTag userTag = userTagsService.createTag(user, text, color); return ResponseEntityGenerator.createSuccess(userTag.toMap()); } @@ -63,11 +64,12 @@ protected ResponseEntity> updateTag(Authentication auth, @PathVariable("tagId") Long tagId, @RequestBody Map tag) { User user = userService.retrieveUserByUsername(auth.getName()); String tagText = ((String) tag.get("text")).trim(); - if (userTagsService.hasTag(user, tagText)) { + if (userTagsService.hasTag(user, tagText, tagId)) { return ResponseEntityGenerator.createError("tagAlreadyExists"); } UserTag userTag = userTagsService.get(tagId); userTag.setText(tagText); + userTag.setColor((String) tag.get("color")); userTagsService.updateTag(user, userTag); return ResponseEntityGenerator.createSuccess(userTag.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 02be2ca7b..c78443087 100644 --- a/src/main/java/org/wise/portal/service/usertags/UserTagsService.java +++ b/src/main/java/org/wise/portal/service/usertags/UserTagsService.java @@ -13,13 +13,15 @@ public interface UserTagsService { UserTag get(Long id); - UserTag createTag(User user, String tag); + UserTag createTag(User user, String text, String color); Set getTags(User user, Project project); - Boolean hasTag(User user, String tag); + Boolean hasTag(User user, String text); - Boolean hasTag(User user, Project project, String tag); + Boolean hasTag(User user, String text, Long idToIgnore); + + Boolean hasTag(User user, Project project, String text); void applyTag(Project project, 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 5d6219539..1c591e681 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 @@ -44,8 +44,8 @@ public UserTag get(Long id) { } @Override - public UserTag createTag(User user, String text) { - UserTag userTag = new UserTagImpl(user, text); + public UserTag createTag(User user, String text, String color) { + UserTag userTag = new UserTagImpl(user, text, color); userTagsDao.save(userTag); return userTag; } @@ -58,16 +58,25 @@ public Set getTags(User user, Project project) { } @Override - public Boolean hasTag(User user, String tag) { - return getTags(user).stream() - .anyMatch(t -> t.getText().toLowerCase().equals(tag.toLowerCase())); + public Boolean hasTag(User user, String text) { + return hasTag(user, text, null); } @Override - public Boolean hasTag(User user, Project project, String tag) { + public Boolean hasTag(User user, String text, Long idToIgnore) { + List tags = getTags(user); + if (idToIgnore != null) { + tags.remove(this.get(idToIgnore)); + } + return tags.stream().anyMatch(t -> t.getText().toLowerCase().equals(text.toLowerCase())); + } + + @Override + public Boolean hasTag(User user, Project project, String text) { MutableAclTargetObjectIdentity mutableObjectIdentity = getMutableObjectIdentity(project); Set tags = mutableObjectIdentity.getTags(); - return tags.stream().anyMatch(t -> t.getUser().equals(user) && t.getText().equals(tag)); + return tags.stream() + .anyMatch(t -> t.getUser().equals(user) && t.getText().equals(text.toLowerCase())); } @Override