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 @@ -10,6 +10,6 @@ public interface CommunityBoardRepository {
CommunityBoard save(CommunityBoard communityBoard);
Optional<CommunityBoard> findById(Long id);
List<CommunityBoard> getCommunityBoards();
List<CommunityBoard> getCommunityBoardsByWriterId(UUID writerId);
List<CommunityBoard> findByWriterId(UUID writerId);
void deleteAllInBatch();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ public CommunityBoard save(CommunityBoard communityBoard) {

@Override
public Optional<CommunityBoard> findById(Long id) {
return communityBoardJpaRepository.findById(id);
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;

return Optional.ofNullable(queryFactory
.selectFrom(communityBoard)
.where(communityBoard.id.eq(id)
.and(communityBoard.deleted.eq(false)))
.fetchOne());
}

@Override
Expand All @@ -39,7 +45,7 @@ public List<CommunityBoard> getCommunityBoards() {
}

@Override
public List<CommunityBoard> getCommunityBoardsByWriterId(UUID writerId) {
public List<CommunityBoard> findByWriterId(UUID writerId) {
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;

return queryFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public List<CommunityBoardGetResponseDto> getCommunityBoards() {

@Override
public List<CommunityBoardGetResponseDto> getCommunityBoardsByWriterId(UUID writerId) {
List<CommunityBoard> boards = communityBoardRepository.getCommunityBoardsByWriterId(writerId);
List<CommunityBoard> boards = communityBoardRepository.findByWriterId(writerId);
String writerNickname = getWriterNickname(writerId);

return boards.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class CommunityRepositoryTest extends IntegrationTestSupport {
@Autowired
private CommunityBoardRepository communityBoardRepository;

@DisplayName("커뮤니티 id로 커뮤니티 상세 정보를 조회할 수 있다. (Repository)")
@DisplayName("커뮤니티 게시글 id로 커뮤니티 상세 정보를 조회할 수 있다. (Repository)")
@Test
void findById() {
void getCommunityBoardById() {
//given
CommunityBoard communityBoard = CommunityBoard.builder()
.title("테스트 커뮤니티 게시글 제목")
Expand All @@ -43,6 +43,27 @@ void findById() {
assertThat(foundCommunityBoard.get().getWriterId()).isEqualTo(communityBoard.getWriterId());
}

@DisplayName("삭제된 커뮤니티 id로 커뮤니티 상세 정보를 조회할 때 예외를 반환할 수 있다. (Repository)")
@Test
void getCommunityBoardByDeletedId() {
//given
CommunityBoard communityBoard = CommunityBoard.builder()
.title("테스트 커뮤니티 게시글 제목")
.content("테스트 커뮤니티 게시글 내용")
.imgUrl("http://community.example.com/123")
.writerId(UUID.randomUUID())
.build();

communityBoardRepository.save(communityBoard);
communityBoardRepository.deleteAllInBatch();

//when
Optional<CommunityBoard> foundCommunityBoard = communityBoardRepository.findById(communityBoard.getId());

//then
assertThat(foundCommunityBoard).isEmpty();
}

@DisplayName("저장된 전체 커뮤니티 게시글을 목록으로 조회할 수 있다. (Repository)")
@Test
void getCommunityBoards() {
Expand Down Expand Up @@ -103,7 +124,7 @@ void getCommunityBoardsByWriterId() {
communityBoardRepository.save(communityBoard3);

//when
List<CommunityBoard> communityBoards = communityBoardRepository.getCommunityBoardsByWriterId(communityBoard1.getWriterId());
List<CommunityBoard> communityBoards = communityBoardRepository.findByWriterId(communityBoard1.getWriterId());

//then
assertThat(communityBoards).hasSize(2);
Expand Down