Skip to content

Commit

Permalink
YEL-221 [modify] 학교 키워드 검색시 해당 학교 사람들 검색
Browse files Browse the repository at this point in the history
YEL-221 [modify] 학교 키워드 검색시 해당 학교 사람들 검색
  • Loading branch information
hyeonjeongs authored Feb 16, 2024
2 parents 54dccfa + 11b655a commit a8bb63e
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
package com.yello.server.domain.friend.service;

import static com.yello.server.global.common.ErrorCode.EXIST_FRIEND_EXCEPTION;
import static com.yello.server.global.common.util.ConstantUtil.YELLO_FEMALE;
import static com.yello.server.global.common.util.ConstantUtil.YELLO_MALE;

import com.yello.server.domain.friend.dto.request.KakaoRecommendRequest;
import com.yello.server.domain.friend.dto.response.FriendResponse;
import com.yello.server.domain.friend.dto.response.FriendShuffleResponse;
import com.yello.server.domain.friend.dto.response.FriendsResponse;
import com.yello.server.domain.friend.dto.response.RecommendFriendResponse;
import com.yello.server.domain.friend.dto.response.SearchFriendResponse;
import com.yello.server.domain.friend.dto.response.SearchFriendVO;
import com.yello.server.domain.friend.dto.response.*;
import com.yello.server.domain.friend.entity.Friend;
import com.yello.server.domain.friend.exception.FriendException;
import com.yello.server.domain.friend.repository.FriendRepository;
Expand All @@ -20,19 +11,20 @@
import com.yello.server.domain.vote.repository.VoteRepository;
import com.yello.server.domain.vote.service.VoteManager;
import com.yello.server.global.common.factory.PaginationFactory;
import java.lang.Character.UnicodeBlock;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.Character.UnicodeBlock;
import java.util.*;

import static com.yello.server.global.common.ErrorCode.EXIST_FRIEND_EXCEPTION;
import static com.yello.server.global.common.util.ConstantUtil.YELLO_FEMALE;
import static com.yello.server.global.common.util.ConstantUtil.YELLO_MALE;

