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
@@ -1,5 +1,6 @@
package com.somemore.community.domain;

import com.somemore.community.dto.request.CommunityCommentUpdateRequestDto;
import com.somemore.global.common.BaseEntity;
import jakarta.persistence.*;
import lombok.Builder;
Expand Down Expand Up @@ -46,4 +47,8 @@ public CommunityComment(Long communityBoardId, UUID writerId, String content, Lo
public boolean isWriter(UUID writerId) {
return this.writerId.equals(writerId);
}
}

public void updateWith(CommunityCommentUpdateRequestDto dto) {
this.content = dto.content();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.somemore.community.dto.request;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Builder;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@Builder
public record CommunityCommentUpdateRequestDto (
@Schema(description = "커뮤니티 댓글 내용", example = "저도 함께 하고 싶습니다.")
@NotBlank(message = "댓글 내용은 필수 값입니다.")
String content
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.somemore.community.service.comment;

import com.somemore.community.domain.CommunityComment;
import com.somemore.community.dto.request.CommunityCommentUpdateRequestDto;
import com.somemore.community.repository.board.CommunityBoardRepository;
import com.somemore.community.repository.comment.CommunityCommentRepository;
import com.somemore.community.usecase.comment.UpdateCommunityCommentUseCase;
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 UpdateCommunityCommentService implements UpdateCommunityCommentUseCase {

private final CommunityCommentRepository communityCommentRepository;
private final CommunityBoardRepository communityBoardRepository;

@Override
public void updateCommunityComment(CommunityCommentUpdateRequestDto requestDto, Long communityCommentId, UUID writerId) {

CommunityComment communityComment = getCommunityCommentById(communityCommentId);

validateCommunityBoardExists(communityComment.getCommunityBoardId());

validateWriter(communityComment, writerId);

communityComment.updateWith(requestDto);

communityCommentRepository.save(communityComment);
}

private CommunityComment getCommunityCommentById(Long id) {
return communityCommentRepository.findById(id)
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_COMMENT.getMessage()));
}

private void validateCommunityBoardExists(Long communityBoardId) {
if (communityBoardRepository.doesNotExistById(communityBoardId)) {
throw new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.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,12 @@
package com.somemore.community.usecase.comment;

import com.somemore.community.dto.request.CommunityCommentUpdateRequestDto;

import java.util.UUID;

public interface UpdateCommunityCommentUseCase {
void updateCommunityComment(
CommunityCommentUpdateRequestDto requestDto,
Long communityCommentId,
UUID writerId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.somemore.community.service.comment;

import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD;
import static com.somemore.global.exception.ExceptionMessage.UNAUTHORIZED_COMMUNITY_COMMENT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import com.somemore.IntegrationTestSupport;
import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.domain.CommunityComment;
import com.somemore.community.dto.request.CommunityBoardCreateRequestDto;
import com.somemore.community.dto.request.CommunityCommentCreateRequestDto;
import com.somemore.community.dto.request.CommunityCommentUpdateRequestDto;
import com.somemore.community.repository.board.CommunityBoardRepository;
import com.somemore.community.repository.comment.CommunityCommentRepository;
import com.somemore.global.exception.BadRequestException;
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.Optional;
import java.util.UUID;

class UpdateCommunityCommentServiceTest extends IntegrationTestSupport {

@Autowired
private UpdateCommunityCommentService updateCommunityCommentService;
@Autowired
private CommunityCommentRepository communityCommentRepository;
@Autowired
private CommunityBoardRepository communityBoardRepository;

private UUID writerId;
private Long commentId;
private CommunityCommentUpdateRequestDto updateRequestDto;

@BeforeEach
void setUp() {
CommunityBoardCreateRequestDto boardDto = CommunityBoardCreateRequestDto.builder()
.title("커뮤니티 테스트 제목")
.content("커뮤니티 테스트 내용")
.build();

writerId = UUID.randomUUID();

CommunityBoard communityBoard = communityBoardRepository.save(boardDto.toEntity(writerId, "https://test.image/123"));

CommunityCommentCreateRequestDto commentDto = CommunityCommentCreateRequestDto.builder()
.communityBoardId(communityBoard.getId())
.content("커뮤니티 댓글 테스트 내용")
.parentCommentId(null)
.build();

CommunityComment communityComment = communityCommentRepository.save(commentDto.toEntity(writerId));

commentId = communityComment.getId();

updateRequestDto = CommunityCommentUpdateRequestDto.builder()
.content("수정한 커뮤니티 댓글 내용")
.build();
}

@AfterEach
void tearDown() {
communityCommentRepository.deleteAllInBatch();
}

@DisplayName("댓글을 수정한다.")
@Test
void updateCommunityComment() {

//given
//when
updateCommunityCommentService.updateCommunityComment(updateRequestDto, commentId, writerId);

//then
Optional<CommunityComment> communityComment = communityCommentRepository.findById(commentId);
assertThat(communityComment).isPresent();
assertThat(communityComment.get().getId()).isEqualTo(commentId);
assertThat(communityComment.get().getContent()).isEqualTo("수정한 커뮤니티 댓글 내용");
}

@DisplayName("작성자가 아닌 id로 댓글을 수정하고자 할 때 예외를 던진다.")
@Test
void updateCommunityCommentWithNotWriterId() {

//given
//when
ThrowableAssert.ThrowingCallable callable = () -> updateCommunityCommentService.updateCommunityComment(updateRequestDto, commentId, UUID.randomUUID());

//then
assertThatExceptionOfType(BadRequestException.class)
.isThrownBy(callable)
.withMessage(UNAUTHORIZED_COMMUNITY_COMMENT.getMessage());
}

@DisplayName("삭제된 게시글의 댓글을 수정하고자 할 때 예외를 던진다.")
@Test
void updateCommunityCommentWithDeletedBoardId() {

//given
communityBoardRepository.deleteAllInBatch();
//when
ThrowableAssert.ThrowingCallable callable = () -> updateCommunityCommentService.updateCommunityComment(updateRequestDto, commentId, writerId);

//then
assertThatExceptionOfType(BadRequestException.class)
.isThrownBy(callable)
.withMessage(NOT_EXISTS_COMMUNITY_BOARD.getMessage());
}
}