Skip to content

Commit

Permalink
Merge branch 'develop' into refactor/207
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunjin3395 committed Sep 8, 2024
2 parents 2a49c9b + 8282fca commit ecdb706
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum ErrorStatus implements BaseErrorCode {

// ํŽ˜์ด์ง• ๊ด€๋ จ ์—๋Ÿฌ
PAGE_INVALID(HttpStatus.BAD_REQUEST, "PAGE401", "ํŽ˜์ด์ง€ ๊ฐ’์€ 1 ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."),
CURSOR_INVALID(HttpStatus.BAD_REQUEST, "PAGE402", "์ปค์„œ ๊ฐ’์€ 1 ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."),

// Member ๊ด€๋ จ ์—๋Ÿฌ
PASSWORD_INVALID(HttpStatus.BAD_REQUEST, "MEMBER400", "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋ถˆ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค."),
Expand Down Expand Up @@ -155,6 +156,8 @@ public enum ErrorStatus implements BaseErrorCode {
MEMBERS_NOT_FRIEND(HttpStatus.BAD_REQUEST, "FRIEND408", "๋‘ ํšŒ์›์€ ์นœ๊ตฌ ๊ด€๊ณ„๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค."),
ALREADY_STAR_FRIEND(HttpStatus.BAD_REQUEST, "FRIEND409", "์ด๋ฏธ ์ฆ๊ฒจ์ฐพ๊ธฐ ๋˜์–ด ์žˆ๋Š” ์นœ๊ตฌ์ž…๋‹ˆ๋‹ค."),
NOT_STAR_FRIEND(HttpStatus.BAD_REQUEST, "FRIEND410", "์ฆ๊ฒจ์ฐพ๊ธฐ ๋˜์–ด ์žˆ๋Š” ์นœ๊ตฌ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค."),
FRIEND_SEARCH_QUERY_BAD_REQUEST(HttpStatus.BAD_REQUEST, "FRIEND411",
"์นœ๊ตฌ ๊ฒ€์ƒ‰ ์ฟผ๋ฆฌ๋Š” 100์ž ์ดํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."),

// ์•Œ๋ฆผ ๊ด€๋ จ ์—๋Ÿฌ
NOTIFICATION_TYPE_NOT_FOUND(HttpStatus.NOT_FOUND, "NOTI401", "ํ•ด๋‹น ์•Œ๋ฆผ ํƒ€์ž… ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
Expand Down
44 changes: 38 additions & 6 deletions src/main/java/com/gamegoo/controller/member/FriendController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.gamegoo.converter.MemberConverter;
import com.gamegoo.domain.friend.Friend;
import com.gamegoo.dto.member.MemberResponse;
import com.gamegoo.dto.member.MemberResponse.friendInfoDTO;
import com.gamegoo.dto.member.MemberResponse.friendRequestResultDTO;
import com.gamegoo.dto.member.MemberResponse.starFriendResultDTO;
import com.gamegoo.service.member.FriendService;
Expand All @@ -15,12 +16,14 @@
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Slice;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
Expand All @@ -32,17 +35,46 @@ public class FriendController {

private final FriendService friendService;

@Operation(summary = "์นœ๊ตฌ ๋ชฉ๋ก ์กฐํšŒ API", description = "ํ•ด๋‹น ํšŒ์›์˜ ์นœ๊ตฌ ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋Š” API ์ž…๋‹ˆ๋‹ค. ์ด๋ฆ„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.")
@Operation(summary = "์นœ๊ตฌ ๋ชฉ๋ก ์กฐํšŒ API", description =
"ํ•ด๋‹น ํšŒ์›์˜ ์นœ๊ตฌ ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋Š” API ์ž…๋‹ˆ๋‹ค. ์ด๋ฆ„ ์˜ค๋ฆ„์ฐจ์ˆœ(ํ•œ๊ธ€-์˜๋ฌธ-์ˆซ์ž ์ˆœ)์œผ๋กœ ์ •๋ ฌํ•ด ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.\n\n"
+ "cursor๋ฅผ ๋ณด๋‚ด์ง€ ์•Š์œผ๋ฉด ์ƒ์œ„ 10๊ฐœ ์นœ๊ตฌ ๋ชฉ๋ก์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.")
@Parameter(name = "cursor", description = "ํŽ˜์ด์ง•์„ ์œ„ํ•œ ์ปค์„œ, ์ด์ „ ์นœ๊ตฌ ๋ชฉ๋ก ์กฐํšŒ์—์„œ ์‘๋‹ต๋ฐ›์€ next_cursor๋ฅผ ๋ณด๋‚ด์ฃผ์„ธ์š”.")
@GetMapping
public ApiResponse<List<MemberResponse.friendInfoDTO>> getFriendList() {
public ApiResponse<MemberResponse.friendListDTO> getFriendList(
@RequestParam(name = "cursor", required = false) Long cursorId
) {
Long memberId = JWTUtil.getCurrentUserId();
List<Friend> friends = friendService.getFriends(memberId);

List<MemberResponse.friendInfoDTO> friendInfoDTOList = friends.stream()
.map(MemberConverter::toFriendInfoDto).collect(
Collectors.toList());
Slice<Friend> friends = friendService.getFriends(memberId, cursorId);

return ApiResponse.onSuccess(MemberConverter.toFriendListDTO(friends));

}

@Operation(summary = "์†Œํ™˜์‚ฌ๋ช…์œผ๋กœ ์นœ๊ตฌ ๊ฒ€์ƒ‰ API", description =
"ํ•ด๋‹น ํšŒ์›์˜ ์นœ๊ตฌ ์ค‘, query string์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์†Œํ™˜์‚ฌ๋ช…์„ ๊ฐ€์ง„ ๋ชจ๋“  ์นœ๊ตฌ ๋ชฉ๋ก์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.")
@Parameter(name = "query", description = "์นœ๊ตฌ ๋ชฉ๋ก ๊ฒ€์ƒ‰์„ ์œ„ํ•œ ์†Œํ™˜์‚ฌ๋ช… string์œผ๋กœ, 100์ž ์ดํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.")
@GetMapping("/search")
public ApiResponse<List<friendInfoDTO>> searchFriend(
@RequestParam(name = "query") String query
) {
Long memberId = JWTUtil.getCurrentUserId();

List<Friend> friends = friendService.searchFriendByQueryString(memberId, query);

List<friendInfoDTO> friendInfoDTOList = friends.stream()
.map(MemberConverter::toFriendInfoDto).collect(Collectors.toList());

return ApiResponse.onSuccess(friendInfoDTOList);
}

@Operation(summary = "๋ชจ๋“  ์นœ๊ตฌ id ์กฐํšŒ API", description = "ํ•ด๋‹น ํšŒ์›์˜ ๋ชจ๋“  ์นœ๊ตฌ id ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋Š” API ์ž…๋‹ˆ๋‹ค.\n\n"
+ "์ •๋ ฌ ๊ธฐ๋Šฅ ์—†์Œ, socket์„œ๋ฒ„์šฉ API ์ž…๋‹ˆ๋‹ค.")
@GetMapping("/ids")
public ApiResponse<List<Long>> getFriendIds() {
Long memberId = JWTUtil.getCurrentUserId();

return ApiResponse.onSuccess(friendService.getFriendIds(memberId));

}

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/gamegoo/converter/MemberConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.gamegoo.domain.friend.Friend;
import com.gamegoo.domain.member.Member;
import com.gamegoo.dto.member.MemberResponse;
import com.gamegoo.dto.member.MemberResponse.friendInfoDTO;
import com.gamegoo.util.MemberUtils;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Slice;

public class MemberConverter {

Expand Down Expand Up @@ -124,12 +126,27 @@ public static MemberResponse.memberProfileDTO toMemberProfileDTO(Member member,

}

public static MemberResponse.friendListDTO toFriendListDTO(Slice<Friend> friends) {
List<friendInfoDTO> friendInfoDTOList = friends.stream()
.map(MemberConverter::toFriendInfoDto).collect(Collectors.toList());
return MemberResponse.friendListDTO.builder()
.friendInfoDTOList(friendInfoDTOList)
.list_size(friendInfoDTOList.size())
.has_next(friends.hasNext())
.next_cursor(
friends.hasNext() ? friends.getContent().get(friendInfoDTOList.size() - 1)
.getToMember().getId()
: null)
.build();
}

public static MemberResponse.friendInfoDTO toFriendInfoDto(Friend friend) {
return MemberResponse.friendInfoDTO.builder()
.memberId(friend.getToMember().getId())
.name(friend.getToMember().getGameName())
.memberProfileImg(friend.getToMember().getProfileImage())
.isLiked(friend.getIsLiked())
.isBlind(friend.getToMember().getBlind())
.build();
}

Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/gamegoo/dto/member/MemberResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ public static class memberProfileDTO {
List<ChampionResponseDTO> championResponseDTOList;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class friendListDTO {

List<friendInfoDTO> friendInfoDTOList;
Integer list_size;
Boolean has_next;
Long next_cursor;
}

@Builder
@Getter
@NoArgsConstructor
Expand All @@ -142,7 +154,8 @@ public static class friendInfoDTO {
Long memberId;
String name;
Integer memberProfileImg;
boolean isLiked;
Boolean isLiked;
Boolean isBlind;
}

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static class notificationDTO {
int notificationType;
String content;
String pageUrl;
boolean read;
Boolean read;
LocalDateTime createdAt;
}

Expand Down
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.Friend;

import com.gamegoo.domain.member.Member;
import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface FriendRepository extends JpaRepository<Friend, Long> {
public interface FriendRepository extends JpaRepository<Friend, Long>, FriendRepositoryCustom {

List<Friend> findAllByFromMemberId(Long memberId);

Expand Down
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);
}
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); // ํ•œ๊ธ€ ํ˜ธํ™˜ ์ž๋ชจ
}
}
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);

}
Loading

0 comments on commit ecdb706

Please sign in to comment.