Skip to content

Commit c8ccff0

Browse files
committed
fix: CoummunityBoardView 이름 변경 및 코드 리뷰 반영
- entity -> dto 메서드 추출 - CommunityBoardView record로 변경 - CommunityBoard 조회 쿼리 추출
1 parent 653033b commit c8ccff0

File tree

8 files changed

+65
-74
lines changed

8 files changed

+65
-74
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.somemore.community.domain;
2+
3+
public record CommunityBoardView(
4+
CommunityBoard communityBoard,
5+
String writerNickname) {
6+
}

src/main/java/com/somemore/community/domain/CommunityBoardWithNickname.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/main/java/com/somemore/community/dto/response/CommunityBoardGetResponseDto.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
44
import com.fasterxml.jackson.databind.annotation.JsonNaming;
5-
import com.somemore.community.domain.CommunityBoardWithNickname;
5+
import com.somemore.community.domain.CommunityBoardView;
66

77
import java.time.LocalDateTime;
88

@@ -13,12 +13,12 @@ public record CommunityBoardGetResponseDto(
1313
String writerNickname,
1414
LocalDateTime createdAt
1515
) {
16-
public static CommunityBoardGetResponseDto fromEntity(CommunityBoardWithNickname board) {
16+
public static CommunityBoardGetResponseDto fromEntity(CommunityBoardView board) {
1717
return new CommunityBoardGetResponseDto(
18-
board.getCommunityBoard().getId(),
19-
board.getCommunityBoard().getTitle(),
20-
board.getWriterNickname(),
21-
board.getCommunityBoard().getCreatedAt()
18+
board.communityBoard().getId(),
19+
board.communityBoard().getTitle(),
20+
board.writerNickname(),
21+
board.communityBoard().getCreatedAt()
2222
);
2323
}
2424
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.somemore.community.repository;
22

33
import com.somemore.community.domain.CommunityBoard;
4-
import com.somemore.community.domain.CommunityBoardWithNickname;
4+
import com.somemore.community.domain.CommunityBoardView;
55

66
import java.util.List;
77
import java.util.Optional;
@@ -10,7 +10,7 @@
1010
public interface CommunityBoardRepository {
1111
CommunityBoard save(CommunityBoard communityBoard);
1212
Optional<CommunityBoard> findById(Long id);
13-
List<CommunityBoardWithNickname> getCommunityBoards();
14-
List<CommunityBoardWithNickname> findByWriterId(UUID writerId);
13+
List<CommunityBoardView> getCommunityBoards();
14+
List<CommunityBoardView> findByWriterId(UUID writerId);
1515
void deleteAllInBatch();
1616
}

src/main/java/com/somemore/community/repository/CommunityRepositoryImpl.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.somemore.community.repository;
22

33
import com.querydsl.core.types.Projections;
4+
import com.querydsl.jpa.impl.JPAQuery;
45
import com.querydsl.jpa.impl.JPAQueryFactory;
56
import com.somemore.community.domain.CommunityBoard;
6-
import com.somemore.community.domain.CommunityBoardWithNickname;
7+
import com.somemore.community.domain.CommunityBoardView;
78
import com.somemore.community.domain.QCommunityBoard;
89
import com.somemore.volunteer.domain.QVolunteer;
910
import lombok.RequiredArgsConstructor;
@@ -37,39 +38,34 @@ public Optional<CommunityBoard> findById(Long id) {
3738
}
3839

3940
@Override
40-
public List<CommunityBoardWithNickname> getCommunityBoards() {
41-
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
42-
QVolunteer volunteer = QVolunteer.volunteer;
43-
44-
return queryFactory
45-
.select(Projections.constructor(CommunityBoardWithNickname.class,
46-
communityBoard,
47-
volunteer.nickname))
48-
.from(communityBoard)
49-
.join(volunteer).on(communityBoard.writerId.eq(volunteer.id))
50-
.where(communityBoard.deleted.eq(false))
51-
.orderBy(communityBoard.createdAt.desc())
41+
public List<CommunityBoardView> getCommunityBoards() {
42+
return getCommunityBoardsQuery()
43+
.where(QCommunityBoard.communityBoard.deleted.eq(false))
5244
.fetch();
5345
}
5446

5547
@Override
56-
public List<CommunityBoardWithNickname> findByWriterId(UUID writerId) {
48+
public List<CommunityBoardView> findByWriterId(UUID writerId) {
49+
return getCommunityBoardsQuery()
50+
.where(QCommunityBoard.communityBoard.writerId.eq(writerId)
51+
.and(QCommunityBoard.communityBoard.deleted.eq(false)))
52+
.fetch();
53+
}
54+
55+
private JPAQuery<CommunityBoardView> getCommunityBoardsQuery() {
5756
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
5857
QVolunteer volunteer = QVolunteer.volunteer;
5958

6059
return queryFactory
61-
.select(Projections.constructor(CommunityBoardWithNickname.class,
60+
.select(Projections.constructor(CommunityBoardView.class,
6261
communityBoard,
6362
volunteer.nickname))
6463
.from(communityBoard)
6564
.join(volunteer).on(communityBoard.writerId.eq(volunteer.id))
66-
.where(communityBoard.writerId.eq(writerId)
67-
.and(communityBoard.deleted.eq(false))
68-
)
69-
.orderBy(communityBoard.createdAt.desc())
70-
.fetch();
65+
.orderBy(communityBoard.createdAt.desc());
7166
}
7267

68+
7369
@Override
7470
public void deleteAllInBatch() {
7571
communityBoardJpaRepository.deleteAllInBatch();
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.somemore.community.service;
22

33
import com.somemore.community.domain.CommunityBoard;
4-
import com.somemore.community.domain.CommunityBoardWithNickname;
4+
import com.somemore.community.domain.CommunityBoardView;
55
import com.somemore.community.dto.response.CommunityBoardGetDetailResponseDto;
66
import com.somemore.community.dto.response.CommunityBoardGetResponseDto;
77
import com.somemore.community.repository.CommunityBoardRepository;
@@ -13,10 +13,10 @@
1313

1414
import java.util.List;
1515
import java.util.UUID;
16+
import java.util.function.Function;
1617

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

19-
2020
@RequiredArgsConstructor
2121
@Transactional(readOnly = true)
2222
@Service
@@ -26,20 +26,14 @@ public class CommunityBoardQueryService implements CommunityBoardQueryUseCase {
2626

2727
@Override
2828
public List<CommunityBoardGetResponseDto> getCommunityBoards() {
29-
List<CommunityBoardWithNickname> boards = communityBoardRepository.getCommunityBoards();
30-
31-
return boards.stream()
32-
.map(CommunityBoardGetResponseDto::fromEntity)
33-
.toList();
29+
List<CommunityBoardView> boards = communityBoardRepository.getCommunityBoards();
30+
return mapEntitiesToDtos(boards, CommunityBoardGetResponseDto::fromEntity);
3431
}
3532

3633
@Override
3734
public List<CommunityBoardGetResponseDto> getCommunityBoardsByWriterId(UUID writerId) {
38-
List<CommunityBoardWithNickname> boards = communityBoardRepository.findByWriterId(writerId);
39-
40-
return boards.stream()
41-
.map(CommunityBoardGetResponseDto::fromEntity)
42-
.toList();
35+
List<CommunityBoardView> boards = communityBoardRepository.findByWriterId(writerId);
36+
return mapEntitiesToDtos(boards, CommunityBoardGetResponseDto::fromEntity);
4337
}
4438

4539
@Override
@@ -48,4 +42,10 @@ public CommunityBoardGetDetailResponseDto getCommunityBoardDetail(Long id) {
4842
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage()));
4943
return CommunityBoardGetDetailResponseDto.fromEntity(board);
5044
}
51-
}
45+
46+
private <T, R> List<R> mapEntitiesToDtos(List<T> entities, Function<T, R> mapper) {
47+
return entities.stream()
48+
.map(mapper)
49+
.toList();
50+
}
51+
}

src/test/java/com/somemore/community/repository/CommunityRepositoryTest.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.somemore.IntegrationTestSupport;
44
import com.somemore.auth.oauth.OAuthProvider;
55
import com.somemore.community.domain.CommunityBoard;
6-
import com.somemore.community.domain.CommunityBoardWithNickname;
6+
import com.somemore.community.domain.CommunityBoardView;
77
import com.somemore.volunteer.domain.Volunteer;
88
import com.somemore.volunteer.repository.VolunteerRepository;
99
import org.junit.jupiter.api.DisplayName;
@@ -96,18 +96,18 @@ void getCommunityBoards() {
9696
communityBoardRepository.save(communityBoard2);
9797

9898
//when
99-
List<CommunityBoardWithNickname> communityBoards = communityBoardRepository.getCommunityBoards();
99+
List<CommunityBoardView> communityBoards = communityBoardRepository.getCommunityBoards();
100100

101101
//then
102102
assertThat(communityBoards).hasSize(2);
103-
assertThat(communityBoards.getFirst().getCommunityBoard().getId()).isEqualTo(communityBoard2.getId());
104-
assertThat(communityBoards.getFirst().getCommunityBoard().getTitle()).isEqualTo(communityBoard2.getTitle());
105-
assertThat(communityBoards.getFirst().getWriterNickname()).isEqualTo(volunteer.getNickname());
106-
assertThat(communityBoards.getFirst().getCommunityBoard().getCreatedAt()).isEqualTo(communityBoard2.getCreatedAt());
107-
assertThat(communityBoards.getLast().getCommunityBoard().getId()).isEqualTo(communityBoard1.getId());
108-
assertThat(communityBoards.getLast().getCommunityBoard().getTitle()).isEqualTo(communityBoard1.getTitle());
109-
assertThat(communityBoards.getLast().getWriterNickname()).isEqualTo(volunteer.getNickname());
110-
assertThat(communityBoards.getLast().getCommunityBoard().getCreatedAt()).isEqualTo(communityBoard1.getCreatedAt());
103+
assertThat(communityBoards.getFirst().communityBoard().getId()).isEqualTo(communityBoard2.getId());
104+
assertThat(communityBoards.getFirst().communityBoard().getTitle()).isEqualTo(communityBoard2.getTitle());
105+
assertThat(communityBoards.getFirst().writerNickname()).isEqualTo(volunteer.getNickname());
106+
assertThat(communityBoards.getFirst().communityBoard().getCreatedAt()).isEqualTo(communityBoard2.getCreatedAt());
107+
assertThat(communityBoards.getLast().communityBoard().getId()).isEqualTo(communityBoard1.getId());
108+
assertThat(communityBoards.getLast().communityBoard().getTitle()).isEqualTo(communityBoard1.getTitle());
109+
assertThat(communityBoards.getLast().writerNickname()).isEqualTo(volunteer.getNickname());
110+
assertThat(communityBoards.getLast().communityBoard().getCreatedAt()).isEqualTo(communityBoard1.getCreatedAt());
111111
}
112112

113113
@DisplayName("저장된 커뮤니티 게시글 리스트를 작성자별로 조회할 수 있다. (Repository)")
@@ -144,17 +144,17 @@ void getCommunityBoardsByWriterId() {
144144
communityBoardRepository.save(communityBoard3);
145145

146146
//when
147-
List<CommunityBoardWithNickname> communityBoards = communityBoardRepository.findByWriterId(volunteer.getId());
147+
List<CommunityBoardView> communityBoards = communityBoardRepository.findByWriterId(volunteer.getId());
148148

149149
//then
150150
assertThat(communityBoards).hasSize(2);
151-
assertThat(communityBoards.getFirst().getCommunityBoard().getId()).isEqualTo(communityBoard2.getId());
152-
assertThat(communityBoards.getFirst().getCommunityBoard().getTitle()).isEqualTo(communityBoard2.getTitle());
153-
assertThat(communityBoards.getFirst().getWriterNickname()).isEqualTo(volunteer.getNickname());
154-
assertThat(communityBoards.getFirst().getCommunityBoard().getCreatedAt()).isEqualTo(communityBoard2.getCreatedAt());
155-
assertThat(communityBoards.getLast().getCommunityBoard().getId()).isEqualTo(communityBoard1.getId());
156-
assertThat(communityBoards.getLast().getCommunityBoard().getTitle()).isEqualTo(communityBoard1.getTitle());
157-
assertThat(communityBoards.getLast().getWriterNickname()).isEqualTo(volunteer.getNickname());
158-
assertThat(communityBoards.getLast().getCommunityBoard().getCreatedAt()).isEqualTo(communityBoard1.getCreatedAt());
151+
assertThat(communityBoards.getFirst().communityBoard().getId()).isEqualTo(communityBoard2.getId());
152+
assertThat(communityBoards.getFirst().communityBoard().getTitle()).isEqualTo(communityBoard2.getTitle());
153+
assertThat(communityBoards.getFirst().writerNickname()).isEqualTo(volunteer.getNickname());
154+
assertThat(communityBoards.getFirst().communityBoard().getCreatedAt()).isEqualTo(communityBoard2.getCreatedAt());
155+
assertThat(communityBoards.getLast().communityBoard().getId()).isEqualTo(communityBoard1.getId());
156+
assertThat(communityBoards.getLast().communityBoard().getTitle()).isEqualTo(communityBoard1.getTitle());
157+
assertThat(communityBoards.getLast().writerNickname()).isEqualTo(volunteer.getNickname());
158+
assertThat(communityBoards.getLast().communityBoard().getCreatedAt()).isEqualTo(communityBoard1.getCreatedAt());
159159
}
160160
}

src/test/java/com/somemore/community/service/CommunityBoardQueryServiceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class CommunityBoardQueryServiceTest extends IntegrationTestSupport {
4343
@Autowired
4444
CommunityBoardQueryService communityBoardQueryService;
4545

46-
private UUID writerId1, writerId2;
46+
private UUID writerId1;
4747
private Long communityId1, communityId2;
4848
private String imgUrl, nickName1, nickName2;
4949

@@ -70,7 +70,7 @@ void setUp() {
7070
volunteerRepository.save(volunteer2);
7171

7272
writerId1 = volunteer.getId();
73-
writerId2 = volunteer2.getId();
73+
UUID writerId2 = volunteer2.getId();
7474
nickName1 = volunteer.getNickname();
7575
nickName2 = volunteer2.getNickname();
7676
imgUrl = "https://image.test.url/123";

0 commit comments

Comments
 (0)