From 25d19539b1c5475020733177edcd03d7b48dfac3 Mon Sep 17 00:00:00 2001 From: Geoffrey Kwan Date: Mon, 8 Apr 2024 18:06:46 -0400 Subject: [PATCH] feat(Tag): Prevent creating a duplicate tag --- .../presentation/web/controllers/tag/UserTagController.java | 6 +++++- .../org/wise/portal/service/usertags/UserTagsService.java | 2 ++ .../portal/service/usertags/impl/UserTagsServiceImpl.java | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) 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 ad3d60122..e05cb15cd 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,7 +38,11 @@ public class UserTagController { protected ResponseEntity> createTag(Authentication auth, @RequestBody Map tag) { User user = userService.retrieveUserByUsername(auth.getName()); - UserTag userTag = userTagsService.createTag(user, (String) tag.get("text")); + String tagText = ((String) tag.get("text")).trim(); + if (userTagsService.hasTag(user, tagText)) { + return ResponseEntityGenerator.createError("tagAlreadyExists"); + } + UserTag userTag = userTagsService.createTag(user, tagText); 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 d78a7ea56..02be2ca7b 100644 --- a/src/main/java/org/wise/portal/service/usertags/UserTagsService.java +++ b/src/main/java/org/wise/portal/service/usertags/UserTagsService.java @@ -17,6 +17,8 @@ public interface UserTagsService { Set getTags(User user, Project project); + Boolean hasTag(User user, String tag); + Boolean hasTag(User user, Project project, String tag); 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 ed99f01b4..5d6219539 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 @@ -57,6 +57,12 @@ public Set getTags(User user, Project project) { .collect(Collectors.toSet()); } + @Override + public Boolean hasTag(User user, String tag) { + return getTags(user).stream() + .anyMatch(t -> t.getText().toLowerCase().equals(tag.toLowerCase())); + } + @Override public Boolean hasTag(User user, Project project, String tag) { MutableAclTargetObjectIdentity mutableObjectIdentity = getMutableObjectIdentity(project);