@Service
@Builder
@RequiredArgsConstructor
Expand All @@ -47,13 +39,13 @@ public class FriendService {
public FriendsResponse findAllFriends(Pageable pageable, Long userId) {
final Page<Friend> friendsData = friendRepository.findAllFriendsByUserId(pageable, userId);
List<UserResponse> friends = friendsData.stream()
.map(friend -> {
User targetUser = friend.getTarget();
Integer friendCount = friendRepository.countAllByUserId(targetUser.getId());
Integer yelloCount = voteRepository.countAllByReceiverUserId(targetUser.getId());
return UserResponse.of(targetUser, yelloCount, friendCount);
})
.toList();
.map(friend -> {
User targetUser = friend.getTarget();
Integer friendCount = friendRepository.countAllByUserId(targetUser.getId());
Integer yelloCount = voteRepository.countAllByReceiverUserId(targetUser.getId());
return UserResponse.of(targetUser, yelloCount, friendCount);
})
.toList();

return FriendsResponse.of(friendsData.getTotalElements(), friends);
}
Expand Down Expand Up @@ -85,10 +77,10 @@ public RecommendFriendResponse findAllRecommendSchoolFriends(Pageable pageable,

Integer size = userRepository.countAllByGroupNameFilteredByNotFriend(userId, user.getGroup().getGroupName());
List<FriendResponse> recommendFriends =
userRepository.findAllByGroupNameFilteredByNotFriend(userId, user.getGroup().getGroupName(), pageable)
.stream()
.map(FriendResponse::of)
.toList();
userRepository.findAllByGroupNameFilteredByNotFriend(userId, user.getGroup().getGroupName(), pageable)
.stream()
.map(FriendResponse::of)
.toList();

return RecommendFriendResponse.of(size, recommendFriends);
}
Expand All @@ -106,25 +98,25 @@ public void deleteFriend(Long userId, Long targetId) {
}

public RecommendFriendResponse findAllRecommendKakaoFriends(Pageable pageable, Long userId,
KakaoRecommendRequest request) {
KakaoRecommendRequest request) {
final User user = userRepository.getById(userId);

List<User> kakaoFriends = Arrays.stream(request.friendKakaoId())
.filter(userRepository::existsByUuid)
.map(userRepository::getByUuid)
.filter(friend -> !friendRepository.existsByUserAndTarget(user.getId(), friend.getId()))
.toList();
.filter(userRepository::existsByUuid)
.map(userRepository::getByUuid)
.filter(friend -> !friendRepository.existsByUserAndTarget(user.getId(), friend.getId()))
.toList();

List<FriendResponse> pageList = PaginationFactory.getPage(kakaoFriends, pageable)
.stream()
.map(FriendResponse::of)
.toList();
.stream()
.map(FriendResponse::of)
.toList();

return RecommendFriendResponse.of(kakaoFriends.size(), pageList);
}

public SearchFriendResponse searchFriend(Long userId, Pageable pageable,
String keyword) {
String keyword) {
final User user = userRepository.getById(userId);
final String groupName = user.getGroup().getGroupName();
List<String> uuidList = Arrays.asList(YELLO_FEMALE, YELLO_MALE);
Expand All @@ -137,23 +129,24 @@ public SearchFriendResponse searchFriend(Long userId, Pageable pageable,

if (!isEnglish(keyword)) {
friendList.addAll(
userRepository.findAllByGroupContainingName(groupName, keyword, uuidList));
userRepository.findAllByGroupContainingName(groupName, keyword, uuidList));
friendList.addAll(
userRepository.findAllByOtherGroupContainingName(groupName, keyword, uuidList));
userRepository.findAllByOtherGroupContainingName(groupName, keyword, uuidList));

} else {
friendList.addAll(
userRepository.findAllByGroupContainingYelloId(groupName, keyword, uuidList));
userRepository.findAllByGroupContainingYelloId(groupName, keyword, uuidList));
friendList.addAll(
userRepository.findAllByOtherGroupContainingYelloId(groupName, keyword, uuidList));
userRepository.findAllByOtherGroupContainingYelloId(groupName, keyword, uuidList));
friendList.addAll(userRepository.findAllByGroupNameContainingAndFriendListNotContaining(keyword, uuidList, friendList));
}

List<SearchFriendVO> pageList = PaginationFactory.getPage(friendList, pageable)
.stream()
.filter(friend -> !userId.equals(friend.getId()))
.map(friend -> SearchFriendVO.of(friend,
friendRepository.existsByUserAndTarget(userId, friend.getId())))
.toList();
.stream()
.filter(friend -> !userId.equals(friend.getId()))
.map(friend -> SearchFriendVO.of(friend,
friendRepository.existsByUserAndTarget(userId, friend.getId())))
.toList();

return SearchFriendResponse.of(friendList.size(), pageList);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,126 +1,136 @@
package com.yello.server.domain.user.repository;

import com.yello.server.domain.user.entity.User;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface UserJpaRepository extends JpaRepository<User, Long> {

@Query("select u from User u " +
"where u.id = :id " +
"and u.deletedAt is null")
"where u.id = :id " +
"and u.deletedAt is null")
Optional<User> findById(@Param("id") Long id);

@Query("select u from User u " +
"where u.id = :id")
"where u.id = :id")
Optional<User> findByIdNotFiltered(@Param("id") Long id);

@Query("select u from User u " +
"where u.uuid = :uuid")
"where u.uuid = :uuid")
Optional<User> findByUuid(@Param("uuid") String uuid);

@Query("select u from User u " +
"where u.uuid = :uuid")
"where u.uuid = :uuid")
Optional<User> findByUuidNotFiltered(@Param("uuid") String uuid);

@Query("select case when count(u) > 0 then true else false end from User u " +
"where u.uuid = :uuid " +
"and u.deletedAt is null")
"where u.uuid = :uuid " +
"and u.deletedAt is null")
boolean existsByUuid(@Param("uuid") String uuid);

@Query("select u from User u " +
"where u.yelloId = :yelloId " +
"and u.deletedAt is null")
"where u.yelloId = :yelloId " +
"and u.deletedAt is null")
Optional<User> findByYelloId(@Param("yelloId") String yelloId);

