-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/68 커뮤니티 댓글 삭제 기능 #73
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 all commits
370911c
9f3333b
20bc66f
818b92a
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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.somemore.community.service.comment; | ||
|
|
||
| import com.somemore.community.domain.CommunityComment; | ||
| import com.somemore.community.repository.comment.CommunityCommentRepository; | ||
| import com.somemore.community.usecase.comment.DeleteCommunityCommentUseCase; | ||
| import com.somemore.global.exception.BadRequestException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| import static com.somemore.global.exception.ExceptionMessage.*; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Transactional | ||
| @Service | ||
| public class DeleteCommunityCommentService implements DeleteCommunityCommentUseCase { | ||
|
|
||
| private final CommunityCommentRepository communityCommentRepository; | ||
|
|
||
| @Override | ||
| public void deleteCommunityComment(UUID writerId, Long id) { | ||
|
|
||
| CommunityComment communityComment = getCommunityCommentById(id); | ||
|
|
||
| validateWriter(communityComment, writerId); | ||
|
|
||
| communityComment.markAsDeleted(); | ||
|
|
||
| communityCommentRepository.save(communityComment); | ||
| } | ||
|
|
||
| private CommunityComment getCommunityCommentById(Long id) { | ||
| return communityCommentRepository.findById(id) | ||
|
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. 이것도 getCommunityCommentById() 인 것 같아여
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. 넵 제가 실수했어요.. |
||
| .orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_COMMENT.getMessage())); | ||
| } | ||
|
|
||
| private void validateWriter(CommunityComment communityComment, UUID writerId) { | ||
| if (communityComment.isWriter(writerId)) { | ||
| return; | ||
| } | ||
|
|
||
| throw new BadRequestException(UNAUTHORIZED_COMMUNITY_COMMENT.getMessage()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.somemore.community.usecase.comment; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public interface DeleteCommunityCommentUseCase { | ||
| void deleteCommunityComment(UUID writerId, Long id); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package com.somemore.community.service.comment; | ||
|
|
||
| import com.somemore.IntegrationTestSupport; | ||
| import com.somemore.community.dto.request.CommunityCommentCreateRequestDto; | ||
| import com.somemore.community.repository.comment.CommunityCommentRepository; | ||
| import com.somemore.community.usecase.comment.CreateCommunityCommentUseCase; | ||
| 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; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
|
||
| class DeleteCommunityCommentServiceTest extends IntegrationTestSupport { | ||
|
|
||
| @Autowired | ||
| private DeleteCommunityCommentService deleteCommunityCommentService; | ||
| @Autowired | ||
| private CreateCommunityCommentUseCase createCommunityCommentUseCase; | ||
| @Autowired | ||
| private CommunityCommentRepository communityCommentRepository; | ||
|
|
||
| private UUID writerId; | ||
| private Long commentId; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| CommunityCommentCreateRequestDto dto = CommunityCommentCreateRequestDto.builder() | ||
| .content("커뮤니티 댓글 테스트 내용") | ||
| .parentCommentId(null) | ||
| .build(); | ||
|
|
||
| writerId = UUID.randomUUID(); | ||
|
|
||
| commentId = createCommunityCommentUseCase.createCommunityComment(dto, writerId); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| communityCommentRepository.deleteAllInBatch(); | ||
| } | ||
|
|
||
| @DisplayName("댓글 id로 댓글을 삭제한다.") | ||
| @Test | ||
| void deleteCommunityCommentWithId() { | ||
| //given | ||
| //when | ||
| deleteCommunityCommentService.deleteCommunityComment(writerId, commentId); | ||
|
|
||
| //then | ||
| assertThat(communityCommentRepository.existsById(commentId)).isFalse(); | ||
| } | ||
|
|
||
| @DisplayName("삭제된 댓글의 id로 댓글을 삭제할 때 예외를 던진다.") | ||
| @Test | ||
| void deleteCommunityCommentWithDeletedId() { | ||
| //given | ||
| deleteCommunityCommentService.deleteCommunityComment(writerId, commentId); | ||
|
|
||
| //when | ||
| ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityCommentService.deleteCommunityComment(writerId, commentId); | ||
|
|
||
| //then | ||
| assertThatExceptionOfType(BadRequestException.class) | ||
| .isThrownBy(callable) | ||
| .withMessage(ExceptionMessage.NOT_EXISTS_COMMUNITY_COMMENT.getMessage()); | ||
| } | ||
|
|
||
| @DisplayName("작성자가 아닌 id로 댓글을 삭제하고자 할 때 예외를 던진다.") | ||
| @Test | ||
| void deleteCommunityCommentWithNotWriterId() { | ||
| //given | ||
| //when | ||
| ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityCommentService.deleteCommunityComment(UUID.randomUUID(), commentId); | ||
|
|
||
| //then | ||
| assertThatExceptionOfType(BadRequestException.class) | ||
| .isThrownBy(callable) | ||
| .withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_COMMENT.getMessage()); | ||
| } | ||
| } |
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.
코드가 깔끔하네요