Skip to content

Commit

Permalink
Merge pull request #431 from bounswe/feature/425-selfFollowing-2
Browse files Browse the repository at this point in the history
[BE] Feature/425 self following
  • Loading branch information
atakanyasar authored Oct 22, 2024
2 parents 978adcb + fc14cae commit 95937cb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public ResponseEntity<GenericApiResponse<UserProfileResponseDto>> getUserById(@P
Optional<User> user = userService.getUserById(id);
if (user.isPresent()) {
UserProfileResponseDto userProfileResponseDto = modelMapper.map(user.get(), UserProfileResponseDto.class);
userProfileResponseDto.setSelfFollowing(userService.selfFollowing(user.get()));

GenericApiResponse<UserProfileResponseDto> response = ApiResponseBuilder.buildSuccessResponse(
userProfileResponseDto.getClass(),
"User retrieved successfully",
Expand Down Expand Up @@ -152,6 +154,8 @@ public ResponseEntity<GenericApiResponse<UserProfileResponseDto>> followUser(@Pa
User user = userContextService.getCurrentUser();
User followedUser = userService.followUser(user, id);
UserProfileResponseDto updatedUserProfileResponseDto = modelMapper.map(followedUser, UserProfileResponseDto.class);
updatedUserProfileResponseDto.setSelfFollowing(userService.selfFollowing(followedUser));

GenericApiResponse<UserProfileResponseDto> response = ApiResponseBuilder.buildSuccessResponse(
updatedUserProfileResponseDto.getClass(),
"User followed successfully",
Expand Down Expand Up @@ -195,6 +199,8 @@ public ResponseEntity<GenericApiResponse<UserProfileResponseDto>> unfollowUser(@
User user = userContextService.getCurrentUser();
User unfollowedUser = userService.unfollowUser(user, id);
UserProfileResponseDto updatedUserProfileResponseDto = modelMapper.map(unfollowedUser, UserProfileResponseDto.class);
updatedUserProfileResponseDto.setSelfFollowing(userService.selfFollowing(unfollowedUser));

GenericApiResponse<UserProfileResponseDto> response = ApiResponseBuilder.buildSuccessResponse(
updatedUserProfileResponseDto.getClass(),
"User unfollowed successfully",
Expand Down
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 @@ -2,6 +2,7 @@

import com.group1.programminglanguagesforum.DTOs.Requests.UserProfileUpdateRequestDto;
import com.group1.programminglanguagesforum.Entities.User;
import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException;
import com.group1.programminglanguagesforum.Exceptions.UserNotFoundException;
import com.group1.programminglanguagesforum.Repositories.UserRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -15,6 +16,8 @@
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
private final UserContextService userContextService;

public Optional<User> getUserById(Long id) {
return userRepository.findById(id);
}
Expand Down Expand Up @@ -59,6 +62,14 @@ public User unfollowUser(User user, Long id) throws UserNotFoundException {
return userRepository.save(userToUnfollow);
}

public boolean selfFollowing(User userToCheck) {
try {
return userContextService.getCurrentUser().getFollowing().contains(userToCheck);
} catch (UnauthorizedAccessException e) {
return false;
}
}

public List<User> getFollowers(User user) {
return user.getFollowers().stream().toList();
}
Expand Down

0 comments on commit 95937cb

Please sign in to comment.