Skip to content

Commit

Permalink
feat(Tag): Implement update tag endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreykwan committed Mar 14, 2024
1 parent c3e64de commit 1b4b788
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

@Entity
@Table(name = UserTagImpl.DATA_STORE_NAME)
@Getter
@Setter
public class UserTagImpl implements UserTag {

@Transient
Expand All @@ -32,19 +34,13 @@ public class UserTagImpl implements UserTag {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Getter
@Setter
private Long id = null;

@ManyToOne(targetEntity = UserImpl.class, fetch = FetchType.LAZY)
@JoinColumn(name = "users_fk", nullable = false)
@Getter
@Setter
@JsonIgnore
private User user;

@Getter
@Setter
@Column(name = "text")
private String text;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wise.portal.domain.user.User;
import org.wise.portal.domain.usertag.UserTag;
import org.wise.portal.presentation.web.response.ResponseEntityGenerator;
import org.wise.portal.service.user.UserService;
import org.wise.portal.service.usertags.UserTagsService;
Expand All @@ -39,4 +43,14 @@ protected ResponseEntity<List<Map<String, Object>>> getTags(Authentication auth)
}).collect(Collectors.toList());
return ResponseEntityGenerator.createSuccess(tags);
}

@PutMapping("/user/tag/{tagId}")
protected ResponseEntity<Map<String, Object>> updateTag(Authentication auth,
@PathVariable("tagId") Long tagId, @RequestBody Map<String, Object> tag) {
User user = userService.retrieveUserByUsername(auth.getName());
UserTag userTag = userTagsService.get(tagId);
userTag.setText((String) tag.get("text"));
userTagsService.updateTag(user, userTag);
return ResponseEntityGenerator.createSuccess(tag);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface UserTagsService {

UserTag get(User user, String text);

UserTag get(Long id);

UserTag createTag(User user, String tag);

Set<UserTag> getTags(User user, Project project);
Expand All @@ -24,4 +26,6 @@ public interface UserTagsService {
List<String> getTagsList(User user, Project project);

List<UserTag> getTags(User user);

void updateTag(User user, UserTag tag);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.hibernate.proxy.HibernateProxyHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.acls.domain.ObjectIdentityImpl;
import org.springframework.security.acls.model.ObjectIdentity;
import org.springframework.stereotype.Service;
Expand All @@ -33,6 +34,11 @@ public UserTag get(User user, String text) {
return userTagsDao.get(user, text);
}

@Override
public UserTag get(Long id) {
return userTagsDao.get(id);
}

@Override
public UserTag createTag(User user, String text) {
UserTag userTag = new UserTagImpl(user, text);
Expand Down Expand Up @@ -84,4 +90,13 @@ public List<String> getTagsList(User user, Project project) {
public List<UserTag> getTags(User user) {
return userTagsDao.get(user);
}

public void updateTag(User user, UserTag tag) {
UserTag userTag = userTagsDao.get((Long) tag.getId());
if (user.equals(userTag.getUser())) {
userTagsDao.save(tag);
} else {
throw new AccessDeniedException("User does not have permission to update tag.");
}
}
}

0 comments on commit 1b4b788

Please sign in to comment.