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 @@ -38,4 +38,8 @@ public CommunityComment(UUID writerId, String content, Long parentCommentId) {
this.content = content;
this.parentCommentId = parentCommentId;
}

public boolean isWriter(UUID writerId) {
return this.writerId.equals(writerId);
}
}
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드가 깔끔하네요


CommunityComment communityComment = getCommunityCommentById(id);

validateWriter(communityComment, writerId);

communityComment.markAsDeleted();

communityCommentRepository.save(communityComment);
}

private CommunityComment getCommunityCommentById(Long id) {
return communityCommentRepository.findById(id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도 getCommunityCommentById() 인 것 같아여

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 이게 위에 있는 내용인 것 같네요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Expand Up @@ -12,6 +12,7 @@ public enum ExceptionMessage {
NOT_EXISTS_COMMUNITY_BOARD("존재하지 않는 게시글 입니다."),
UNAUTHORIZED_COMMUNITY_BOARD("해당 게시글에 권한이 없습니다."),
NOT_EXISTS_COMMUNITY_COMMENT("존재하지 않는 댓글 입니다."),
UNAUTHORIZED_COMMUNITY_COMMENT("해당 댓글에 권한이 없습니다."),
NOT_EXISTS_LOCATION("존재하지 않는 위치 ID 입니다."),
NOT_EXISTS_RECRUIT_BOARD("존재하지 않는 봉사 모집글 ID 입니다."),
UNAUTHORIZED_RECRUIT_BOARD("자신이 작성한 봉사 모집글이 아닙니다."),
Expand Down
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());
}
}