diff --git a/.github/workflows/backend_test.yml b/.github/workflows/backend_test.yml index d6dd3e3b..d134bcf2 100644 --- a/.github/workflows/backend_test.yml +++ b/.github/workflows/backend_test.yml @@ -23,4 +23,4 @@ jobs: uses: actions/checkout@v4 - name: Test with Maven - run: docker compose -f dev.yml run --rm backend mvn test + run: docker compose -f dev.yml run --rm backend sh -c "mvn clean install -q && mvn test -B" diff --git a/.github/workflows/validate_pr.yml b/.github/workflows/validate_pr.yml new file mode 100644 index 00000000..e2f571b7 --- /dev/null +++ b/.github/workflows/validate_pr.yml @@ -0,0 +1,18 @@ +name: Validate PR to main + +on: + pull_request: + branches: + - main + +jobs: + validate_pr_source: + runs-on: ubuntu-latest + steps: + - name: Check PR source branch + run: | + if [ "${{ github.head_ref }}" != "develop" ]; then + echo "Error: Pull requests to main are only allowed from the develop branch." + echo "Current branch: ${{ github.head_ref }}" + exit 1 + fi \ No newline at end of file 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/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; } 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(); } diff --git a/compose.yml b/compose.yml index c1db9285..cf1bc670 100644 --- a/compose.yml +++ b/compose.yml @@ -36,7 +36,7 @@ services: ports: - "8080:8080" environment: - DB_HOST: db + DB_HOST: db:3306 DB_NAME: programmingforum-test DB_USER: root DB_PASSWORD: admin