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 @@ -41,15 +41,15 @@ public ResponseEntity<RsData<CommentResponse>> createComment(
));
}

// 댓글 다건 조회
// 댓글 목록 조회
@GetMapping
public ResponseEntity<RsData<PageResponse<CommentListResponse>>> getComments(
@PathVariable Long postId,
@AuthenticationPrincipal CustomUserDetails user,
@PageableDefault(sort = "createdAt", direction = Sort.Direction.ASC) Pageable pageable
) {
Long userId = (user != null) ? user.getUserId() : null;
PageResponse<CommentListResponse> response = commentService.getComments(postId, userId, pageable);
PageResponse<CommentListResponse> response = commentService.getComments(postId, pageable, userId);
return ResponseEntity
.status(HttpStatus.OK)
.body(RsData.success(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,18 @@ ResponseEntity<RsData<PageResponse<CommentListResponse>>> getComments(
),
@ApiResponse(
responseCode = "404",
description = "존재하지 않는 게시글 또는 댓글",
description = "존재하지 않는 사용자/게시글/댓글",
content = @Content(
mediaType = "application/json",
examples = {
@ExampleObject(name = "존재하지 않는 사용자", value = """
{
"success": false,
"code": "USER_001",
"message": "존재하지 않는 사용자입니다.",
"data": null
}
"""),
@ExampleObject(name = "존재하지 않는 게시글", value = """
{
"success": false,
Expand Down Expand Up @@ -473,10 +481,18 @@ ResponseEntity<RsData<CommentResponse>> updateComment(
),
@ApiResponse(
responseCode = "404",
description = "존재하지 않는 게시글 또는 댓글",
description = "존재하지 않는 사용자/게시글/댓글",
content = @Content(
mediaType = "application/json",
examples = {
@ExampleObject(name = "존재하지 않는 사용자", value = """
{
"success": false,
"code": "USER_001",
"message": "존재하지 않는 사용자입니다.",
"data": null
}
"""),
@ExampleObject(name = "존재하지 않는 게시글", value = """
{
"success": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface CommentLikeControllerDocs {
examples = @ExampleObject(value = """
{
"success": true,
"code": "SUCCESS_201",
"code": "SUCCESS_200",
"message": "댓글 좋아요가 등록되었습니다.",
"data": {
"commentId": 25,
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/com/back/domain/board/comment/entity/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static Comment createRoot(Post post, User user, String content) {
/** 대댓글 생성 */
public static Comment createChild(Post post, User user, String content, Comment parent) {
Comment comment = new Comment(post, user, content, parent);
parent.getChildren().add(comment);
parent.addChildren(comment);
return comment;
}

Expand All @@ -71,6 +71,14 @@ public void removeLike(CommentLike like) {
this.commentLikes.remove(like);
}

public void addChildren(Comment child) {
this.children.add(child);
}

public void removeChildren(Comment child) {
this.children.remove(child);
}

// -------------------- 비즈니스 메서드 --------------------
/** 댓글 내용 수정 */
public void update(String content) {
Expand All @@ -88,4 +96,14 @@ public void decreaseLikeCount() {
this.likeCount--;
}
}

// -------------------- 헬퍼 메서드 --------------------
/** 댓글 삭제 시 연관관계 정리 */
public void remove() {
this.post.removeComment(this);
this.user.removeComment(this);
if (this.parent != null) {
this.parent.removeChildren(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ public CommentLike(Comment comment, User user) {
comment.addLike(this);
user.addCommentLike(this);
}

// -------------------- 헬퍼 메서드 --------------------
/** 댓글 좋아요 삭제 시 연관관계 정리 */
public void remove() {
this.comment.removeLike(this);
this.user.removeCommentLike(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ public class CommentLikeService {

/**
* 댓글 좋아요 서비스
* 1. User 조회
* 2. Comment 조회
* 3. 이미 존재하는 경우 예외 처리
* 4. CommentLike 저장 및 likeCount 증가
* 5. 알림 이벤트 발행 (자기 글이 아닐 경우)
*
* @param commentId 댓글 ID
* @param userId 사용자 ID
* @return 댓글 좋아요 응답 DTO
*/
public CommentLikeResponse likeComment(Long commentId, Long userId) {
// User 조회
Expand All @@ -41,21 +40,20 @@ public CommentLikeResponse likeComment(Long commentId, Long userId) {
Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND));

// 이미 좋아요를 누른 경우 예외
// 이미 좋아요한 경우 예외
if (commentLikeRepository.existsByUserIdAndCommentId(userId, commentId)) {
throw new CustomException(ErrorCode.COMMENT_ALREADY_LIKED);
}

// 좋아요 수 증가
// CommentLike 생성 및 좋아요 수 증가 처리
comment.increaseLikeCount();

// CommentLike 저장 및 응답 반환
commentLikeRepository.save(new CommentLike(comment, user));

// 댓글 좋아요 이벤트 발행 (자기 댓글이 아닐 때만)
if (!comment.getUser().getId().equals(userId)) {
eventPublisher.publishEvent(
new CommentLikedEvent(
userId, // 좋아요 누른 사람
userId, // 좋아요한 사용자
comment.getUser().getId(), // 댓글 작성자
comment.getPost().getId(), // 게시글 ID
comment.getId(),
Expand All @@ -69,10 +67,10 @@ public CommentLikeResponse likeComment(Long commentId, Long userId) {

/**
* 댓글 좋아요 취소 서비스
* 1. User 조회
* 2. Comment 조회
* 3. CommentLike 조회
* 4. CommentLike 삭제 및 likeCount 감소
*
* @param commentId 댓글 ID
* @param userId 사용자 ID
* @return 댓글 좋아요 응답 DTO
*/
public CommentLikeResponse cancelLikeComment(Long commentId, Long userId) {
// User 조회
Expand All @@ -87,17 +85,11 @@ public CommentLikeResponse cancelLikeComment(Long commentId, Long userId) {
CommentLike commentLike = commentLikeRepository.findByUserIdAndCommentId(userId, commentId)
.orElseThrow(() -> new CustomException(ErrorCode.COMMENT_LIKE_NOT_FOUND));

// 연관관계 제거
comment.removeLike(commentLike);
user.removeCommentLike(commentLike);

// CommentLike 삭제
// CommentLike 삭제 및 좋아요 수 감소 처리
commentLike.remove();
commentLikeRepository.delete(commentLike);

// 좋아요 수 감소
comment.decreaseLikeCount();

// 응답 반환
return CommentLikeResponse.from(comment);
}
}
Loading