From 5b5599aaa1cd9b211e24c588a7aafd70a910e702 Mon Sep 17 00:00:00 2001 From: Jeongdonghun Date: Tue, 7 Jan 2025 14:48:23 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat=20:=20=EB=8C=93=EA=B8=80=20=ED=83=80?= =?UTF-8?q?=EC=9E=85=20=EB=B6=84=EB=A6=AC=20#48?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/itstime/reflog/comment/enums/CommentType.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/java/itstime/reflog/comment/enums/CommentType.java diff --git a/src/main/java/itstime/reflog/comment/enums/CommentType.java b/src/main/java/itstime/reflog/comment/enums/CommentType.java new file mode 100644 index 0000000..31191f6 --- /dev/null +++ b/src/main/java/itstime/reflog/comment/enums/CommentType.java @@ -0,0 +1,5 @@ +package itstime.reflog.comment.enums; + +public enum CommentType { + COMMUNITY, RETROSPECT +} From e406180a78747c01d7da44d063f3bc9849e0aa33 Mon Sep 17 00:00:00 2001 From: Jeongdonghun Date: Tue, 7 Jan 2025 15:05:23 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor=20:=20=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20API=20=EC=88=98=EC=A0=95=20#48?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/controller/CommentController.java | 8 ++--- .../reflog/comment/domain/Comment.java | 5 +++ .../reflog/comment/dto/CommentDto.java | 2 ++ .../reflog/comment/enums/CommentType.java | 5 --- .../comment/service/CommentService.java | 31 ++++++++++++++++--- .../common/code/status/ErrorStatus.java | 3 +- 6 files changed, 39 insertions(+), 15 deletions(-) delete mode 100644 src/main/java/itstime/reflog/comment/enums/CommentType.java diff --git a/src/main/java/itstime/reflog/comment/controller/CommentController.java b/src/main/java/itstime/reflog/comment/controller/CommentController.java index 831d073..6622f11 100644 --- a/src/main/java/itstime/reflog/comment/controller/CommentController.java +++ b/src/main/java/itstime/reflog/comment/controller/CommentController.java @@ -37,13 +37,13 @@ public class CommentController { ) } ) - @PostMapping("/{communityId}") + @PostMapping("/{postId}") public ResponseEntity> createComment( - @PathVariable Long communityId, + @PathVariable Long postId, @UserId String memberId, @RequestBody CommentDto.CommentSaveOrUpdateRequest dto ) { - commentService.createComment(communityId, memberId, dto); + commentService.createComment(postId, memberId, dto); return ResponseEntity.ok(CommonApiResponse.onSuccess(null)); } @@ -122,7 +122,7 @@ public ResponseEntity> deleteComment( public ResponseEntity> toggleCommentLike( @PathVariable Long commentId, @UserId String memberId - ) { + ) { commentService.toggleCommentLike(commentId, memberId); return ResponseEntity.ok(CommonApiResponse.onSuccess(null)); } diff --git a/src/main/java/itstime/reflog/comment/domain/Comment.java b/src/main/java/itstime/reflog/comment/domain/Comment.java index f814d7f..aba8177 100644 --- a/src/main/java/itstime/reflog/comment/domain/Comment.java +++ b/src/main/java/itstime/reflog/comment/domain/Comment.java @@ -4,6 +4,7 @@ import itstime.reflog.commentLike.domain.CommentLike; import itstime.reflog.community.domain.Community; import itstime.reflog.member.domain.Member; +import itstime.reflog.postlike.domain.enums.PostType; import itstime.reflog.retrospect.domain.Retrospect; import jakarta.persistence.*; import lombok.*; @@ -14,6 +15,7 @@ @Entity @Getter +@Setter @Builder @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor @@ -35,6 +37,9 @@ public class Comment { @JoinColumn(name = "retrospect_id", nullable = true) private Retrospect retrospect; + @Enumerated(EnumType.STRING) + private PostType postType; + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_id", nullable = false) private Member member; diff --git a/src/main/java/itstime/reflog/comment/dto/CommentDto.java b/src/main/java/itstime/reflog/comment/dto/CommentDto.java index bfdae13..4c78c53 100644 --- a/src/main/java/itstime/reflog/comment/dto/CommentDto.java +++ b/src/main/java/itstime/reflog/comment/dto/CommentDto.java @@ -12,6 +12,8 @@ public class CommentDto { @AllArgsConstructor public static class CommentSaveOrUpdateRequest { + private String postType; + @NotBlank(message = "댓글은 비어 있을 수 없습니다.") private String content; diff --git a/src/main/java/itstime/reflog/comment/enums/CommentType.java b/src/main/java/itstime/reflog/comment/enums/CommentType.java deleted file mode 100644 index 31191f6..0000000 --- a/src/main/java/itstime/reflog/comment/enums/CommentType.java +++ /dev/null @@ -1,5 +0,0 @@ -package itstime.reflog.comment.enums; - -public enum CommentType { - COMMUNITY, RETROSPECT -} diff --git a/src/main/java/itstime/reflog/comment/service/CommentService.java b/src/main/java/itstime/reflog/comment/service/CommentService.java index 4dab1fe..dc0527f 100644 --- a/src/main/java/itstime/reflog/comment/service/CommentService.java +++ b/src/main/java/itstime/reflog/comment/service/CommentService.java @@ -11,6 +11,9 @@ import itstime.reflog.community.repository.CommunityRepository; import itstime.reflog.member.domain.Member; import itstime.reflog.member.repository.MemberRepository; +import itstime.reflog.postlike.domain.enums.PostType; +import itstime.reflog.retrospect.domain.Retrospect; +import itstime.reflog.retrospect.repository.RetrospectRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -25,18 +28,30 @@ public class CommentService { private final MemberRepository memberRepository; private final CommunityRepository communityRepository; + private final RetrospectRepository retrospectRepository; private final CommentRepository commentRepository; private final CommentLikeRepository commentLikeRepository; @Transactional - public void createComment(Long communityId, String memberId, CommentDto.CommentSaveOrUpdateRequest dto) { + public void createComment(Long postId, String memberId, CommentDto.CommentSaveOrUpdateRequest dto) { // 1. 멤버 조회 Member member = memberRepository.findByUuid(UUID.fromString(memberId)) .orElseThrow(() -> new GeneralException(ErrorStatus._MEMBER_NOT_FOUND)); - // 2. 커뮤니티 조회 - Community community = communityRepository.findById(communityId) - .orElseThrow(() -> new GeneralException(ErrorStatus._COMMUNITY_NOT_FOUND)); + // 2. 게시물 조회 + PostType postType = PostType.valueOf(dto.getPostType()); + Community community = null; + Retrospect retrospect = null; + + if (postType == PostType.COMMUNITY) { + community = communityRepository.findById(postId) + .orElseThrow(() -> new GeneralException(ErrorStatus._COMMUNITY_NOT_FOUND)); + } else if (postType == PostType.RETROSPECT) { + retrospect = retrospectRepository.findById(postId) + .orElseThrow(() -> new GeneralException(ErrorStatus._RETROSPECT_NOT_FOUND)); + } else { + throw new GeneralException(ErrorStatus._INVALID_POST_TYPE); + } // 3. parent 댓글 확인 Comment parentComment = null; @@ -48,13 +63,19 @@ public void createComment(Long communityId, String memberId, CommentDto.CommentS // 4. 댓글 생성 Comment comment = Comment.builder() .content(dto.getContent()) - .community(community) + .postType(postType) .member(member) .parent(parentComment) .createdAt(LocalDateTime.now()) .updatedAt(LocalDateTime.now()) .build(); + if (postType == PostType.COMMUNITY) { + comment.setCommunity(community); + } else { + comment.setRetrospect(retrospect); + } + // 5. 댓글 저장 commentRepository.save(comment); } diff --git a/src/main/java/itstime/reflog/common/code/status/ErrorStatus.java b/src/main/java/itstime/reflog/common/code/status/ErrorStatus.java index f91e365..e0f24d2 100644 --- a/src/main/java/itstime/reflog/common/code/status/ErrorStatus.java +++ b/src/main/java/itstime/reflog/common/code/status/ErrorStatus.java @@ -63,7 +63,8 @@ public enum ErrorStatus implements BaseErrorCode { _POSTLIKE_BAD_REQUEST(HttpStatus.BAD_REQUEST, "POSTLIKE400", "올바른 PostType을 입력해주세요."), // comment 관련 에러 _COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "COMMENT404", "해당 댓글을 찾을 수 없습니다."), - _PARENT_COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "COMMENT404", "해당 부모 댓글을 찾을 수 없습니다."); + _PARENT_COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "COMMENT404", "해당 부모 댓글을 찾을 수 없습니다."), + _INVALID_POST_TYPE(HttpStatus.NOT_FOUND, "COMMENT404", "해당 댓글에 대한 게시글의 타입이 존재하지 않습니다."); private final HttpStatus httpStatus; private final String code;