Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -275,34 +275,57 @@ public PostListResDto getPostList(Long projectId, Long userId, PostType type, in
return new PostListResDto(mapped, pageInfo);
}

// FREE만 조회 요청일 때는 FREE만 페이징 (DB Pageable 사용 금지)
if (type == PostType.FREE) {
List<Post> freePosts = postRepository.findFreePosts(projectId, baseSort);
return paginatePosts(sortPosts(freePosts), page, fixedSize);
}

// FREE만 페이지네이션
Pageable pageable = PageRequest.of(page, fixedSize, baseSort);
Page<Post> freePage = postRepository.findFreePosts(projectId, pageable);
// 공지 + FREE 전체를 합쳐서 페이징 (type == null)
List<Post> notices = postRepository.findAllNotices(projectId, baseSort);
List<Post> freePosts = postRepository.findFreePosts(projectId, baseSort);

List<PostListResDto.PostSummaryDto> result = new java.util.ArrayList<>();
List<Post> combined = new ArrayList<>(notices.size() + freePosts.size());
combined.addAll(sortPosts(notices));
combined.addAll(sortPosts(freePosts));

// page==0 일 때만 공지 전부 상단에 붙이기
List<Post> notices = postRepository.findAllNotices(projectId, baseSort);
if (type == null && page == 0) {
result.addAll(notices.stream().map(this::toSummary).toList());
}
return paginatePosts(combined, page, fixedSize);
}

// FREE 페이징 결과 붙이기
result.addAll(freePage.getContent().stream().map(this::toSummary).toList());
private PostListResDto paginatePosts(List<Post> posts, int page, int size) {
int totalElements = posts.size();
int totalPages = totalElements == 0 ? 0 : (int) Math.ceil((double) totalElements / size);
int start = page * size;
int end = Math.min(start + size, totalElements);
boolean hasNext = end < totalElements;

List<PostListResDto.PostSummaryDto> result = (start >= totalElements)
? List.of()
: posts.subList(start, end).stream()
.map(this::toSummary)
.toList();

// pageInfo는 FREE 기준으로만 계산 (공지는 제외)
PostListResDto.PageInfo pageInfo = new PostListResDto.PageInfo(
freePage.getNumber(),
freePage.getSize(),
freePage.getTotalElements() + + notices.size(),
freePage.getTotalPages(),
freePage.hasNext()
page,
size,
totalElements,
totalPages,
hasNext
);

return new PostListResDto(result, pageInfo);
}

private List<Post> sortPosts(List<Post> posts) {
return posts.stream()
.sorted((a, b) -> {
int cmp = b.getCreatedAt().compareTo(a.getCreatedAt());
if (cmp != 0) return cmp;
return b.getId().compareTo(a.getId());
})
.toList();
}

private PostListResDto.PostSummaryDto toSummary(Post p) {
var u = p.getAuthor();
PostListResDto.AuthorDto authorDto = (u == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public interface PostRepository extends JpaRepository<Post, Long> {
""")
Page<Post> findFreePosts(@Param("projectId") Long projectId, Pageable pageable);

@Query("""
select p from Post p
where p.project.id = :projectId
and p.deletedAt is null
and p.postType <> com.nect.core.entity.team.workspace.enums.PostType.NOTICE
""")
List<Post> findFreePosts(@Param("projectId") Long projectId, Sort sort);

@Query("""
select p from Post p
where p.project.id = :projectId
Expand Down