Skip to content

Commit e23176a

Browse files
authored
[feat] 댓글 단건 조회 기능 구현#64
[feat] 댓글 단건 조회 기능 구현#64
2 parents ff008b5 + 77cc961 commit e23176a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/main/java/com/back/domain/post/comment/controller/CommentController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.back.domain.post.comment.dto.request.CommentCreateRequestDto;
44
import com.back.domain.post.comment.dto.response.CommentResponseDto;
55
import com.back.domain.post.comment.service.CommentService;
6+
import com.back.domain.post.post.dto.response.PostResponseDto;
67
import com.back.global.rsData.RsData;
78
import io.swagger.v3.oas.annotations.Operation;
89
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -48,4 +49,13 @@ public RsData<List<CommentResponseDto>> getComments(
4849
) {
4950
return RsData.successOf(commentService.getComments(postId, lastId)); // code=200, message="success"
5051
}
52+
53+
@GetMapping("/{commentId}")
54+
@Operation(summary = "댓글 단건 조회")
55+
public RsData<CommentResponseDto> getComment(
56+
@PathVariable Long postId,
57+
@PathVariable Long commentId
58+
) {
59+
return RsData.successOf(commentService.getComment(postId, commentId)); // code=200, message="success"
60+
}
5161
}

src/main/java/com/back/domain/post/comment/service/CommentService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,17 @@ public List<CommentResponseDto> getComments(Long postId, Long lastId) {
5353
.toList();
5454
}
5555
}
56+
57+
// 댓글 단건 조회 로직
58+
@Transactional(readOnly = true)
59+
public CommentResponseDto getComment(Long postId, Long commentId) {
60+
Comment comment = commentRepository.findById(commentId)
61+
.orElseThrow(() -> new IllegalArgumentException("댓글이 존재하지 않습니다. id=" + commentId));
62+
63+
if (!comment.getPost().getId().equals(postId)) {
64+
throw new IllegalStateException("댓글이 해당 게시글에 속하지 않습니다.");
65+
}
66+
67+
return new CommentResponseDto(comment);
68+
}
5669
}

0 commit comments

Comments
 (0)