@Query("select u from User u " +
"where u.yelloId = :yelloId")
"where u.yelloId = :yelloId")
Optional<User> findByYelloIdNotFiltered(@Param("yelloId") String yelloId);

@Query("select u from User u, UserGroup g " +
"where u.group.id = g.id " +
"and g.id = :groupId " +
"and u.deletedAt is null")
"where u.group.id = g.id " +
"and g.id = :groupId " +
"and u.deletedAt is null")
List<User> findAllByGroupId(@Param("groupId") Long groupId);

@Query("select count (u) from User u, UserGroup g " +
"where u.group.id = g.id " +
"and g.groupName = :groupName " +
"and u.id <> :userId " +
"and u.id not in (select f.target.id from Friend f where :userId = f.user.id and f.target.deletedAt is null) "
+
"and u.deletedAt is null")
"where u.group.id = g.id " +
"and g.groupName = :groupName " +
"and u.id <> :userId " +
"and u.id not in (select f.target.id from Friend f where :userId = f.user.id and f.target.deletedAt is null) "
+
"and u.deletedAt is null")
Integer countAllByGroupNameFilteredByNotFriend(@Param("userId") Long userId, @Param("groupName") String groupName);

@Query("select u from User u, UserGroup g " +
"where u.group.id = g.id " +
"and g.groupName = :groupName " +
"and u.id <> :userId " +
"and u.id not in (select f.target.id from Friend f where :userId = f.user.id and f.target.deletedAt is null) "
+
"and u.deletedAt is null")
"where u.group.id = g.id " +
"and g.groupName = :groupName " +
"and u.id <> :userId " +
"and u.id not in (select f.target.id from Friend f where :userId = f.user.id and f.target.deletedAt is null) "
+
"and u.deletedAt is null")
List<User> findAllByGroupNameFilteredByNotFriend(@Param("userId") Long userId,
@Param("groupName") String groupName, Pageable pageable);
@Param("groupName") String groupName, Pageable pageable);

@Query("select u from User u "
+ "where u.group.groupName = :groupName "
+ "and u.uuid not in :uuidList "
+ "and u.name like CONCAT('%', :keyword, '%') "
+ "and u.deletedAt is null "
+ "order by u.name ASC ")
+ "where u.group.groupName = :groupName "
+ "and u.uuid not in :uuidList "
+ "and u.name like CONCAT('%', :keyword, '%') "
+ "and u.deletedAt is null "
+ "order by u.name ASC ")
List<User> findAllByGroupContainingName(@Param("groupName") String groupName,
@Param("keyword") String keyword, @Param("uuidList") List<String> uuidList);
@Param("keyword") String keyword, @Param("uuidList") List<String> uuidList);

@Query("select u from User u "
+ "where u.group.groupName <> :groupName "
+ "and u.uuid not in :uuidList "
+ "and u.name like CONCAT('%', :keyword, '%') "
+ "and u.deletedAt is null "
+ "order by u.groupAdmissionYear DESC ")
+ "where u.group.groupName <> :groupName "
+ "and u.uuid not in :uuidList "
+ "and u.name like CONCAT('%', :keyword, '%') "
+ "and u.deletedAt is null "
+ "order by u.groupAdmissionYear DESC ")
List<User> findAllByOtherGroupContainingName(@Param("groupName") String groupName,
@Param("keyword") String keyword, @Param("uuidList") List<String> uuidList);
@Param("keyword") String keyword, @Param("uuidList") List<String> uuidList);

@Query("select u from User u " +
"where u.group.groupName like CONCAT('%', :keyword, '%') " +
"and u.uuid not in :uuidList " +
"and u not in :friendList " +
"and u.deletedAt is null " +
"order by u.name ASC ")
List<User> findAllByGroupContaining(@Param("keyword") String keyword, @Param("uuidList") List<String> uuidList, @Param("friendList") List<User> friendList);


