Skip to content

Commit

Permalink
feat: 친구 요청 목록 API 응답 필드에 보낸 사용자 정보 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
peeerr committed Oct 16, 2024
1 parent 3cca37c commit 8d33ca5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class FriendRequestResponse {

private Long id;
private Long senderId;
private String nickname;
private String profileImageUrl;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.potatocake.everymoment.service;

import static java.util.function.Function.identity;
import static org.springframework.data.domain.Sort.Direction.DESC;

import com.potatocake.everymoment.dto.response.FriendRequestPageRequest;
Expand All @@ -14,6 +15,9 @@
import com.potatocake.everymoment.repository.MemberRepository;
import com.potatocake.everymoment.util.PagingUtil;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.ScrollPosition;
Expand Down Expand Up @@ -106,11 +110,27 @@ private Window<FriendRequest> fetchFriendRequestWindow(Long key, int size, Long
}

private List<FriendRequestResponse> convertToFriendRequestResponses(List<FriendRequest> requests) {
Set<Long> senderIds = requests.stream()
.map(request -> request.getSender().getId())
.collect(Collectors.toSet());

Map<Long, Member> senderMap = memberRepository.findAllById(senderIds)
.stream()
.collect(Collectors.toMap(Member::getId, identity()));

return requests.stream()
.map(request -> FriendRequestResponse.builder()
.id(request.getId())
.senderId(request.getSender().getId())
.build())
.map(request -> {
Member sender = senderMap.get(request.getSender().getId());
if (sender == null) {
throw new GlobalException(ErrorCode.MEMBER_NOT_FOUND);
}
return FriendRequestResponse.builder()
.id(request.getId())
.senderId(sender.getId())
.nickname(sender.getNickname())
.profileImageUrl(sender.getProfileImageUrl())
.build();
})
.toList();
}

Expand Down

0 comments on commit 8d33ca5

Please sign in to comment.