Skip to content

Commit

Permalink
Merge pull request #6515 from ita-social-projects/endpoint-search-peo…
Browse files Browse the repository at this point in the history
…ple-by-frieds-friends

Endpoint search people by frieds friends
  • Loading branch information
2or5 authored Oct 9, 2023
2 parents f77878f + 1178c08 commit d6b7f6c
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/greencity/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ protected void configure(HttpSecurity http) throws Exception {
"/habit/search",
"/habit/{habitId}/friends/profile-pictures",
FRIENDS + "/not-friends-yet",
FRIENDS + "/recommended-friends",
FRIENDS + "/friendRequests",
FRIENDS)
.hasAnyRole(USER, ADMIN, MODERATOR, UBS_EMPLOYEE)
Expand Down
23 changes: 23 additions & 0 deletions core/src/main/java/greencity/controller/FriendController.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,29 @@ public ResponseEntity<PageableDto<UserFriendDto>> findAllUsersExceptMainUserAndU
.body(friendService.findAllUsersExceptMainUserAndUsersFriend(userVO.getId(), name, page));
}

/**
* Method to find friends of friends that are not friend for current user(except
* current user).
*
* @param userVO user.
*
* @return {@link PageableDto} of {@link UserFriendDto}.
*/
@ApiOperation(value = "Find recommended friends of friends that are not friend for current users")
@ApiResponses(value = {
@ApiResponse(code = 200, message = HttpStatuses.OK),
@ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED),
})
@GetMapping("/recommended-friends")
@ApiPageable
public ResponseEntity<PageableDto<UserFriendDto>> findRecommendedFriends(
@ApiIgnore Pageable page,
@ApiIgnore @CurrentUser UserVO userVO) {
return ResponseEntity
.status(HttpStatus.OK)
.body(friendService.findRecommendedFriends(userVO.getId(), page));
}

/**
* Method to find {@link UserFriendDto}s which sent request to current user.
*
Expand Down
12 changes: 12 additions & 0 deletions core/src/test/java/greencity/controller/FriendControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ void findAllUsersExceptMainUserAndUsersFriend() throws Exception {
verify(friendService).findAllUsersExceptMainUserAndUsersFriend(userVO.getId(), null, PageRequest.of(0, 20));
}

@Test
void findRecommendedFriends() throws Exception {
when(userService.findByEmail(principal.getName())).thenReturn(userVO);

mockMvc.perform(get(FRIEND_LINK + "/recommended-friends")
.principal(principal))
.andExpect(status().isOk());

verify(userService).findByEmail(principal.getName());
verify(friendService).findRecommendedFriends(userVO.getId(), PageRequest.of(0, 20));
}

@Test
void getAllUserFriendsRequestsTest() throws Exception {
when(userService.findByEmail(principal.getName())).thenReturn(userVO);
Expand Down
19 changes: 19 additions & 0 deletions dao/src/main/java/greencity/repository/UserRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,25 @@ public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExe
+ "'%', '\\%'), '_', '\\_'), '#', '\\#'), '%')) ")
Page<User> getAllUsersExceptMainUserAndFriends(Long userId, String filteringName, Pageable pageable);

/**
* Method that finds recommended friends of friends.
*
* @param userId current user's id.
* @param pageable current page.
*
* @return {@link Page} of {@link User}.
*/
@Query(nativeQuery = true, value = "SELECT u.* FROM users u "
+ "WHERE u.id != :userId"
+ " AND u.id IN ("
+ " SELECT user_id FROM users_friends"
+ " WHERE (friend_id IN (SELECT friend_id FROM users_friends WHERE user_id = :userId)"
+ " OR friend_id IN (SELECT user_id FROM users_friends WHERE friend_id = :userId)) AND status = 'FRIEND'"
+ " UNION"
+ " SELECT friend_id FROM users_friends"
+ " WHERE user_id IN (SELECT friend_id FROM users_friends WHERE user_id = :userId) AND status = 'FRIEND')")
Page<User> getRecommendedFriendsOfFriends(Long userId, Pageable pageable);

/**
* Method to find users which sent request to user with userId.
*
Expand Down
11 changes: 11 additions & 0 deletions service-api/src/main/java/greencity/service/FriendService.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,15 @@ PageableDto<UserFriendDto> findAllUsersExceptMainUserAndUsersFriend(long userId,
* @return {@link PageableDto} of {@link RecommendedFriendDto} instances.
*/
PageableDto<UserFriendDto> findAllFriendsOfUser(long userId, @Nullable String name, Pageable pageable);

/**
* Method find friends of friends for current user.
*
* @param userId user id.
* @param pageable the information about pagination and sorting for the result,
* must not be null.
*
* @return {@link PageableDto} of {@link UserFriendDto}.
*/
PageableDto<UserFriendDto> findRecommendedFriends(long userId, Pageable pageable);
}
17 changes: 17 additions & 0 deletions service/src/main/java/greencity/service/FriendServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ public PageableDto<UserFriendDto> findAllUsersExceptMainUserAndUsersFriend(long
users.getTotalPages());
}

/**
* {@inheritDoc}
*/
@Override
public PageableDto<UserFriendDto> findRecommendedFriends(long userId, Pageable pageable) {
validateUserExistence(userId);
Page<User> users =
userRepo.getRecommendedFriendsOfFriends(userId, pageable);
List<UserFriendDto> userFriendDtoList =
customUserRepo.fillListOfUserWithCountOfMutualFriendsAndChatIdForCurrentUser(userId, users.getContent());
return new PageableDto<>(
userFriendDtoList,
users.getTotalElements(),
users.getPageable().getPageNumber(),
users.getTotalPages());
}

/**
* {@inheritDoc}
*/
Expand Down
34 changes: 32 additions & 2 deletions service/src/test/java/greencity/service/FriendServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
Expand Down Expand Up @@ -571,6 +569,38 @@ void findAllUsersExceptMainUserAndUsersFriendWhenNameIsNullTest() {
userPage.getContent());
}

@Test
void findRecommendedFriends() {
long userId = 1L;
int page = 0;
int size = 1;
long totalElements = 50;
Pageable pageable = PageRequest.of(page, size);
UserFriendDto expectedResult = ModelUtils.getUserFriendDto();
Page<User> userPage = new PageImpl<>(List.of(ModelUtils.getUser()), pageable, totalElements);

when(userRepo.existsById(userId)).thenReturn(true);
when(userRepo.getRecommendedFriendsOfFriends(userId, pageable)).thenReturn(userPage);
when(
customUserRepo.fillListOfUserWithCountOfMutualFriendsAndChatIdForCurrentUser(userId, userPage.getContent()))
.thenReturn(List.of(expectedResult));

PageableDto<UserFriendDto> pageableDto =
friendService.findRecommendedFriends(userId, pageable);

assertNotNull(pageableDto.getPage());
assertEquals(1, pageableDto.getPage().size());
assertEquals(expectedResult, pageableDto.getPage().get(0));
assertEquals(totalElements, pageableDto.getTotalElements());
assertEquals(totalElements, pageableDto.getTotalPages());
assertEquals(page, pageableDto.getCurrentPage());

verify(userRepo).existsById(userId);
verify(userRepo).getRecommendedFriendsOfFriends(userId, pageable);
verify(customUserRepo).fillListOfUserWithCountOfMutualFriendsAndChatIdForCurrentUser(userId,
userPage.getContent());
}

@Test
void findAllUsersExceptMainUserAndUsersFriendWhenUserNotFoundTest() {
long userId = 1L;
Expand Down

0 comments on commit d6b7f6c

Please sign in to comment.