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

[BE] Feature/425 self following #428

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public class UserProfileResponseDto {
private Long answerCount;
private int followersCount;
private int followingCount;
private boolean selfFollowing;
private int reputationPoints;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class User implements UserDetails {
private Set<User> followers = new HashSet<>();
@ManyToMany(mappedBy = "followers", fetch = FetchType.EAGER)
private Set<User> following = new HashSet<>();
private boolean selfFollowing = false;

@Builder.Default
private int followersCount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public User followUser(User user, Long id) throws UserNotFoundException {
throw new UserNotFoundException("User not found");
}
if(user.getId().equals(id)) {
throw new UserNotFoundException("You can't follow yourself");
user.setSelfFollowing(true);
return userRepository.save(user);
}
User userToFollow = userToFollowOptional.get();
user.getFollowing().add(userToFollow);
Expand All @@ -50,6 +51,10 @@ public User unfollowUser(User user, Long id) throws UserNotFoundException {
if (userToUnfollowOptional.isEmpty()) {
throw new UserNotFoundException("User not found");
}
if (user.getId().equals(id)) {
user.setSelfFollowing(false);
return userRepository.save(user);
}
User userToUnfollow = userToUnfollowOptional.get();
user.getFollowing().remove(userToUnfollow);
user.setFollowingCount(user.getFollowingCount() - 1);
Expand Down
Loading