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 add and remove project tag endpoints #260

Conversation

geoffreykwan
Copy link
Member

@geoffreykwan geoffreykwan commented Mar 12, 2024

Changes

  • Added endpoint to retrieve user tags
  • Added endpoint to add tag to project
  • Added endpoint to remove tag from project

Test

Copy link
Member

@hirokiterashima hirokiterashima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

The body of TagProjectController.addTagToProjects() and TagProjectController.removeTagFromProjects() have some duplicate code. Can you extract common code to another method before you merge?

   User user = userService.retrieveUserByUsername(auth.getName());
   UserTag userTag = getOrCreateTag(user, tag);
   List<Project> projects = getProjects(projectIds);

@geoffreykwan
Copy link
Member Author

How would you extract the common code?

@hirokiterashima
Copy link
Member

I would introduce a new private method like this:

  @PutMapping("/projects/tag/{tag}")
  protected ResponseEntity<List<Map<String, Object>>> addTagToProjects(Authentication auth,
      @RequestBody List<Long> projectIds, @PathVariable("tag") String tag) throws Exception {
    return addTagToProjects(auth, projectIds, tag, true);
  }

  @DeleteMapping("/projects/tag/{tag}")
  protected ResponseEntity<List<Map<String, Object>>> removeTagFromProjects(Authentication auth,
      @RequestParam List<Long> projectIds, @PathVariable("tag") String tag) throws Exception {
    return addTagToProjects(auth, projectIds, tag, false);
  }

  private ResponseEntity<List<Map<String, Object>>> addTagToProjects(Authentication auth,
      List<Long> projectIds, String tag, boolean add) throws ObjectNotFoundException {
    User user = userService.retrieveUserByUsername(auth.getName());
    UserTag userTag = getOrCreateTag(user, tag);
    List<Project> projects = getProjects(projectIds);
    for (Project project : projects) {
      if (add) {
        userTagsService.applyTag(project, userTag);
      } else {
        userTagsService.removeTag(project, userTag);
      }
    }
    return ResponseEntityGenerator.createSuccess(createProjectsResponse(user, projects));
  }

@geoffreykwan geoffreykwan merged commit bf51065 into issue-259-implement-unit-tagging Mar 18, 2024
0 of 2 checks passed
@geoffreykwan geoffreykwan deleted the add-remove-project-tag-endpoints branch March 18, 2024 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants