Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] : 댓글 리펙토링 #53

Merged
merged 2 commits into from
Jan 7, 2025
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 @@ -37,13 +37,13 @@ public class CommentController {
)
}
)
@PostMapping("/{communityId}")
@PostMapping("/{postId}")
public ResponseEntity<CommonApiResponse<Void>> 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));
}

Expand Down Expand Up @@ -122,7 +122,7 @@ public ResponseEntity<CommonApiResponse<Void>> deleteComment(
public ResponseEntity<CommonApiResponse<Void>> toggleCommentLike(
@PathVariable Long commentId,
@UserId String memberId
) {
) {
commentService.toggleCommentLike(commentId, memberId);
return ResponseEntity.ok(CommonApiResponse.onSuccess(null));
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/itstime/reflog/comment/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -14,6 +15,7 @@

@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/itstime/reflog/comment/dto/CommentDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class CommentDto {
@AllArgsConstructor
public static class CommentSaveOrUpdateRequest {

private String postType;

@NotBlank(message = "댓글은 비어 있을 수 없습니다.")
private String content;

Expand Down
31 changes: 26 additions & 5 deletions src/main/java/itstime/reflog/comment/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading