Skip to content

Commit 474558f

Browse files
committed
reafactor(community): 커뮤니티 게시글 조회 페이징 처리
1 parent 8abf87b commit 474558f

File tree

4 files changed

+48
-36
lines changed

4 files changed

+48
-36
lines changed

src/main/java/com/somemore/community/repository/board/CommunityBoardRepository.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
import com.somemore.community.domain.CommunityBoard;
44
import com.somemore.community.repository.mapper.CommunityBoardView;
5+
import org.springframework.data.domain.Page;
6+
import org.springframework.data.domain.Pageable;
57

6-
import java.util.List;
78
import java.util.Optional;
89
import java.util.UUID;
910

1011
public interface CommunityBoardRepository {
1112
CommunityBoard save(CommunityBoard communityBoard);
1213
Optional<CommunityBoard> findById(Long id);
13-
List<CommunityBoardView> getCommunityBoards();
14-
List<CommunityBoardView> findByWriterId(UUID writerId);
14+
Page<CommunityBoardView> findCommunityBoards(Pageable pageable);
15+
Page<CommunityBoardView> findByWriterId(UUID writerId, Pageable pageable);
1516
boolean existsById(Long id);
1617
default boolean doesNotExistById(Long id) {
1718
return !existsById(id);

src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import com.somemore.community.domain.QCommunityBoard;
99
import com.somemore.volunteer.domain.QVolunteer;
1010
import lombok.RequiredArgsConstructor;
11+
import org.springframework.data.domain.Page;
12+
import org.springframework.data.domain.Pageable;
13+
import org.springframework.data.support.PageableExecutionUtils;
1114
import org.springframework.stereotype.Repository;
1215

1316
import java.util.List;
@@ -21,15 +24,16 @@ public class CommunityBoardRepositoryImpl implements CommunityBoardRepository {
2124
private final JPAQueryFactory queryFactory;
2225
private final CommunityBoardJpaRepository communityBoardJpaRepository;
2326

27+
private static final QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
28+
private static final QVolunteer volunteer = QVolunteer.volunteer;
29+
2430
@Override
2531
public CommunityBoard save(CommunityBoard communityBoard) {
2632
return communityBoardJpaRepository.save(communityBoard);
2733
}
2834

2935
@Override
3036
public Optional<CommunityBoard> findById(Long id) {
31-
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
32-
3337
return Optional.ofNullable(queryFactory
3438
.selectFrom(communityBoard)
3539
.where(communityBoard.id.eq(id)
@@ -38,18 +42,30 @@ public Optional<CommunityBoard> findById(Long id) {
3842
}
3943

4044
@Override
41-
public List<CommunityBoardView> getCommunityBoards() {
42-
return getCommunityBoardsQuery()
45+
public Page<CommunityBoardView> findCommunityBoards(Pageable pageable) {
46+
List<CommunityBoardView> content = getCommunityBoardsQuery()
4347
.where(QCommunityBoard.communityBoard.deleted.eq(false))
4448
.fetch();
49+
50+
JPAQuery<Long> countQuery = queryFactory
51+
.select(communityBoard.count())
52+
.from(communityBoard);
53+
54+
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
4555
}
4656

4757
@Override
48-
public List<CommunityBoardView> findByWriterId(UUID writerId) {
49-
return getCommunityBoardsQuery()
58+
public Page<CommunityBoardView> findByWriterId(UUID writerId, Pageable pageable) {
59+
List<CommunityBoardView> content = getCommunityBoardsQuery()
5060
.where(QCommunityBoard.communityBoard.writerId.eq(writerId)
5161
.and(QCommunityBoard.communityBoard.deleted.eq(false)))
5262
.fetch();
63+
64+
JPAQuery<Long> countQuery = queryFactory
65+
.select(communityBoard.count())
66+
.from(communityBoard);
67+
68+
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
5369
}
5470

5571
@Override
@@ -58,9 +74,6 @@ public boolean existsById(Long id) {
5874
}
5975

6076
private JPAQuery<CommunityBoardView> getCommunityBoardsQuery() {
61-
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
62-
QVolunteer volunteer = QVolunteer.volunteer;
63-
6477
return queryFactory
6578
.select(Projections.constructor(CommunityBoardView.class,
6679
communityBoard,

src/main/java/com/somemore/community/service/board/CommunityBoardQueryService.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
import com.somemore.community.domain.CommunityBoard;
44
import com.somemore.community.repository.mapper.CommunityBoardView;
5-
import com.somemore.community.dto.response.CommunityBoardGetDetailResponseDto;
6-
import com.somemore.community.dto.response.CommunityBoardGetResponseDto;
5+
import com.somemore.community.dto.response.CommunityBoardDetailResponseDto;
6+
import com.somemore.community.dto.response.CommunityBoardResponseDto;
77
import com.somemore.community.repository.board.CommunityBoardRepository;
88
import com.somemore.community.usecase.board.CommunityBoardQueryUseCase;
99
import com.somemore.global.exception.BadRequestException;
1010
import lombok.RequiredArgsConstructor;
11+
import org.springframework.data.domain.Page;
12+
import org.springframework.data.domain.PageRequest;
13+
import org.springframework.data.domain.Pageable;
1114
import org.springframework.stereotype.Service;
1215
import org.springframework.transaction.annotation.Transactional;
1316

14-
import java.util.List;
1517
import java.util.UUID;
16-
import java.util.function.Function;
1718

1819
import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD;
1920

@@ -23,29 +24,26 @@
2324
public class CommunityBoardQueryService implements CommunityBoardQueryUseCase {
2425

2526
private final CommunityBoardRepository communityBoardRepository;
27+
private static final int PAGE_SIZE = 10;
2628

2729
@Override
28-
public List<CommunityBoardGetResponseDto> getCommunityBoards() {
29-
List<CommunityBoardView> boards = communityBoardRepository.getCommunityBoards();
30-
return mapEntitiesToDtos(boards, CommunityBoardGetResponseDto::fromEntity);
30+
public Page<CommunityBoardResponseDto> getCommunityBoards(int page) {
31+
Pageable pageable = PageRequest.of(page, PAGE_SIZE);
32+
Page<CommunityBoardView> boards = communityBoardRepository.findCommunityBoards(pageable);
33+
return boards.map(CommunityBoardResponseDto::from);
3134
}
3235

3336
@Override
34-
public List<CommunityBoardGetResponseDto> getCommunityBoardsByWriterId(UUID writerId) {
35-
List<CommunityBoardView> boards = communityBoardRepository.findByWriterId(writerId);
36-
return mapEntitiesToDtos(boards, CommunityBoardGetResponseDto::fromEntity);
37+
public Page<CommunityBoardResponseDto> getCommunityBoardsByWriterId(UUID writerId, int page) {
38+
Pageable pageable = PageRequest.of(page, PAGE_SIZE);
39+
Page<CommunityBoardView> boards = communityBoardRepository.findByWriterId(writerId ,pageable);
40+
return boards.map(CommunityBoardResponseDto::from);
3741
}
3842

3943
@Override
40-
public CommunityBoardGetDetailResponseDto getCommunityBoardDetail(Long id) {
44+
public CommunityBoardDetailResponseDto getCommunityBoardDetail(Long id) {
4145
CommunityBoard board = communityBoardRepository.findById(id)
4246
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage()));
43-
return CommunityBoardGetDetailResponseDto.fromEntity(board);
44-
}
45-
46-
private <T, R> List<R> mapEntitiesToDtos(List<T> entities, Function<T, R> mapper) {
47-
return entities.stream()
48-
.map(mapper)
49-
.toList();
47+
return CommunityBoardDetailResponseDto.from(board);
5048
}
5149
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.somemore.community.usecase.board;
22

3-
import com.somemore.community.dto.response.CommunityBoardGetDetailResponseDto;
4-
import com.somemore.community.dto.response.CommunityBoardGetResponseDto;
3+
import com.somemore.community.dto.response.CommunityBoardDetailResponseDto;
4+
import com.somemore.community.dto.response.CommunityBoardResponseDto;
5+
import org.springframework.data.domain.Page;
56

6-
import java.util.List;
77
import java.util.UUID;
88

99
public interface CommunityBoardQueryUseCase {
10-
List<CommunityBoardGetResponseDto> getCommunityBoards();
11-
List<CommunityBoardGetResponseDto> getCommunityBoardsByWriterId(UUID writerId);
12-
CommunityBoardGetDetailResponseDto getCommunityBoardDetail(Long id);
10+
Page<CommunityBoardResponseDto> getCommunityBoards(int page);
11+
Page<CommunityBoardResponseDto> getCommunityBoardsByWriterId(UUID writerId, int page);
12+
CommunityBoardDetailResponseDto getCommunityBoardDetail(Long id);
1313
}

0 commit comments

Comments
 (0)