좌충우돌 개발공부

TIL(20240624) [아웃소싱 프로젝트: fix 팔로우]


📌 Spring

💡 아웃소싱 프로젝트: fix 팔로우


public List<PublicPostResponseDto> getFollowedPosts(User user, int page, int pageSize) {
    Pageable pageable = PageRequest.of(page, pageSize);

    User currentUser = userService.findByUserSeq(user.getUserSeq());
    List<Follow> follows = followRepository.findByFromUser(currentUser);

    List<PublicPostResponseDto> publicPostResponseDtos = new ArrayList<>();
    for (Follow followerList : follows) {
      User followedUser = followerList.getToUser();
      Page<PublicPost> publicPosts = postRepository.findByUserOrderByCreatedAtDesc(followedUser, pageable);
      if (publicPosts.isEmpty()) {
        throw new CustomException(ErrorCode.POST_EMPTY);
      }
      for(PublicPost publicPost : publicPosts) {
        PublicPostResponseDto responseDto = new PublicPostResponseDto(publicPost);
        publicPostResponseDtos.add(responseDto);
      }
    }
    return publicPostResponseDtos;
  }