-
Notifications
You must be signed in to change notification settings - Fork 1
Bug/80 커뮤니티 댓글 필드 수정 #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
9b15a18
cc82066
0f63ec6
b7b0d9d
5af09e9
3b6552f
ff886b7
c339f4f
4e00263
c5ea384
511c89d
5350e83
aefe88e
58b731e
139c454
1e53d65
53576ef
1412311
7e4f4c4
6f73258
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -52,6 +52,18 @@ public List<CommunityBoardView> findByWriterId(UUID writerId) { | |||||||||||||||||||||||||||||||
| .fetch(); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||
| public boolean existsById(Long id) { | ||||||||||||||||||||||||||||||||
| QCommunityBoard communityBoard = QCommunityBoard.communityBoard; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return queryFactory | ||||||||||||||||||||||||||||||||
| .selectOne() | ||||||||||||||||||||||||||||||||
| .from(communityBoard) | ||||||||||||||||||||||||||||||||
| .where(communityBoard.id.eq(id) | ||||||||||||||||||||||||||||||||
| .and(communityBoard.deleted.eq(false))) | ||||||||||||||||||||||||||||||||
| .fetchFirst() != null; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. existsByIdAndDeletedFalse가 너무 긴 거 같아서 querydsl로 했었는데 이렇게 보니 jpa로 하는게 훨씬 간결할 거 같네요! 수정하겠습니다. 찾아보니 fetchCount() 메서드가 deprecated 되어서 fetchFirst() 또는 limit(1)을 사용하여 존재 여부를 확인하는 방식이 권장되고 있다고 하네요!
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 감사합니다. 조건이 1개가 아니라서 찝찝하긴 한데, id and deleted는 괜찮지 않을까 싶어서 제안드렸습니다. |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| private JPAQuery<CommunityBoardView> getCommunityBoardsQuery() { | ||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이건 이번 변경사항은 아닌데 repo에 find로 맞추기로 하지 않았는지 여쭤보고 싶습니다!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 안그래도 저도 다시 수정하면서 거슬렸는데
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 ! |
||||||||||||||||||||||||||||||||
| QCommunityBoard communityBoard = QCommunityBoard.communityBoard; | ||||||||||||||||||||||||||||||||
| QVolunteer volunteer = QVolunteer.volunteer; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import com.somemore.community.domain.CommunityComment; | ||
| import com.somemore.community.dto.request.CommunityCommentCreateRequestDto; | ||
| import com.somemore.community.repository.board.CommunityBoardRepository; | ||
| import com.somemore.community.repository.comment.CommunityCommentRepository; | ||
| import com.somemore.community.usecase.comment.CreateCommunityCommentUseCase; | ||
| import com.somemore.global.exception.BadRequestException; | ||
|
|
@@ -11,24 +12,34 @@ | |
|
|
||
| import java.util.UUID; | ||
|
|
||
| import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD; | ||
| import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_COMMENT; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Transactional | ||
| @Service | ||
| public class CreateCommunityCommentService implements CreateCommunityCommentUseCase { | ||
|
|
||
| private final CommunityBoardRepository communityBoardRepository; | ||
| private final CommunityCommentRepository communityCommentRepository; | ||
|
|
||
| @Override | ||
| public Long createCommunityComment(CommunityCommentCreateRequestDto requestDto, UUID writerId) { | ||
| CommunityComment communityComment = requestDto.toEntity(writerId); | ||
|
|
||
| validateCommunityBoardExists(communityComment.getCommunityBoardId()); | ||
|
|
||
| validateParentCommentExists(communityComment.getParentCommentId()); | ||
|
|
||
| return communityCommentRepository.save(communityComment).getId(); | ||
| } | ||
|
|
||
| private void validateCommunityBoardExists(Long communityBoardId) { | ||
| if (communityBoardRepository.doesNotExistById(communityBoardId)) { | ||
| throw new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage()); | ||
| } | ||
| } | ||
|
Comment on lines
+39
to
+43
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이전에는 early return 으로 해결하시지 않으셨는지, 변경하게 된 의도도 여쭤보고 싶습니다!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아 지난번에는 validator가 아니고 리턴 값이 있었는데
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 감사합니다. 이해했습니다. 저는 반환값이 없는 return을 적어두는 편이라서 여쭤보고 싶었습니다 ㅎㅎ |
||
|
|
||
| private void validateParentCommentExists(Long parentCommentId) { | ||
| if (parentCommentId != null && !communityCommentRepository.existsById(parentCommentId)) { | ||
| throw new BadRequestException(NOT_EXISTS_COMMUNITY_COMMENT.getMessage()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거 다시 보는데 조건 가독성이 아쉬운 것 같아요. 정확한 의도를 여쭤보고 싶습니다.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이게 더 가독성 별로 인가요.. 더 좋은 방법이 생각나시면 알려주세용
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하 존재를 검증한다는 의도로 느껴지는 메서드 네이밍이라서 헷갈렸던 것 같아요 |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| package com.somemore.community.repository; | ||
|
|
||
| import com.somemore.IntegrationTestSupport; | ||
| import com.somemore.community.domain.CommunityBoard; | ||
| import com.somemore.community.domain.CommunityComment; | ||
| import com.somemore.community.repository.board.CommunityBoardRepository; | ||
| import com.somemore.community.repository.comment.CommunityCommentRepository; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
|
|
@@ -17,6 +20,24 @@ | |
| class CommunityCommentRepositoryTest extends IntegrationTestSupport { | ||
| @Autowired | ||
| CommunityCommentRepository communityCommentRepository; | ||
| @Autowired | ||
| CommunityBoardRepository communityBoardRepository; | ||
|
|
||
| private Long boardId; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| CommunityBoard communityBoard = CommunityBoard.builder() | ||
| .title("테스트 커뮤니티 게시글 제목") | ||
| .content("테스트 커뮤니티 게시글 내용") | ||
| .imgUrl("http://community.example.com/123") | ||
| .writerId(UUID.randomUUID()) | ||
| .build(); | ||
|
|
||
| communityBoardRepository.save(communityBoard); | ||
|
|
||
| boardId = communityBoard.getId(); | ||
| } | ||
|
|
||
| @DisplayName("커뮤니티 게시글에 댓글을 생성할 수 있다. (Repository)") | ||
| @Test | ||
|
|
@@ -26,6 +47,7 @@ void createCommunityComment() { | |
| UUID writerId = UUID.randomUUID(); | ||
|
|
||
| CommunityComment communityComment = CommunityComment.builder() | ||
| .communityBoardId(boardId) | ||
| .writerId(writerId) | ||
| .content("커뮤니티 댓글 테스트 내용") | ||
| .parentCommentId(null) | ||
|
|
@@ -48,6 +70,7 @@ void createCommunityCommentReply() { | |
| UUID writerId = UUID.randomUUID(); | ||
|
|
||
| CommunityComment communityComment = CommunityComment.builder() | ||
| .communityBoardId(boardId) | ||
| .writerId(writerId) | ||
| .content("커뮤니티 댓글 테스트 내용") | ||
| .parentCommentId(1L) | ||
|
|
@@ -70,6 +93,7 @@ void findCommunityCommentById() { | |
| UUID writerId = UUID.randomUUID(); | ||
|
|
||
| CommunityComment communityComment = CommunityComment.builder() | ||
| .communityBoardId(boardId) | ||
| .writerId(writerId) | ||
| .content("커뮤니티 댓글 테스트 내용") | ||
| .parentCommentId(null) | ||
|
|
@@ -95,6 +119,7 @@ void existsById() { | |
| UUID writerId = UUID.randomUUID(); | ||
|
|
||
| CommunityComment communityComment = CommunityComment.builder() | ||
| .communityBoardId(boardId) | ||
| .writerId(writerId) | ||
| .content("커뮤니티 댓글 테스트 내용") | ||
| .parentCommentId(null) | ||
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,16 @@ | |
|
|
||
| import com.somemore.IntegrationTestSupport; | ||
| import com.somemore.community.domain.CommunityComment; | ||
| import com.somemore.community.dto.request.CommunityBoardCreateRequestDto; | ||
| import com.somemore.community.dto.request.CommunityCommentCreateRequestDto; | ||
| import com.somemore.community.repository.comment.CommunityCommentRepository; | ||
| import com.somemore.community.usecase.board.CreateCommunityBoardUseCase; | ||
| import com.somemore.community.usecase.board.DeleteCommunityBoardUseCase; | ||
| import com.somemore.global.exception.BadRequestException; | ||
| import com.somemore.global.exception.ExceptionMessage; | ||
| import org.assertj.core.api.ThrowableAssert; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
|
|
@@ -24,6 +28,25 @@ class CreateCommunityCommentServiceTest extends IntegrationTestSupport { | |
| private CreateCommunityCommentService createCommunityCommentService; | ||
| @Autowired | ||
| private CommunityCommentRepository communityCommentRepository; | ||
| @Autowired | ||
| private CreateCommunityBoardUseCase createCommunityBoardUseCase; | ||
| @Autowired | ||
| private DeleteCommunityBoardUseCase deleteCommunityBoardUseCase; | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CommunityRepository 주입받아서도 가능할 것 같은데 맞나요..?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 repository로 하는게 나을까요?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 테스트 하려는 서비스만 주입받고 나머지는 레포지토리가 좋아보입니다.!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 수정했습니다! |
||
| private UUID writerId; | ||
| private Long boardId; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| CommunityBoardCreateRequestDto boardDto = CommunityBoardCreateRequestDto.builder() | ||
| .title("커뮤니티 테스트 제목") | ||
| .content("커뮤니티 테스트 내용") | ||
| .build(); | ||
|
|
||
| writerId = UUID.randomUUID(); | ||
|
|
||
| boardId = createCommunityBoardUseCase.createCommunityBoard(boardDto, writerId, null); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
|
|
@@ -36,12 +59,11 @@ void createCommunityCommentWithDto() { | |
|
|
||
| //given | ||
| CommunityCommentCreateRequestDto dto = CommunityCommentCreateRequestDto.builder() | ||
| .communityBoardId(boardId) | ||
| .content("커뮤니티 댓글 테스트 내용") | ||
| .parentCommentId(null) | ||
| .build(); | ||
|
|
||
| UUID writerId = UUID.randomUUID(); | ||
|
|
||
| //when | ||
| Long commentId = createCommunityCommentService.createCommunityComment(dto, writerId); | ||
|
|
||
|
|
@@ -61,14 +83,15 @@ void createCommunityCommentRelyWithDto() { | |
|
|
||
| //given | ||
| CommunityCommentCreateRequestDto commentDto = CommunityCommentCreateRequestDto.builder() | ||
| .communityBoardId(boardId) | ||
| .content("커뮤니티 댓글 테스트 내용") | ||
| .parentCommentId(null) | ||
| .build(); | ||
|
|
||
| UUID writerId = UUID.randomUUID(); | ||
| Long commentId = createCommunityCommentService.createCommunityComment(commentDto, writerId); | ||
|
|
||
| CommunityCommentCreateRequestDto replyDto = CommunityCommentCreateRequestDto.builder() | ||
| .communityBoardId(boardId) | ||
| .content("커뮤니티 대댓글 테스트 내용") | ||
| .parentCommentId(commentId) | ||
| .build(); | ||
|
|
@@ -86,12 +109,13 @@ void createCommunityCommentRelyWithDto() { | |
| assertThat(communityCommentReply.get().getParentCommentId()).isEqualTo(commentId); | ||
| } | ||
|
|
||
| @DisplayName("삭제된 댓글에 대댓글을 등록할 때 예외를 던진다.") | ||
| @DisplayName("삭제된 댓글이나 존재하지 않는 댓글에 대댓글을 등록할 때 예외를 던진다.") | ||
| @Test | ||
| void createCommunityCommentReplyWithDeletedParentId() { | ||
|
|
||
| //given | ||
| CommunityCommentCreateRequestDto replyDto = CommunityCommentCreateRequestDto.builder() | ||
| .communityBoardId(boardId) | ||
| .content("커뮤니티 대댓글 테스트 내용") | ||
| .parentCommentId(2L) | ||
| .build(); | ||
|
|
@@ -104,4 +128,26 @@ void createCommunityCommentReplyWithDeletedParentId() { | |
| .isThrownBy(callable) | ||
| .withMessage(ExceptionMessage.NOT_EXISTS_COMMUNITY_COMMENT.getMessage()); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 static import 하면 좋을 것 같습니다.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 수정했습니다~ |
||
|
|
||
| @DisplayName("삭제된 게시글에 댓글을 등록할 때 예외를 던진다.") | ||
| @Test | ||
| void createCommunityCommentWithDeletedBoardId() { | ||
|
|
||
| //given | ||
| CommunityCommentCreateRequestDto commentDto = CommunityCommentCreateRequestDto.builder() | ||
| .communityBoardId(boardId) | ||
| .content("커뮤니티 댓글 테스트 내용") | ||
| .parentCommentId(null) | ||
| .build(); | ||
|
|
||
| deleteCommunityBoardUseCase.deleteCommunityBoard(writerId, boardId); | ||
|
|
||
| //when | ||
| ThrowableAssert.ThrowingCallable callable = () -> createCommunityCommentService.createCommunityComment(commentDto, UUID.randomUUID()); | ||
|
|
||
| //then | ||
| assertThatExceptionOfType(BadRequestException.class) | ||
| .isThrownBy(callable) | ||
| .withMessage(ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD.getMessage()); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. static import 하면 좋을 것 같아요.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵! 수정했습니다 |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 메서드가 필요할까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
서진님 코드에서 멋져 보여서 저도 따라해봤습니다 ㅎ
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 !를 쓰지 않으려고 만든다고 생각했습니다..!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵 참고하겠습니다!