Skip to content

Commit

Permalink
friend filtering added
Browse files Browse the repository at this point in the history
  • Loading branch information
Warded120 committed Dec 23, 2024
1 parent 5de5652 commit 068e215
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 69 deletions.
22 changes: 17 additions & 5 deletions core/src/main/java/greencity/controller/FriendController.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,16 @@ public ResponseEntity<PageableDto<UserFriendDto>> findUserFriendsByUserIAndShowF
public ResponseEntity<PageableDto<UserFriendDto>> findAllUsersExceptMainUserAndUsersFriendAndRequestersToMainUser(
@Parameter(hidden = true) @PageableDefault Pageable page,
@Parameter(hidden = true) @CurrentUser UserVO userVO,
@RequestParam(required = false) @Nullable String name) {
@RequestParam(required = false, defaultValue = "") String name,
@RequestParam(required = false, defaultValue = "false") boolean filterByFriendsOfFriends,
@RequestParam(required = false, defaultValue = "true") boolean filterByCity) {
return ResponseEntity
.status(HttpStatus.OK)
.body(friendService.findAllUsersExceptMainUserAndUsersFriendAndRequestersToMainUser(userVO.getId(), name,
page));
.body(friendService.findAllUsersExceptMainUserAndUsersFriendAndRequestersToMainUser(userVO.getId(),
name,
filterByFriendsOfFriends,
filterByCity,
page));
}

/**
Expand Down Expand Up @@ -299,6 +304,7 @@ public ResponseEntity<PageableDto<UserFriendDto>> getAllUserFriendsRequests(
*
* @return {@link PageableDto} of {@link UserFriendDto}.
*/
//TODO: do the same
@Operation(summary = "Find all friends")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = HttpStatuses.OK),
Expand All @@ -310,12 +316,18 @@ public ResponseEntity<PageableDto<UserFriendDto>> getAllUserFriendsRequests(
@GetMapping
@ApiPageable
public ResponseEntity<PageableDto<UserFriendDto>> findAllFriendsOfUser(
@RequestParam(required = false, defaultValue = "") String name,
@RequestParam(required = false, defaultValue = "false") boolean filterByFriendsOfFriends,
@RequestParam(required = false, defaultValue = "false") boolean filterByCity,
@Parameter(hidden = true) Pageable page,
@RequestParam(required = false) @Nullable String name,
@Parameter(hidden = true) @CurrentUser UserVO userVO) {
return ResponseEntity
.status(HttpStatus.OK)
.body(friendService.findAllFriendsOfUser(userVO.getId(), name, page));
.body(friendService.findAllFriendsOfUser(userVO.getId(),
name,
filterByFriendsOfFriends,
filterByCity,
page));
}

/**
Expand Down
185 changes: 131 additions & 54 deletions dao/src/main/java/greencity/repository/UserRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ OR LOWER(u.user_credo) LIKE LOWER(
'_', '\\_'),
'#', '\\#'), '%')
)
)
OR EXISTS (
SELECT 1
FROM user_location ul
Expand All @@ -368,7 +367,6 @@ AND LOWER(ul.city_en) LIKE LOWER(
'#', '\\#'), '%')
)
)
)
""")

Page<User> getAllUsersExceptMainUserAndFriends(Long userId, String filteringName, Pageable pageable);
Expand All @@ -383,58 +381,99 @@ AND LOWER(ul.city_en) LIKE LOWER(
* @return {@link Page} of {@link User}.
*/
@Query(nativeQuery = true,
value = """
SELECT *
FROM users u
WHERE u.id != :userId
AND u.id NOT IN (
SELECT user_id AS id
FROM users_friends
WHERE friend_id = :userId
AND status = 'FRIEND'
UNION
SELECT friend_id AS id
FROM users_friends
WHERE user_id = :userId
AND status = 'FRIEND'
)
AND (
LOWER(u.name) LIKE LOWER(
CONCAT('%',
REPLACE(REPLACE(
REPLACE(REPLACE(:filteringName, '&', '\\&'),
'%', '\\%'),
'_', '\\_'),
'#', '\\#'), '%')
)
)
OR LOWER(u.user_credo) LIKE LOWER(
CONCAT('%',
REPLACE(REPLACE(
REPLACE(REPLACE(:filteringName, '&', '\\&'),
'%', '\\%'),
'_', '\\_'),
'#', '\\#'), '%')
)
)
OR EXISTS (
SELECT 1
FROM user_location ul
WHERE ul.id = u.user_location
AND LOWER(ul.city_en) LIKE LOWER(
CONCAT('%',
REPLACE(REPLACE(
value = """
SELECT *
FROM users u
WHERE u.id != :userId
AND u.id NOT IN (
SELECT user_id AS id
FROM users_friends
WHERE friend_id = :userId
AND status = 'FRIEND'
UNION
SELECT friend_id AS id
FROM users_friends
WHERE user_id = :userId
AND status = 'FRIEND'
)
AND (
LOWER(u.name) LIKE LOWER(
CONCAT('%',
REPLACE(REPLACE(
REPLACE(REPLACE(:filteringName, '&', '\\&'),
'%', '\\%'),
'_', '\\_'),
'#', '\\#'), '%')
)
OR LOWER(u.user_credo) LIKE LOWER(
CONCAT('%',
REPLACE(REPLACE(
REPLACE(REPLACE(:filteringName, '&', '\\&'),
'%', '\\%'),
'_', '\\_'),
'#', '\\#'), '%')
)
OR EXISTS (
SELECT 1
FROM user_location ul
WHERE ul.id = u.user_location
AND LOWER(ul.city_en) LIKE LOWER(
CONCAT('%',
REPLACE(REPLACE(
REPLACE(REPLACE(:filteringName, '&', '\\&'),
'%', '\\%'),
'_', '\\_'),
'#', '\\#'), '%')
)
)
)
""")

Page<User> getAllUsersExceptMainUserAndFriendsAndRequestersToMainUser(Long userId, String filteringName,
Pageable pageable);
'_', '\\_'),
'#', '\\#'), '%')
)
)
)
AND (
:filterByFriendsOfFriends = FALSE
OR 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'
)
)
AND (
:filterByCity = FALSE
OR EXISTS (
SELECT 1
FROM user_location ul
WHERE ul.id = u.user_location
AND ul.city_ua IN (
SELECT ul2.city_ua FROM user_location ul2
JOIN users u2 ON ul2.id = u2.user_location
WHERE u2.id = :userId
)
)
)
"""
)
Page<User> getAllUsersExceptMainUserAndFriendsAndRequestersToMainUser(Long userId,
String filteringName,
boolean filterByFriendsOfFriends,
boolean filterByCity,
Pageable pageable);

/**
* Method that finds recommended friends of friends.
Expand Down Expand Up @@ -508,7 +547,6 @@ OR LOWER(u.user_credo) LIKE LOWER(
'_', '\\_'),
'#', '\\#'), '%')
)
)
OR EXISTS (
SELECT 1
FROM user_location ul
Expand All @@ -522,7 +560,46 @@ AND LOWER(ul.city_en) LIKE LOWER(
'#', '\\#'), '%')
)
)
)
AND (
:filterByFriendsOfFriends = FALSE
OR 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'
)
)
AND (
:filterByCity = FALSE
OR EXISTS (
SELECT 1
FROM user_location ul
WHERE ul.id = u.user_location
AND ul.city_ua IN (
SELECT ul2.city_ua FROM user_location ul2
JOIN users u2 ON ul2.id = u2.user_location
WHERE u2.id = :userId
)
)
)
""")

Page<User> findAllFriendsOfUser(Long userId, String filteringName, Pageable pageable);
Expand Down
12 changes: 9 additions & 3 deletions service-api/src/main/java/greencity/service/FriendService.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ PageableDto<UserFriendDto> findUserFriendsByUserIAndShowFriendStatusRelatedToCur
* @author Stepan Omeliukh
*/
PageableDto<UserFriendDto> findAllUsersExceptMainUserAndUsersFriendAndRequestersToMainUser(long userId,
@Nullable String name,
Pageable pageable);
String name,
boolean filterByFriendsOfFriends,
boolean filterByCity,
Pageable pageable);

