Skip to content
Closed
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 @@ -8,7 +8,7 @@

public interface CommunityBoardRepository {
CommunityBoard save(CommunityBoard communityBoard);
Optional<CommunityBoard> findById(Long id);
Optional<CommunityBoard> getCommunityBoardWithId(Long id);
List<CommunityBoard> getCommunityBoards();
List<CommunityBoard> getCommunityBoardsByWriterId(UUID writerId);
void deleteAllInBatch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ public CommunityBoard save(CommunityBoard communityBoard) {
}

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public List<CommunityBoardGetResponseDto> getCommunityBoardsByWriterId(UUID writ

@Override
public CommunityBoardGetDetailResponseDto getCommunityBoardDetail(Long id) {
CommunityBoard board = communityBoardRepository.findById(id)
CommunityBoard board = communityBoardRepository.getCommunityBoardWithId(id)
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage()));

return CommunityBoardGetDetailResponseDto.fromEntity(board, getWriterDetail(board.getWriterId()));
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 @@ -33,7 +33,7 @@ void findById() {
communityBoardRepository.save(communityBoard);

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

//then
assertThat(foundCommunityBoard).isNotNull();
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.getCommunityBoardWithId(communityBoard.getId());

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

@DisplayName("저장된 전체 커뮤니티 게시글을 목록으로 조회할 수 있다. (Repository)")
@Test
void getCommunityBoards() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void createCommunityWithDto() {
Long communityId = createCommunityBoardService.createCommunityBoard(dto, writerId, imgUrl);

//then
Optional<CommunityBoard> communityBoard = communityBoardRepository.findById(communityId);
Optional<CommunityBoard> communityBoard = communityBoardRepository.getCommunityBoardWithId(communityId);

assertThat(communityBoard).isPresent();
assertThat(communityBoard.get().getId()).isEqualTo(communityId);
Expand All @@ -67,7 +67,7 @@ void createCommunityWithoutImgUrl() {
Long communityId = createCommunityBoardService.createCommunityBoard(dto, writerId, imgUrl);

//then
Optional<CommunityBoard> communityBoard = communityBoardRepository.findById(communityId);
Optional<CommunityBoard> communityBoard = communityBoardRepository.getCommunityBoardWithId(communityId);

assertThat(communityBoard).isPresent();
assertThat(communityBoard.get().getId()).isEqualTo(communityId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ void getAllCommunityBoards() {
List<CommunityBoardGetResponseDto> dtos = communityBoardQueryService.getCommunityBoards();

// then
Optional<CommunityBoard> communityBoard1 = communityBoardRepository.findById(communityId1);
Optional<CommunityBoard> communityBoard2 = communityBoardRepository.findById(communityId2);
Optional<CommunityBoard> communityBoard1 = communityBoardRepository.getCommunityBoardWithId(communityId1);
Optional<CommunityBoard> communityBoard2 = communityBoardRepository.getCommunityBoardWithId(communityId2);


assertThat(dtos)
Expand Down Expand Up @@ -135,8 +135,8 @@ void getCommunityBoardsByWriter() {
List<CommunityBoardGetResponseDto> dtos = communityBoardQueryService.getCommunityBoardsByWriterId(volunteer.getId());

//then
Optional<CommunityBoard> communityBoard1 = communityBoardRepository.findById(communityId1);
Optional<CommunityBoard> communityBoard2 = communityBoardRepository.findById(communityId2);
Optional<CommunityBoard> communityBoard1 = communityBoardRepository.getCommunityBoardWithId(communityId1);
Optional<CommunityBoard> communityBoard2 = communityBoardRepository.getCommunityBoardWithId(communityId2);

assertThat(dtos)
.isNotNull()
Expand Down