diff --git a/src/main/java/com/yello/server/domain/friend/service/FriendService.java b/src/main/java/com/yello/server/domain/friend/service/FriendService.java index 9f907c75..4450a430 100644 --- a/src/main/java/com/yello/server/domain/friend/service/FriendService.java +++ b/src/main/java/com/yello/server/domain/friend/service/FriendService.java @@ -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; @@ -20,12 +11,6 @@ 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; @@ -33,6 +18,13 @@ 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 @@ -47,13 +39,13 @@ public class FriendService { public FriendsResponse findAllFriends(Pageable pageable, Long userId) { final Page friendsData = friendRepository.findAllFriendsByUserId(pageable, userId); List 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); } @@ -85,10 +77,10 @@ public RecommendFriendResponse findAllRecommendSchoolFriends(Pageable pageable, Integer size = userRepository.countAllByGroupNameFilteredByNotFriend(userId, user.getGroup().getGroupName()); List 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); } @@ -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 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 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 uuidList = Arrays.asList(YELLO_FEMALE, YELLO_MALE); @@ -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 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); } diff --git a/src/main/java/com/yello/server/domain/user/repository/UserJpaRepository.java b/src/main/java/com/yello/server/domain/user/repository/UserJpaRepository.java index 1d5eaab1..01d64b09 100644 --- a/src/main/java/com/yello/server/domain/user/repository/UserJpaRepository.java +++ b/src/main/java/com/yello/server/domain/user/repository/UserJpaRepository.java @@ -1,115 +1,125 @@ 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 { @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 findById(@Param("id") Long id); @Query("select u from User u " + - "where u.id = :id") + "where u.id = :id") Optional findByIdNotFiltered(@Param("id") Long id); @Query("select u from User u " + - "where u.uuid = :uuid") + "where u.uuid = :uuid") Optional findByUuid(@Param("uuid") String uuid); @Query("select u from User u " + - "where u.uuid = :uuid") + "where u.uuid = :uuid") Optional 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 findByYelloId(@Param("yelloId") String yelloId); @Query("select u from User u " + - "where u.yelloId = :yelloId") + "where u.yelloId = :yelloId") Optional 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 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 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 findAllByGroupContainingName(@Param("groupName") String groupName, - @Param("keyword") String keyword, @Param("uuidList") List uuidList); + @Param("keyword") String keyword, @Param("uuidList") List 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 findAllByOtherGroupContainingName(@Param("groupName") String groupName, - @Param("keyword") String keyword, @Param("uuidList") List uuidList); + @Param("keyword") String keyword, @Param("uuidList") List 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 findAllByGroupContaining(@Param("keyword") String keyword, @Param("uuidList") List uuidList, @Param("friendList") List 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 findAllByGroupContainingYelloId(@Param("groupName") String groupName, - @Param("keyword") String keyword, @Param("uuidList") List uuidList); + @Param("keyword") String keyword, @Param("uuidList") List 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 findAllByOtherGroupContainingYelloId(@Param("groupName") String groupName, - @Param("keyword") String keyword, @Param("uuidList") List uuidList); + @Param("keyword") String keyword, @Param("uuidList") List 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 findByDeviceToken(@Param("deviceToken") String deviceToken); @Query("select u from User u " + - "where u.deviceToken = :deviceToken") + "where u.deviceToken = :deviceToken") Optional findByDeviceTokenNotFiltered(@Param("deviceToken") String deviceToken); Long countAllByYelloIdContaining(String yelloId); @@ -117,10 +127,10 @@ List findAllByOtherGroupContainingYelloId(@Param("groupName") String group 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 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 findAllByNameContaining(Pageable pageable, @Param("name") String name); } diff --git a/src/main/java/com/yello/server/domain/user/repository/UserRepository.java b/src/main/java/com/yello/server/domain/user/repository/UserRepository.java index 66df3633..2ef67768 100644 --- a/src/main/java/com/yello/server/domain/user/repository/UserRepository.java +++ b/src/main/java/com/yello/server/domain/user/repository/UserRepository.java @@ -60,6 +60,8 @@ List findAllByGroupContainingYelloId(String groupName, String keyword, List findAllByOtherGroupContainingYelloId(String groupName, String keyword, List uuidList); + List findAllByGroupNameContainingAndFriendListNotContaining(String keyword, List uuidList, List friendList); + Long count(); Long countAllByYelloIdContaining(String yelloId); diff --git a/src/main/java/com/yello/server/domain/user/repository/UserRepositoryImpl.java b/src/main/java/com/yello/server/domain/user/repository/UserRepositoryImpl.java index a56bc042..aa1e8df4 100644 --- a/src/main/java/com/yello/server/domain/user/repository/UserRepositoryImpl.java +++ b/src/main/java/com/yello/server/domain/user/repository/UserRepositoryImpl.java @@ -153,6 +153,11 @@ public List findAllByOtherGroupContainingYelloId(String groupName, String return userJpaRepository.findAllByOtherGroupContainingYelloId(groupName, keyword, uuidList); } + @Override + public List findAllByGroupNameContainingAndFriendListNotContaining(String keyword, List uuidList, List friendList) { + return userJpaRepository.findAllByGroupContaining(keyword, uuidList, friendList); + } + @Override public Long count() { return userJpaRepository.count(); diff --git a/src/main/java/com/yello/server/global/common/SuccessCode.java b/src/main/java/com/yello/server/global/common/SuccessCode.java index 524ecb24..10025da4 100644 --- a/src/main/java/com/yello/server/global/common/SuccessCode.java +++ b/src/main/java/com/yello/server/global/common/SuccessCode.java @@ -72,7 +72,7 @@ public enum SuccessCode { private final HttpStatus httpStatus; - private final String message; + String message; public int getHttpStatusCode() { return httpStatus.value(); diff --git a/src/test/java/com/yello/server/domain/user/FakeUserRepository.java b/src/test/java/com/yello/server/domain/user/FakeUserRepository.java index 908bb1ff..569a3e48 100644 --- a/src/test/java/com/yello/server/domain/user/FakeUserRepository.java +++ b/src/test/java/com/yello/server/domain/user/FakeUserRepository.java @@ -258,6 +258,15 @@ public List findAllByOtherGroupContainingYelloId(String groupName, String .toList(); } + @Override + public List findAllByGroupNameContainingAndFriendListNotContaining(String keyword, List uuidList, List 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();