-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into refactor/207
- Loading branch information
Showing
11 changed files
with
276 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
src/main/java/com/gamegoo/repository/friend/FriendRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/com/gamegoo/repository/friend/FriendRepositoryCustom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.gamegoo.repository.friend; | ||
|
||
import com.gamegoo.domain.friend.Friend; | ||
import java.util.List; | ||
import org.springframework.data.domain.Slice; | ||
|
||
interface FriendRepositoryCustom { | ||
|
||
Slice<Friend> findFriendsByCursorAndOrdered(Long cursor, Long memberId, Integer pageSize); | ||
|
||
List<Friend> findFriendsByQueryStringAndOrdered(String queryString, Long memberId); | ||
} |
140 changes: 140 additions & 0 deletions
140
src/main/java/com/gamegoo/repository/friend/FriendRepositoryCustomImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package com.gamegoo.repository.friend; | ||
|
||
import static com.gamegoo.domain.friend.QFriend.friend; | ||
|
||
import com.gamegoo.domain.friend.Friend; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.domain.SliceImpl; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class FriendRepositoryCustomImpl implements FriendRepositoryCustom { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public Slice<Friend> findFriendsByCursorAndOrdered(Long cursorId, Long memberId, | ||
Integer pageSize) { | ||
// ์ ์ฒด ์น๊ตฌ ๋ชฉ๋ก ์กฐํ | ||
List<Friend> allFriends = queryFactory.selectFrom(friend) | ||
.where(friend.fromMember.id.eq(memberId)) | ||
.fetch(); | ||
|
||
// ์น๊ตฌ ๋ชฉ๋ก ์ ์ฒด ์ ๋ ฌ | ||
allFriends.sort( | ||
(f1, f2) -> memberNameComparator.compare(f1.getToMember().getGameName(), | ||
f2.getToMember().getGameName())); | ||
|
||
// ์ ๋ ฌ๋ ๋ฐ์ดํฐ์์ ํ์ด์ง ์ ์ฉ | ||
// cursorId์ ํด๋นํ๋ ์์์ ์ธ๋ฑ์ค ์ฐพ๊ธฐ | ||
int startIndex = 0; | ||
if (cursorId != null) { | ||
startIndex = findCursorIndex(allFriends, cursorId); | ||
} | ||
|
||
List<Friend> pagedFriends = allFriends.stream() | ||
.skip(startIndex != 0 ? startIndex + 1 : 0) // cursorId๊ฐ null์ด ์๋๋ฉด ์ธ๋ฑ์ค ๋ค์๋ถํฐ, null์ด๋ฉด ์ฒ์๋ถํฐ | ||
.limit(pageSize + 1) // ๋ค์ ํ์ด์ง๊ฐ ์๋์ง ํ์ธํ๊ธฐ ์ํด +1 | ||
.collect(Collectors.toList()); | ||
|
||
boolean hasNext = false; | ||
if (pagedFriends.size() > pageSize) { | ||
pagedFriends.remove(pageSize.intValue()); | ||
hasNext = true; | ||
} | ||
|
||
PageRequest pageRequest = PageRequest.of(0, pageSize); | ||
|
||
return new SliceImpl<>(pagedFriends, pageRequest, hasNext); | ||
} | ||
|
||
@Override | ||
public List<Friend> findFriendsByQueryStringAndOrdered(String queryString, Long memberId) { | ||
// query string์ผ๋ก ์์ํ๋ ์ํ์ฌ๋ช ์ ๊ฐ๋ ๋ชจ๋ ์น๊ตฌ ๋ชฉ๋ก ์กฐํ | ||
List<Friend> result = queryFactory.selectFrom(friend) | ||
.where(friend.fromMember.id.eq(memberId) | ||
.and(friend.toMember.gameName.startsWith(queryString)) | ||
) | ||
.fetch(); | ||
|
||
result.sort( | ||
(f1, f2) -> memberNameComparator.compare(f1.getToMember().getGameName(), | ||
f2.getToMember().getGameName())); | ||
|
||
return result; | ||
} | ||
|
||
// cursorId์ ํด๋นํ๋ Friend ๊ฐ์ฒด์ ์ธ๋ฑ์ค ์ฐพ๊ธฐ | ||
private int findCursorIndex(List<Friend> allFriends, Long cursorId) { | ||
for (int i = 0; i < allFriends.size(); i++) { | ||
if (allFriends.get(i).getToMember().getId().equals(cursorId)) { | ||
return i; | ||
} | ||
} | ||
return 0; // cursorId์ ํด๋นํ๋ ๊ฐ์ฒด๋ฅผ ์ฐพ์ง ๋ชปํ๋ฉด ์ฒ์๋ถํฐ ์์ | ||
} | ||
|
||
|
||
private static final Comparator<String> memberNameComparator = (s1, s2) -> { | ||
int length1 = s1.length(); | ||
int length2 = s2.length(); | ||
int minLength = Math.min(length1, length2); | ||
|
||
// ๊ฐ ๋ฌธ์ ๋น๊ต | ||
for (int i = 0; i < minLength; i++) { | ||
int result = compareChars(s1.charAt(i), s2.charAt(i)); | ||
if (result != 0) { | ||
return result; | ||
} | ||
} | ||
|
||
// ์๋ถ๋ถ์ด ๋์ผํ๋ฉด, ๊ธธ์ด๊ฐ ์งง์ ๊ฒ์ด ์์ผ๋ก ์ค๋๋ก ์ ๋ ฌ | ||
return Integer.compare(length1, length2); | ||
}; | ||
|
||
/** | ||
* ๋ฌธ์ ๋น๊ต ๋ฉ์๋: ํ๊ธ -> ์๋ฌธ์ -> ์ซ์ ์์ผ๋ก ์ฐ์ ์์ ์ง์ | ||
* | ||
* @param c1 | ||
* @param c2 | ||
* @return | ||
*/ | ||
private static int compareChars(char c1, char c2) { | ||
boolean isC1Korean = isKorean(c1); | ||
boolean isC2Korean = isKorean(c2); | ||
|
||
// ํ๊ธ๊ณผ ์๋ฌธ์/์ซ์๋ฅผ ๊ตฌ๋ถํ์ฌ ์ฐ์ ์์ ์ค์ | ||
if (isC1Korean && !isC2Korean) { | ||
return -1; // ํ๊ธ์ ์๋ฌธ์/์ซ์๋ณด๋ค ๋จผ์ | ||
} else if (!isC1Korean && isC2Korean) { | ||
return 1; // ์๋ฌธ์/์ซ์๋ ํ๊ธ๋ณด๋ค ๋ค | ||
} else if (Character.isDigit(c1) && Character.isDigit(c2)) { | ||
return Character.compare(c1, c2); // ๋ ๋ค ์ซ์์ธ ๊ฒฝ์ฐ ์ซ์ ๋น๊ต | ||
} else if (Character.isDigit(c1)) { | ||
return 1; // ์ซ์๋ ํญ์ ๋ค๋ก | ||
} else if (Character.isDigit(c2)) { | ||
return -1; // ์ซ์๋ ํญ์ ๋ค๋ก | ||
} else { | ||
return Character.compare(c1, c2); // ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฌธ์ ๋น๊ต (์๋ฌธ์๋ผ๋ฆฌ ๋ฑ) | ||
} | ||
} | ||
|
||
/** | ||
* ํ๊ธ ์ฌ๋ถ๋ฅผ ํ๋ณ | ||
* | ||
* @param c | ||
* @return | ||
*/ | ||
private static boolean isKorean(char c) { | ||
return (c >= 0x1100 && c <= 0x11FF) || // ํ๊ธ ์๋ชจ | ||
(c >= 0xAC00 && c <= 0xD7AF) || // ํ๊ธ ์์ | ||
(c >= 0x3130 && c <= 0x318F); // ํ๊ธ ํธํ ์๋ชจ | ||
} | ||
} |
6 changes: 2 additions & 4 deletions
6
src/main/java/com/gamegoo/repository/friend/FriendRequestsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
package com.gamegoo.repository.friend; | ||
|
||
import com.gamegoo.domain.member.Member; | ||
import com.gamegoo.domain.friend.FriendRequestStatus; | ||
import com.gamegoo.domain.friend.FriendRequests; | ||
|
||
import com.gamegoo.domain.member.Member; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface FriendRequestsRepository extends JpaRepository<FriendRequests, Long> { | ||
|
||
Optional<FriendRequests> findByFromMemberAndToMemberAndStatus(Member fromMember, | ||
Member toMember, FriendRequestStatus status); | ||
Member toMember, FriendRequestStatus status); | ||
|
||
} |
Oops, something went wrong.