Skip to content

Commit f15bedd

Browse files
ayoung-devleebs0521
authored andcommitted
feat: �community-comment 삭제 기능 (#73)
* test(community): CommunityComment 삭제 기능 테스트 작성 * test(community): CommunityComment 삭제 Usecase 추가 및 Service 구현 * test(community): CommunityComment 작성자 확인 메서드 및 권한 예외 메세지 추가 * refactor(community): 코드 리뷰 사항 반영 - getCommunityBoardById -> getCommunityCommentById
1 parent a089963 commit f15bedd

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ public CommunityComment(UUID writerId, String content, Long parentCommentId) {
3838
this.content = content;
3939
this.parentCommentId = parentCommentId;
4040
}
41+
42+
public boolean isWriter(UUID writerId) {
43+
return this.writerId.equals(writerId);
44+
}
4145
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.somemore.community.service.comment;
2+
3+
import com.somemore.community.domain.CommunityComment;
4+
import com.somemore.community.repository.comment.CommunityCommentRepository;
5+
import com.somemore.community.usecase.comment.DeleteCommunityCommentUseCase;
6+
import com.somemore.global.exception.BadRequestException;
7+
import lombok.RequiredArgsConstructor;
8+
import org.springframework.stereotype.Service;
9+
import org.springframework.transaction.annotation.Transactional;
10+
11+
import java.util.UUID;
12+
13+
import static com.somemore.global.exception.ExceptionMessage.*;
14+
15+
@RequiredArgsConstructor
16+
@Transactional
17+
@Service
18+
public class DeleteCommunityCommentService implements DeleteCommunityCommentUseCase {
19+
20+
private final CommunityCommentRepository communityCommentRepository;
21+
22+
@Override
23+
public void deleteCommunityComment(UUID writerId, Long id) {
24+
25+
CommunityComment communityComment = getCommunityCommentById(id);
26+
27+
validateWriter(communityComment, writerId);
28+
29+
communityComment.markAsDeleted();
30+
31+
communityCommentRepository.save(communityComment);
32+
}
33+
34+
private CommunityComment getCommunityCommentById(Long id) {
35+
return communityCommentRepository.findById(id)
36+
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_COMMENT.getMessage()));
37+
}
38+
39+
private void validateWriter(CommunityComment communityComment, UUID writerId) {
40+
if (communityComment.isWriter(writerId)) {
41+
return;
42+
}
43+
44+
throw new BadRequestException(UNAUTHORIZED_COMMUNITY_COMMENT.getMessage());
45+
}
46+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.somemore.community.usecase.comment;
2+
3+
import java.util.UUID;
4+
5+
public interface DeleteCommunityCommentUseCase {
6+
void deleteCommunityComment(UUID writerId, Long id);
7+
}

src/main/java/com/somemore/global/exception/ExceptionMessage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public enum ExceptionMessage {
1212
NOT_EXISTS_COMMUNITY_BOARD("존재하지 않는 게시글 입니다."),
1313
UNAUTHORIZED_COMMUNITY_BOARD("해당 게시글에 권한이 없습니다."),
1414
NOT_EXISTS_COMMUNITY_COMMENT("존재하지 않는 댓글 입니다."),
15+
UNAUTHORIZED_COMMUNITY_COMMENT("해당 댓글에 권한이 없습니다."),
1516
NOT_EXISTS_LOCATION("존재하지 않는 위치 ID 입니다."),
1617
NOT_EXISTS_RECRUIT_BOARD("존재하지 않는 봉사 모집글 ID 입니다."),
1718
UNAUTHORIZED_RECRUIT_BOARD("해당 봉사 모집글에 권한이 없습니다."),
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.somemore.community.service.comment;
2+
3+
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.community.dto.request.CommunityCommentCreateRequestDto;
5+
import com.somemore.community.repository.comment.CommunityCommentRepository;
6+
import com.somemore.community.usecase.comment.CreateCommunityCommentUseCase;
7+
import com.somemore.global.exception.BadRequestException;
8+
import com.somemore.global.exception.ExceptionMessage;
9+
import org.assertj.core.api.ThrowableAssert;
10+
import org.junit.jupiter.api.AfterEach;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.DisplayName;
13+
import org.junit.jupiter.api.Test;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
16+
import java.util.UUID;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20+
21+
class DeleteCommunityCommentServiceTest extends IntegrationTestSupport {
22+
23+
@Autowired
24+
private DeleteCommunityCommentService deleteCommunityCommentService;
25+
@Autowired
26+
private CreateCommunityCommentUseCase createCommunityCommentUseCase;
27+
@Autowired
28+
private CommunityCommentRepository communityCommentRepository;
29+
30+
private UUID writerId;
31+
private Long commentId;
32+
33+
@BeforeEach
34+
void setUp() {
35+
CommunityCommentCreateRequestDto dto = CommunityCommentCreateRequestDto.builder()
36+
.content("커뮤니티 댓글 테스트 내용")
37+
.parentCommentId(null)
38+
.build();
39+
40+
writerId = UUID.randomUUID();
41+
42+
commentId = createCommunityCommentUseCase.createCommunityComment(dto, writerId);
43+
}
44+
45+
@AfterEach
46+
void tearDown() {
47+
communityCommentRepository.deleteAllInBatch();
48+
}
49+
50+
@DisplayName("댓글 id로 댓글을 삭제한다.")
51+
@Test
52+
void deleteCommunityCommentWithId() {
53+
//given
54+
//when
55+
deleteCommunityCommentService.deleteCommunityComment(writerId, commentId);
56+
57+
//then
58+
assertThat(communityCommentRepository.existsById(commentId)).isFalse();
59+
}
60+
61+
@DisplayName("삭제된 댓글의 id로 댓글을 삭제할 때 예외를 던진다.")
62+
@Test
63+
void deleteCommunityCommentWithDeletedId() {
64+
//given
65+
deleteCommunityCommentService.deleteCommunityComment(writerId, commentId);
66+
67+
//when
68+
ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityCommentService.deleteCommunityComment(writerId, commentId);
69+
70+
//then
71+
assertThatExceptionOfType(BadRequestException.class)
72+
.isThrownBy(callable)
73+
.withMessage(ExceptionMessage.NOT_EXISTS_COMMUNITY_COMMENT.getMessage());
74+
}
75+
76+
@DisplayName("작성자가 아닌 id로 댓글을 삭제하고자 할 때 예외를 던진다.")
77+
@Test
78+
void deleteCommunityCommentWithNotWriterId() {
79+
//given
80+
//when
81+
ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityCommentService.deleteCommunityComment(UUID.randomUUID(), commentId);
82+
83+
//then
84+
assertThatExceptionOfType(BadRequestException.class)
85+
.isThrownBy(callable)
86+
.withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_COMMENT.getMessage());
87+
}
88+
}

0 commit comments

Comments
 (0)