Skip to content

Commit

Permalink
Merge pull request #164 from Gamegoo-repo/refactor/161
Browse files Browse the repository at this point in the history
[Refactor/161] ์•Œ๋ฆผ ๋ชฉ๋ก ์กฐํšŒ, ์นœ๊ตฌ ๋ชฉ๋ก ์กฐํšŒ API ์ˆ˜์ •
  • Loading branch information
rimi3226 authored Aug 13, 2024
2 parents e06c7bc + 7a780ef commit f4fc041
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class FriendController {

private final FriendService friendService;

@Operation(summary = "์นœ๊ตฌ ๋ชฉ๋ก ์กฐํšŒ API", description = "ํ•ด๋‹น ํšŒ์›์˜ ์นœ๊ตฌ ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋Š” API ์ž…๋‹ˆ๋‹ค.")
@Operation(summary = "์นœ๊ตฌ ๋ชฉ๋ก ์กฐํšŒ API", description = "ํ•ด๋‹น ํšŒ์›์˜ ์นœ๊ตฌ ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋Š” API ์ž…๋‹ˆ๋‹ค. ์ด๋ฆ„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.")
@GetMapping
public ApiResponse<List<MemberResponse.friendInfoDTO>> getFriendList() {
Long memberId = JWTUtil.getCurrentUserId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@ public class NotificationController {
@Operation(summary = "์•Œ๋ฆผ ๋ชฉ๋ก ์กฐํšŒ API", description = "์•Œ๋ฆผ ํŒ์—… ํ™”๋ฉด์—์„œ ์•Œ๋ฆผ ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋Š” API ์ž…๋‹ˆ๋‹ค.")
@Parameters({
@Parameter(name = "cursor", description = "ํŽ˜์ด์ง•์„ ์œ„ํ•œ ์ปค์„œ, Long ํƒ€์ž… notificationId๋ฅผ ๋ณด๋‚ด์ฃผ์„ธ์š”. ๋ณด๋‚ด์ง€ ์•Š์œผ๋ฉด ๊ฐ€์žฅ ์ตœ๊ทผ ์•Œ๋ฆผ 10๊ฐœ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค."),
@Parameter(name = "type", description = "์•Œ๋ฆผ ํƒ€์ž… ์กฐํšŒ ํ•„ํ„ฐ, general ๋˜๋Š” friend๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
})
@GetMapping
public ApiResponse<NotificationResponse.cursorNotificationListDTO> getNotificationList(
@RequestParam(name = "type") String type,
@RequestParam(name = "cursor", required = false) Long cursor
) {

Long memberId = JWTUtil.getCurrentUserId();

Slice<Notification> notifications = notificationService.getNotificationListByCursor(
memberId, type, cursor);
memberId, cursor);

return ApiResponse.onSuccess(
NotificationConverter.toCursorNotificationListDTO(notifications));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

public interface NotificationRepositoryCustom {

Slice<Notification> findNotificationsByCursorAndType(Long memberId, String type, Long cursor);
Slice<Notification> findNotificationsByCursor(Long memberId, Long cursor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,19 @@ public class NotificationRepositoryCustomImpl implements NotificationRepositoryC
private final JPAQueryFactory queryFactory;

/**
* ์•Œ๋ฆผ ๋‚ด์—ญ ์กฐํšŒ, ์ปค์„œ ๊ธฐ๋ฐ˜ ํŽ˜์ด์ง• ๋ฐ type ํ•„ํ„ฐ๋ง ํฌํ•จ
* ์•Œ๋ฆผ ๋‚ด์—ญ ์กฐํšŒ, ์ปค์„œ ๊ธฐ๋ฐ˜ ํŽ˜์ด์ง• ํฌํ•จ
*
* @param memberId
* @param type
* @param cursor
* @return
*/
@Override
public Slice<Notification> findNotificationsByCursorAndType(Long memberId, String type,
Long cursor) {
public Slice<Notification> findNotificationsByCursor(Long memberId, Long cursor) {

List<Notification> result = queryFactory.selectFrom(notification)
.where(
notification.member.id.eq(memberId),
notificationTypeEqualReqeustType(type),
idBefore(cursor)
)
.orderBy(notification.createdAt.desc())
Expand All @@ -47,21 +45,13 @@ public Slice<Notification> findNotificationsByCursorAndType(Long memberId, Strin
result.remove(CURSOR_PAGE_SIZE);
hasNext = true;
}

PageRequest pageRequest = PageRequest.of(0, CURSOR_PAGE_SIZE);

return new SliceImpl<>(result, pageRequest, hasNext);
}


//--- BooleanExpression ---//
private BooleanExpression notificationTypeEqualReqeustType(String type) {
if (type.equals("general")) {
return notification.notificationType.id.in(5, 6, 7, 8);
} else {
return notification.notificationType.id.in(1, 2, 3, 4);
}
}

private BooleanExpression idBefore(Long cursor) {
return cursor != null ? notification.id.lt(cursor) : null;
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/gamegoo/service/member/BlockService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class BlockService {
private final ChatCommandService chatCommandService;
private final FriendService friendService;

Integer pageSize = 9;
private final static Integer PAGE_SIZE = 10;

/**
* memberId์— ํ•ด๋‹นํ•˜๋Š” ํšŒ์›์ด targetMemberId์— ํ•ด๋‹นํ•˜๋Š” ํšŒ์›์„ ์ฐจ๋‹จ
Expand All @@ -53,15 +53,15 @@ public Member blockMember(Long memberId, Long targetMemberId) {

// ์ด๋ฏธ ์ฐจ๋‹จํ•œ ํšŒ์›์ธ์ง€ ๊ฒ€์ฆ
boolean isblocked = blockRepository.existsByBlockerMemberAndBlockedMember(member,
targetMember);
targetMember);
if (isblocked) {
throw new BlockHandler(ErrorStatus.ALREADY_BLOCKED);
}

// block ์—”ํ‹ฐํ‹ฐ ์ƒ์„ฑ ๋ฐ ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘
Block block = Block.builder()
.blockedMember(targetMember)
.build();
.blockedMember(targetMember)
.build();
block.setBlockerMember(member);

blockRepository.save(block);
Expand Down Expand Up @@ -98,10 +98,10 @@ public Page<Member> getBlockList(Long memberId, Integer pageIdx) {
// member ์—”ํ‹ฐํ‹ฐ ์กฐํšŒ
Member member = profileService.findMember(memberId);

PageRequest pageRequest = PageRequest.of(pageIdx, pageSize);
PageRequest pageRequest = PageRequest.of(pageIdx, PAGE_SIZE);

return memberRepository.findBlockedMembersByBlockerIdAndNotBlind(member.getId(),
pageRequest);
pageRequest);
}

/**
Expand All @@ -117,7 +117,7 @@ public void unBlockMember(Long memberId, Long targetMemberId) {

// targetMember๊ฐ€ ์ฐจ๋‹จ ์‹ค์ œ๋กœ ์ฐจ๋‹จ ๋ชฉ๋ก์— ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์ฆ
Block block = blockRepository.findByBlockerMemberAndBlockedMember(member, targetMember)
.orElseThrow(() -> new BlockHandler(ErrorStatus.TARGET_MEMBER_NOT_BLOCKED));
.orElseThrow(() -> new BlockHandler(ErrorStatus.TARGET_MEMBER_NOT_BLOCKED));

block.removeBlockerMember(member); // ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„ ์ œ๊ฑฐ
blockRepository.delete(block);
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/gamegoo/service/member/FriendService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.gamegoo.repository.friend.FriendRequestsRepository;
import com.gamegoo.service.notification.NotificationService;
import com.gamegoo.util.MemberUtils;
import com.gamegoo.util.SortUtil;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand All @@ -35,7 +36,14 @@ public class FriendService {
*/
@Transactional(readOnly = true)
public List<Friend> getFriends(Long memberId) {
return friendRepository.findAllByFromMemberId(memberId);
List<Friend> friendList = friendRepository.findAllByFromMemberId(memberId);

// ์นœ๊ตฌ ํšŒ์›์˜ gameName ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
friendList.sort(
(f1, f2) -> SortUtil.memberNameComparator.compare(f1.getToMember().getGameName(),
f2.getToMember().getGameName()));

return friendList;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class NotificationService {
private final NotificationRepository notificationRepository;
private final ProfileService profileService;

private static final int PAGE_SIZE = 5;
private static final int PAGE_SIZE = 10;

/**
* ์ƒˆ๋กœ์šด ์•Œ๋ฆผ ์ƒ์„ฑ ๋ฐ ์ €์žฅ ๋ฉ”์†Œ๋“œ
Expand Down Expand Up @@ -90,15 +90,10 @@ public Notification createNotification(NotificationTypeTitle notificationTypeTit
* @param cursor
* @return
*/
public Slice<Notification> getNotificationListByCursor(Long memberId,
String type, Long cursor) {
public Slice<Notification> getNotificationListByCursor(Long memberId, Long cursor) {
Member member = profileService.findMember(memberId);

if (!"general".equals(type) && !"friend".equals(type)) {
throw new NotificationHandler(ErrorStatus.INVALID_NOTIFICATION_TYPE);
}

return notificationRepository.findNotificationsByCursorAndType(member.getId(), type,
return notificationRepository.findNotificationsByCursor(member.getId(),
cursor);
}

Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/gamegoo/util/SortUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.gamegoo.util;

import java.util.Comparator;

public class SortUtil {

public static 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);
};

// ๋ฌธ์ž ๋น„๊ต ๋ฉ”์„œ๋“œ: ํ•œ๊ธ€ -> ์˜๋ฌธ์ž -> ์ˆซ์ž ์ˆœ์œผ๋กœ ์šฐ์„ ์ˆœ์œ„ ์ง€์ •
private static int compareChars(char c1, char c2) {
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 if (Character.isAlphabetic(c1) && Character.isAlphabetic(c2)) {
return Character.compare(c1, c2);
} else if (Character.isAlphabetic(c1)) {
return 1; // ์˜๋ฌธ์ž๋Š” ํ•œ๊ธ€๋ณด๋‹ค ๋’ค
} else if (Character.isAlphabetic(c2)) {
return -1; // ์˜๋ฌธ์ž๋Š” ํ•œ๊ธ€๋ณด๋‹ค ๋’ค
} else {
return Character.compare(c1, c2); // ๊ธฐ๋ณธ์ ์œผ๋กœ ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’ ๋น„๊ต
}
}
}

0 comments on commit f4fc041

Please sign in to comment.