Skip to content

Commit

Permalink
feat(Tag): Implement tag color
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreykwan committed Apr 11, 2024
1 parent 8944476 commit 9d5d60a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 31 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/wise/portal/domain/usertag/UserTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public interface UserTag extends Persistable {

void setText(String text);

String getColor();

void setColor(String color);

User getUser();

Map<String, Object> toMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("id", this.id);
map.put("text", this.text);
map.put("color", this.color);
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected ResponseEntity<List<Map<String, Object>>> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,30 @@ public class TagProjectController {
@Autowired
private UserTagsService userTagsService;

@PutMapping("/projects/tag/{tag}")
@PutMapping("/projects/tag/{tagId}")
protected ResponseEntity<List<Map<String, Object>>> addTagToProjects(Authentication auth,
@RequestBody List<Long> projectIds, @PathVariable("tag") String tag) throws Exception {
@RequestBody List<Long> projectIds, @PathVariable("tagId") Long tagId) throws Exception {
User user = userService.retrieveUserByUsername(auth.getName());
UserTag usertag = getOrCreateTag(user, tag);
UserTag userTag = userTagsService.get(tagId);
List<Project> 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<List<Map<String, Object>>> removeTagFromProjects(Authentication auth,
@RequestParam List<Long> projectIds, @PathVariable("tag") String tag) throws Exception {
@RequestParam List<Long> projectIds, @PathVariable("tagId") Long tagId) throws Exception {
User user = userService.retrieveUserByUsername(auth.getName());
UserTag userTag = getOrCreateTag(user, tag);
UserTag userTag = userTagsService.get(tagId);
List<Project> projects = getProjects(projectIds);
for (Project project : projects) {
userTagsService.removeTag(project, userTag);
}
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<Project> getProjects(List<Long> projectIds) throws ObjectNotFoundException {
List<Project> projects = new ArrayList<Project>();
for (Long projectId : projectIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ public class UserTagController {
protected ResponseEntity<Map<String, Object>> createTag(Authentication auth,
@RequestBody Map<String, Object> 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());
}

Expand All @@ -63,11 +64,12 @@ protected ResponseEntity<Map<String, Object>> updateTag(Authentication auth,
@PathVariable("tagId") Long tagId, @RequestBody Map<String, Object> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserTag> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -58,16 +58,25 @@ public Set<UserTag> 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<UserTag> 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<UserTag> 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
Expand Down

0 comments on commit 9d5d60a

Please sign in to comment.