From 33c742338393250caeba31713ae4746d37c7c3fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atakan=20Ya=C5=9Far?= Date: Sun, 20 Oct 2024 21:46:10 +0300 Subject: [PATCH 1/2] feat(backend): add selfFollowing feature --- .../Controllers/UserController.java | 6 ++++++ .../Services/UserService.java | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/UserController.java b/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/UserController.java index 7c943189..46e1ceaa 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/UserController.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/UserController.java @@ -67,6 +67,8 @@ public ResponseEntity> getUserById(@P Optional user = userService.getUserById(id); if (user.isPresent()) { UserProfileResponseDto userProfileResponseDto = modelMapper.map(user.get(), UserProfileResponseDto.class); + userProfileResponseDto.setSelfFollowing(userService.selfFollowing(user.get())); + GenericApiResponse response = ApiResponseBuilder.buildSuccessResponse( userProfileResponseDto.getClass(), "User retrieved successfully", @@ -152,6 +154,8 @@ public ResponseEntity> 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 response = ApiResponseBuilder.buildSuccessResponse( updatedUserProfileResponseDto.getClass(), "User followed successfully", @@ -195,6 +199,8 @@ public ResponseEntity> unfollowUser(@ User user = userContextService.getCurrentUser(); User unfollowedUser = userService.unfollowUser(user, id); UserProfileResponseDto updatedUserProfileResponseDto = modelMapper.map(unfollowedUser, UserProfileResponseDto.class); + updatedUserProfileResponseDto.setSelfFollowing(userService.selfFollowing(unfollowedUser)); + GenericApiResponse response = ApiResponseBuilder.buildSuccessResponse( updatedUserProfileResponseDto.getClass(), "User unfollowed successfully", diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java b/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java index 404cc441..28242082 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java @@ -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; @@ -15,6 +16,8 @@ @RequiredArgsConstructor public class UserService { private final UserRepository userRepository; + private final UserContextService userContextService; + public Optional getUserById(Long id) { return userRepository.findById(id); } @@ -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 getFollowers(User user) { return user.getFollowers().stream().toList(); } From fc14cae0edc911ca65b4aa2cdd9d0dbd57689690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atakan=20Ya=C5=9Far?= Date: Sun, 20 Oct 2024 21:50:53 +0300 Subject: [PATCH 2/2] feat(backend): add selfFollowing feature 2 --- .../DTOs/Responses/UserProfileResponseDto.java | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/DTOs/Responses/UserProfileResponseDto.java b/backend/src/main/java/com/group1/programminglanguagesforum/DTOs/Responses/UserProfileResponseDto.java index 9d3fd249..47df028a 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/DTOs/Responses/UserProfileResponseDto.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/DTOs/Responses/UserProfileResponseDto.java @@ -16,5 +16,6 @@ public class UserProfileResponseDto { private Long answerCount; private int followersCount; private int followingCount; + private boolean selfFollowing; private int reputationPoints; }