@Query("select u from User u "
+ "where u.group.groupName = :groupName "
+ "and u.uuid not in :uuidList "
+ "and LOWER(u.yelloId) like LOWER(CONCAT('%', :keyword, '%')) "
+ "and u.deletedAt is null "
+ "order by u.yelloId ASC ")
+ "where u.group.groupName = :groupName "
+ "and u.uuid not in :uuidList "
+ "and LOWER(u.yelloId) like LOWER(CONCAT('%', :keyword, '%')) "
+ "and u.deletedAt is null "
+ "order by u.yelloId ASC ")
List<User> findAllByGroupContainingYelloId(@Param("groupName") String groupName,
@Param("keyword") String keyword, @Param("uuidList") List<String> uuidList);
@Param("keyword") String keyword, @Param("uuidList") List<String> uuidList);

@Query("select u from User u "
+ "where u.group.groupName <> :groupName "
+ "and u.uuid not in :uuidList "
+ "and LOWER(u.yelloId) like LOWER(CONCAT('%', :keyword, '%')) "
+ "and u.deletedAt is null "
+ "order by u.groupAdmissionYear DESC ")
+ "where u.group.groupName <> :groupName "
+ "and u.uuid not in :uuidList "
+ "and LOWER(u.yelloId) like LOWER(CONCAT('%', :keyword, '%')) "
+ "and u.deletedAt is null "
+ "order by u.groupAdmissionYear DESC ")
List<User> findAllByOtherGroupContainingYelloId(@Param("groupName") String groupName,
@Param("keyword") String keyword, @Param("uuidList") List<String> uuidList);
@Param("keyword") String keyword, @Param("uuidList") List<String> uuidList);

@Query("select u from User u "
+ "where u.deviceToken = :deviceToken "
+ "and u.deletedAt is null")
+ "where u.deviceToken = :deviceToken "
+ "and u.deletedAt is null")
Optional<User> findByDeviceToken(@Param("deviceToken") String deviceToken);

@Query("select u from User u " +
"where u.deviceToken = :deviceToken")
"where u.deviceToken = :deviceToken")
Optional<User> findByDeviceTokenNotFiltered(@Param("deviceToken") String deviceToken);

Long countAllByYelloIdContaining(String yelloId);

Long countAllByNameContaining(String name);

@Query("select u from User u "
+ "where LOWER(u.yelloId) like LOWER(CONCAT('%', :yelloId, '%'))")
+ "where LOWER(u.yelloId) like LOWER(CONCAT('%', :yelloId, '%'))")
Page<User> findAllByYelloIdContaining(Pageable pageable, @Param("yelloId") String yelloId);

@Query("select u from User u "
+ "where LOWER(u.name) like LOWER(CONCAT('%', :name, '%'))")
+ "where LOWER(u.name) like LOWER(CONCAT('%', :name, '%'))")
Page<User> findAllByNameContaining(Pageable pageable, @Param("name") String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ List<User> findAllByGroupContainingYelloId(String groupName, String keyword,
List<User> findAllByOtherGroupContainingYelloId(String groupName, String keyword,
List<String> uuidList);

List<User> findAllByGroupNameContainingAndFriendListNotContaining(String keyword, List<String> uuidList, List<User> friendList);

Long count();

Long countAllByYelloIdContaining(String yelloId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ public List<User> findAllByOtherGroupContainingYelloId(String groupName, String
return userJpaRepository.findAllByOtherGroupContainingYelloId(groupName, keyword, uuidList);
}

@Override
public List<User> findAllByGroupNameContainingAndFriendListNotContaining(String keyword, List<String> uuidList, List<User> friendList) {
return userJpaRepository.findAllByGroupContaining(keyword, uuidList, friendList);
}

@Override
public Long count() {
return userJpaRepository.count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public enum SuccessCode {


private final HttpStatus httpStatus;
private final String message;
String message;

public int getHttpStatusCode() {
return httpStatus.value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ public List<User> findAllByOtherGroupContainingYelloId(String groupName, String
.toList();
}

@Override
public List<User> findAllByGroupNameContainingAndFriendListNotContaining(String keyword, List<String> uuidList, List<User> friendList) {
return data.stream()
.filter(user -> user.getGroup().getGroupName().contains(keyword))
.filter(user -> !user.getId().equals(1L))
.filter(user -> !uuidList.contains(user.getUuid()))
.toList();
}

@Override
public Long count() {
return (long) data.size();
Expand Down

0 comments on commit a8bb63e

Please sign in to comment.