Skip to content

Commit be132e1

Browse files
ayoung-dev7zrvleebs0521
authored
Bug: community-comment 필드 수정 (#83)
* fix(community): CommunityComment 엔티티 및 Dto에 communityBoardId 추가 * feat(community): CommunityBoardRepository에 existsById 추가 및 검증 테스트 추가 * feat(community): CommunityComment 생성 시 게시글 검증 로직 추가 * test(community): CommunityComment 생성 시 게시글 검증 테스트 추가 * test(community): CommunityCommentRepositoryTest 수정 * test(community): 불필요한 import 및 변수 삭제 * Feature/75 관심기관 등록, 취소 기능 구현 (#85) * feat: 관심기관 등록 기능 구현 - InterestCenter 엔티티에 누락된 기관, 봉사자 id 필드 추가 - 관심 기관 등록 유스케이스 추가및 서비스 레이어 구현 - 등록, 검증을 위한 영속성 레이어 구현 - 중복 예외 클래스 추가 - 예외 메세지 추가 - 테스트 코드 작성및 검증 완 * feat: 관심기관 취소 기능 구현 - 관심 기관 취소 유스케이스 추가및 서비스 레이어 구현 - 관심기관 취소, 검증을 위한 영속성 레이어 구현 - 예외 메세지 추가 - 테스트 코드 작성및 검증 완료 * feat: 관심기관 API 컨트롤러 구현 - 관심 기관 등록과 취소 엔드포인트 컨트롤러 구현 - ApiResponse 공통 응답 객체 메서드 추가(논의 예정) - 테스트 코드 작성및 검증완료 * feat: 요청 유효성 검증추가 - 관심기관 등록 요청 Dto에 유효성 검증 어노테이션 추가 - 활성화를 위해 컨트롤러에 @Valid 어노테이션 추가 * refactor: 공통 응답 객체 수정및 적용 - 기존 와일드카드를 사용하던 ApiResponse의 ok 메서드를 String 타입으로 리팩토링 - 변경에 따른 컨트롤러 return문 수정 * chore: 요청 Dto swagger scheme 수정 - 봉사자 -> 기관으로 수정 * chore: 컨트롤러 엔드 포인트 수정 - 기존 RequestMapping을 이용한 전역적인 엔드포인트 적용에서 엔드포인트마다 url을 붙여주는 방식으로 수정 * cicd: workflow 환경변수 추가 (#90) - 기본 이미지 주소를 저장한 환경변수 값추가 * cicd: workflow 환경변수 추가 (#92) - swagger에서의 테스트를 위한 환경변수 추가 * feat: 스웨거 API 테스트를 위한 봉사자 / 기관 토큰 발급 (#91) * feat(jwt): JwtAuthFilter 필터 로직 수정 - Authorization 없다면 익명 유저 - Authorization 있다면 Bearer 인증 작업 * feat(swagger): 토큰 인증 추가 * feat(center): 이름 기반 조회 기능 추가 * feat(security): 디버깅을 위한 기관 봉사 토큰 발급 기능 * chore: 디버깅 env 추가 * chore: 불필요한 import 제거 * hotfix: fix typo (#94) * fix(community): CommunityComment 엔티티 및 Dto에 communityBoardId 추가 * feat(community): CommunityBoardRepository에 existsById 추가 및 검증 테스트 추가 * feat(community): CommunityComment 생성 시 게시글 검증 로직 추가 * test(community): CommunityComment 생성 시 게시글 검증 테스트 추가 * test(community): CommunityCommentRepositoryTest 수정 * test(community): 불필요한 import 및 변수 삭제 * refactor(community): 코드 리뷰 사항 반영 - existsById 메서드 QeuryDSL -> JPA - repositoryTest 빌더 beforeEach로 추출 - serviceTest 예외 메세지 static import - serviceTest usecase 주입 > repository로 주입 * test(community): 변수명 변경 --------- Co-authored-by: seojin Yoon <[email protected]> Co-authored-by: Beom-Su Lee <[email protected]>
1 parent 511c89d commit be132e1

File tree

11 files changed

+177
-53
lines changed

11 files changed

+177
-53
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public class CommunityComment extends BaseEntity {
2222
@Column(name = "id", nullable = false)
2323
private Long id;
2424

25+
@Column(name = "community_board_id", nullable = false)
26+
private Long communityBoardId;
27+
2528
@Column(name = "writer_id", nullable = false, length = 16)
2629
private UUID writerId;
2730

@@ -33,7 +36,8 @@ public class CommunityComment extends BaseEntity {
3336
private Long parentCommentId;
3437

3538
@Builder
36-
public CommunityComment(UUID writerId, String content, Long parentCommentId) {
39+
public CommunityComment(Long communityBoardId, UUID writerId, String content, Long parentCommentId) {
40+
this.communityBoardId = communityBoardId;
3741
this.writerId = writerId;
3842
this.content = content;
3943
this.parentCommentId = parentCommentId;

src/main/java/com/somemore/community/dto/request/CommunityCommentCreateRequestDto.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,27 @@
66
import io.swagger.v3.oas.annotations.media.Schema;
77
import jakarta.annotation.Nullable;
88
import jakarta.validation.constraints.NotBlank;
9+
import jakarta.validation.constraints.NotNull;
910
import lombok.Builder;
1011

1112
import java.util.UUID;
1213

1314
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
1415
@Builder
1516
public record CommunityCommentCreateRequestDto(
17+
@Schema(description = "커뮤니티 게시글 ID", example = "33")
18+
@NotNull(message = "게시글 ID는 필수 값입니다.")
19+
Long communityBoardId,
1620
@Schema(description = "커뮤니티 댓글 내용", example = "저도 함께 하고 싶습니다.")
1721
@NotBlank(message = "댓글 내용은 필수 값입니다.")
1822
String content,
19-
@Schema(description = "부모 댓글의 ID", example = "1234", nullable = true)
23+
@Schema(description = "부모 댓글 ID", example = "1234", nullable = true)
2024
@Nullable
2125
Long parentCommentId
2226
) {
2327
public CommunityComment toEntity(UUID writerId) {
2428
return CommunityComment.builder()
29+
.communityBoardId(communityBoardId)
2530
.writerId(writerId)
2631
.content(content)
2732
.parentCommentId(parentCommentId)

src/main/java/com/somemore/community/repository/board/CommunityBoardJpaRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
import org.springframework.data.jpa.repository.JpaRepository;
55

66
public interface CommunityBoardJpaRepository extends JpaRepository<CommunityBoard, Long> {
7+
boolean existsByIdAndDeletedFalse(Long id);
78
}

src/main/java/com/somemore/community/repository/board/CommunityBoardRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ public interface CommunityBoardRepository {
1212
Optional<CommunityBoard> findById(Long id);
1313
List<CommunityBoardView> getCommunityBoards();
1414
List<CommunityBoardView> findByWriterId(UUID writerId);
15+
boolean existsById(Long id);
16+
default boolean doesNotExistById(Long id) {
17+
return !existsById(id);
18+
}
1519
void deleteAllInBatch();
1620
}

src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public List<CommunityBoardView> findByWriterId(UUID writerId) {
5252
.fetch();
5353
}
5454

55+
@Override
56+
public boolean existsById(Long id) {
57+
return communityBoardJpaRepository.existsByIdAndDeletedFalse(id);
58+
}
59+
5560
private JPAQuery<CommunityBoardView> getCommunityBoardsQuery() {
5661
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
5762
QVolunteer volunteer = QVolunteer.volunteer;

src/main/java/com/somemore/community/repository/comment/CommunityCommentRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ public interface CommunityCommentRepository {
88
CommunityComment save(CommunityComment communityComment);
99
Optional<CommunityComment> findById(Long id);
1010
boolean existsById(Long id);
11+
default boolean doesNotExistById(Long id) {
12+
return !existsById(id);
13+
}
1114
void deleteAllInBatch();
1215
}

src/main/java/com/somemore/community/service/comment/CreateCommunityCommentService.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.somemore.community.domain.CommunityComment;
44
import com.somemore.community.dto.request.CommunityCommentCreateRequestDto;
5+
import com.somemore.community.repository.board.CommunityBoardRepository;
56
import com.somemore.community.repository.comment.CommunityCommentRepository;
67
import com.somemore.community.usecase.comment.CreateCommunityCommentUseCase;
78
import com.somemore.global.exception.BadRequestException;
@@ -11,26 +12,38 @@
1112

1213
import java.util.UUID;
1314

15+
import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD;
1416
import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_COMMENT;
1517

1618
@RequiredArgsConstructor
1719
@Transactional
1820
@Service
1921
public class CreateCommunityCommentService implements CreateCommunityCommentUseCase {
2022

23+
private final CommunityBoardRepository communityBoardRepository;
2124
private final CommunityCommentRepository communityCommentRepository;
2225

2326
@Override
2427
public Long createCommunityComment(CommunityCommentCreateRequestDto requestDto, UUID writerId) {
2528
CommunityComment communityComment = requestDto.toEntity(writerId);
2629

27-
validateParentCommentExists(communityComment.getParentCommentId());
30+
validateCommunityBoardExists(communityComment.getCommunityBoardId());
31+
32+
if (requestDto.parentCommentId() != null) {
33+
validateParentCommentExists(communityComment.getParentCommentId());
34+
}
2835

2936
return communityCommentRepository.save(communityComment).getId();
3037
}
3138

39+
private void validateCommunityBoardExists(Long communityBoardId) {
40+
if (communityBoardRepository.doesNotExistById(communityBoardId)) {
41+
throw new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage());
42+
}
43+
}
44+
3245
private void validateParentCommentExists(Long parentCommentId) {
33-
if (parentCommentId != null && !communityCommentRepository.existsById(parentCommentId)) {
46+
if (communityCommentRepository.doesNotExistById(parentCommentId)) {
3447
throw new BadRequestException(NOT_EXISTS_COMMUNITY_COMMENT.getMessage());
3548
}
3649
}

src/test/java/com/somemore/community/repository/CommunityBoardRepositoryTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,27 @@ void getCommunityBoardsByWriterId() {
158158
assertThat(communityBoards.getLast().writerNickname()).isEqualTo(volunteer.getNickname());
159159
assertThat(communityBoards.getLast().communityBoard().getCreatedAt()).isEqualTo(communityBoard1.getCreatedAt());
160160
}
161+
162+
@DisplayName("게시글 id로 게시글이 존재하는지 확인할 수 있다.")
163+
@Test
164+
void existsById() {
165+
166+
//given
167+
UUID writerId = UUID.randomUUID();
168+
169+
CommunityBoard communityBoard = CommunityBoard.builder()
170+
.title("테스트 커뮤니티 게시글 제목")
171+
.content("테스트 커뮤니티 게시글 내용")
172+
.imgUrl("http://community.example.com/123")
173+
.writerId(writerId)
174+
.build();
175+
176+
CommunityBoard savedComment = communityBoardRepository.save(communityBoard);
177+
178+
//when
179+
boolean isExist = communityBoardRepository.existsById(savedComment.getId());
180+
181+
//then
182+
assertThat(isExist).isTrue();
183+
}
161184
}

src/test/java/com/somemore/community/repository/CommunityCommentRepositoryTest.java

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.somemore.community.repository;
22

33
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.community.domain.CommunityBoard;
45
import com.somemore.community.domain.CommunityComment;
6+
import com.somemore.community.repository.board.CommunityBoardRepository;
57
import com.somemore.community.repository.comment.CommunityCommentRepository;
8+
import org.junit.jupiter.api.BeforeEach;
69
import org.junit.jupiter.api.DisplayName;
710
import org.junit.jupiter.api.Test;
811
import org.springframework.beans.factory.annotation.Autowired;
@@ -15,25 +18,47 @@
1518

1619
@Transactional
1720
class CommunityCommentRepositoryTest extends IntegrationTestSupport {
21+
1822
@Autowired
1923
CommunityCommentRepository communityCommentRepository;
24+
@Autowired
25+
CommunityBoardRepository communityBoardRepository;
26+
27+
private Long boardId;
28+
private UUID writerId;
29+
private CommunityComment savedComment;
30+
31+
@BeforeEach
32+
void setUp() {
33+
CommunityBoard communityBoard = CommunityBoard.builder()
34+
.title("테스트 커뮤니티 게시글 제목")
35+
.content("테스트 커뮤니티 게시글 내용")
36+
.imgUrl("http://community.example.com/123")
37+
.writerId(UUID.randomUUID())
38+
.build();
2039

21-
@DisplayName("커뮤니티 게시글에 댓글을 생성할 수 있다. (Repository)")
22-
@Test
23-
void createCommunityComment() {
40+
communityBoardRepository.save(communityBoard);
2441

25-
//given
26-
UUID writerId = UUID.randomUUID();
42+
boardId = communityBoard.getId();
43+
44+
writerId = UUID.randomUUID();
2745

2846
CommunityComment communityComment = CommunityComment.builder()
47+
.communityBoardId(boardId)
2948
.writerId(writerId)
3049
.content("커뮤니티 댓글 테스트 내용")
3150
.parentCommentId(null)
3251
.build();
3352

34-
//when
35-
CommunityComment savedComment = communityCommentRepository.save(communityComment);
53+
savedComment = communityCommentRepository.save(communityComment);
54+
}
55+
56+
@DisplayName("커뮤니티 게시글에 댓글을 생성할 수 있다. (Repository)")
57+
@Test
58+
void createCommunityComment() {
3659

60+
//given
61+
//when
3762
//then
3863
assertThat(savedComment.getWriterId()).isEqualTo(writerId);
3964
assertThat(savedComment.getContent()).isEqualTo("커뮤니티 댓글 테스트 내용");
@@ -45,38 +70,27 @@ void createCommunityComment() {
4570
void createCommunityCommentReply() {
4671

4772
//given
48-
UUID writerId = UUID.randomUUID();
49-
50-
CommunityComment communityComment = CommunityComment.builder()
73+
CommunityComment communityCommentReply = CommunityComment.builder()
74+
.communityBoardId(boardId)
5175
.writerId(writerId)
5276
.content("커뮤니티 댓글 테스트 내용")
5377
.parentCommentId(1L)
5478
.build();
5579

5680
//when
57-
CommunityComment savedComment = communityCommentRepository.save(communityComment);
81+
CommunityComment savedCommentReply = communityCommentRepository.save(communityCommentReply);
5882

5983
//then
60-
assertThat(savedComment.getWriterId()).isEqualTo(writerId);
61-
assertThat(savedComment.getContent()).isEqualTo("커뮤니티 댓글 테스트 내용");
62-
assertThat(savedComment.getParentCommentId()).isEqualTo(1L);
84+
assertThat(savedCommentReply.getWriterId()).isEqualTo(writerId);
85+
assertThat(savedCommentReply.getContent()).isEqualTo("커뮤니티 댓글 테스트 내용");
86+
assertThat(savedCommentReply.getParentCommentId()).isEqualTo(1L);
6387
}
6488

6589
@DisplayName("댓글을 id로 조회할 수 있다. (Repository)")
6690
@Test
6791
void findCommunityCommentById() {
6892

6993
//given
70-
UUID writerId = UUID.randomUUID();
71-
72-
CommunityComment communityComment = CommunityComment.builder()
73-
.writerId(writerId)
74-
.content("커뮤니티 댓글 테스트 내용")
75-
.parentCommentId(null)
76-
.build();
77-
78-
CommunityComment savedComment = communityCommentRepository.save(communityComment);
79-
8094
//when
8195
Optional<CommunityComment> comment = communityCommentRepository.findById(savedComment.getId());
8296

@@ -92,16 +106,6 @@ void findCommunityCommentById() {
92106
void existsById() {
93107

94108
//given
95-
UUID writerId = UUID.randomUUID();
96-
97-
CommunityComment communityComment = CommunityComment.builder()
98-
.writerId(writerId)
99-
.content("커뮤니티 댓글 테스트 내용")
100-
.parentCommentId(null)
101-
.build();
102-
103-
CommunityComment savedComment = communityCommentRepository.save(communityComment);
104-
105109
//when
106110
boolean isExist = communityCommentRepository.existsById(savedComment.getId());
107111

0 commit comments

Comments
 (0)