/**
* Method to find {@link UserFriendDto}s which sent request to user with userId.
Expand All @@ -107,7 +109,11 @@ PageableDto<UserFriendDto> findAllUsersExceptMainUserAndUsersFriendAndRequesters
*
* @return {@link PageableDto} of {@link UserFriendDto} instances.
*/
PageableDto<UserFriendDto> findAllFriendsOfUser(long userId, @Nullable String name, Pageable pageable);
PageableDto<UserFriendDto> findAllFriendsOfUser(long userId,
String name,
boolean filterByFriendsOfFriends,
boolean filterByCity,
Pageable pageable);

/**
* Method find recommended friends for user by recommendation type.
Expand Down
22 changes: 15 additions & 7 deletions service/src/main/java/greencity/service/FriendServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,21 @@ public PageableDto<UserFriendDto> findUserFriendsByUserIAndShowFriendStatusRelat
*/
@Override
public PageableDto<UserFriendDto> findAllUsersExceptMainUserAndUsersFriendAndRequestersToMainUser(long userId,
@Nullable String name, Pageable pageable) {
@Nullable String name,
boolean filterByFriendsOfFriends,
boolean filterByCity,
Pageable pageable) {
Objects.requireNonNull(pageable);

validateUserExistence(userId);
name = name == null ? "" : name;
if (name.isEmpty()) {
//name = name == null ? "" : name;
/*if (name.isEmpty()) {
return new PageableDto<>(List.of(), 0, 0, 0);
}
}*/
Page<User> users;
System.out.println(pageable.getSort());
if (pageable.getSort().isEmpty()) {
users = userRepo.getAllUsersExceptMainUserAndFriendsAndRequestersToMainUser(userId, name, pageable);
users = userRepo.getAllUsersExceptMainUserAndFriendsAndRequestersToMainUser(userId, name, filterByFriendsOfFriends, filterByCity, pageable);
} else {
throw new UnsupportedSortException(ErrorMessage.INVALID_SORTING_VALUE);
}
Expand Down Expand Up @@ -239,11 +243,15 @@ public PageableDto<UserFriendDto> getAllUserFriendRequests(long userId, Pageable
* {@inheritDoc}
*/
@Override
public PageableDto<UserFriendDto> findAllFriendsOfUser(long userId, @Nullable String name, Pageable pageable) {
public PageableDto<UserFriendDto> findAllFriendsOfUser(long userId,
@Nullable String name,
boolean filterByFriendsOfFriends,
boolean filterByCity,
Pageable pageable) {
Objects.requireNonNull(pageable);

validateUserExistence(userId);
name = name == null ? "" : name;
//name = name == null ? "" : name;
Page<User> users;
if (pageable.getSort().isEmpty()) {
users = userRepo.findAllFriendsOfUser(userId, name, pageable);
Expand Down

0 comments on commit 068e215

Please sign in to comment.