Skip to content

Commit e97cdef

Browse files
committed
Feat: 내 댓글 목록 조회 API 구현
1 parent a95cf0e commit e97cdef

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.back.domain.board.comment.dto;
2+
3+
import com.back.domain.board.comment.entity.Comment;
4+
5+
import java.time.LocalDateTime;
6+
7+
/**
8+
* 내 댓글 목록 응답 DTO
9+
*
10+
* @param commentId 댓글 ID
11+
* @param postId 게시글 ID
12+
* @param postTitle 게시글 제목
13+
* @param parentId 부모 댓글 ID
14+
* @param parentContent 부모 댓글 내용 (50자)
15+
* @param content 댓글 내용
16+
* @param likeCount 좋아요 수
17+
* @param createdAt 댓글 생성 일시
18+
* @param updatedAt 댓글 수정 일시
19+
*/
20+
public record MyCommentResponse(
21+
Long commentId,
22+
Long postId,
23+
String postTitle,
24+
Long parentId,
25+
String parentContent,
26+
String content,
27+
long likeCount,
28+
LocalDateTime createdAt,
29+
LocalDateTime updatedAt
30+
) {
31+
public static MyCommentResponse from(Comment comment) {
32+
return new MyCommentResponse(
33+
comment.getId(),
34+
comment.getPost().getId(),
35+
comment.getPost().getTitle(),
36+
comment.getParent() != null
37+
? comment.getParent().getId()
38+
: null,
39+
comment.getParent() != null
40+
? truncate(comment.getParent().getContent())
41+
: null,
42+
comment.getContent(),
43+
comment.getLikeCount(),
44+
comment.getCreatedAt(),
45+
comment.getUpdatedAt()
46+
);
47+
}
48+
49+
private static String truncate(String content) {
50+
int length = 50;
51+
return (content == null || content.length() <= length)
52+
? content
53+
: content.substring(0, length) + "...";
54+
}
55+
}

src/main/java/com/back/domain/board/comment/repository/CommentRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import com.back.domain.board.comment.entity.Comment;
44
import com.back.domain.board.comment.repository.custom.CommentRepositoryCustom;
5+
import org.springframework.data.domain.Page;
6+
import org.springframework.data.domain.Pageable;
57
import org.springframework.data.jpa.repository.JpaRepository;
68
import org.springframework.stereotype.Repository;
79

810
@Repository
911
public interface CommentRepository extends JpaRepository<Comment, Long>, CommentRepositoryCustom {
12+
Page<Comment> findAllByUserId(Long userId, Pageable pageable);
1013
}

src/main/java/com/back/domain/user/controller/UserController.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.back.domain.user.controller;
22

3+
import com.back.domain.board.comment.dto.MyCommentResponse;
34
import com.back.domain.board.common.dto.PageResponse;
45
import com.back.domain.board.post.dto.PostListResponse;
56
import com.back.domain.user.dto.ChangePasswordRequest;
@@ -88,4 +89,18 @@ public ResponseEntity<RsData<PageResponse<PostListResponse>>> getMyPosts(
8889
response
8990
));
9091
}
92+
93+
// 내 댓글 목록 조회
94+
@GetMapping("/me/comments")
95+
public ResponseEntity<RsData<PageResponse<MyCommentResponse>>> getMyComments(
96+
@AuthenticationPrincipal CustomUserDetails user,
97+
@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
98+
) {
99+
PageResponse<MyCommentResponse> response = userService.getMyComments(user.getUserId(), pageable);
100+
return ResponseEntity
101+
.ok(RsData.success(
102+
"내 댓글 목록이 조회되었습니다.",
103+
response
104+
));
105+
}
91106
}

src/main/java/com/back/domain/user/service/UserService.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.back.domain.user.service;
22

3+
import com.back.domain.board.comment.dto.MyCommentResponse;
4+
import com.back.domain.board.comment.repository.CommentRepository;
35
import com.back.domain.board.common.dto.PageResponse;
46
import com.back.domain.board.post.dto.PostListResponse;
57
import com.back.domain.board.post.repository.PostRepository;
@@ -30,6 +32,7 @@ public class UserService {
3032
private final UserRepository userRepository;
3133
private final UserProfileRepository userProfileRepository;
3234
private final PostRepository postRepository;
35+
private final CommentRepository commentRepository;
3336
private final PasswordEncoder passwordEncoder;
3437

3538
/**
@@ -150,6 +153,24 @@ public PageResponse<PostListResponse> getMyPosts(Long userId, Pageable pageable)
150153
return PageResponse.from(page);
151154
}
152155

156+
/**
157+
* 내 댓글 목록 조회 서비스
158+
* 1. 사용자 조회 및 상태 검증
159+
* 2. 댓글 목록 조회
160+
* 3. PageResponse 반환
161+
*/
162+
@Transactional(readOnly = true)
163+
public PageResponse<MyCommentResponse> getMyComments(Long userId, Pageable pageable) {
164+
165+
// 사용자 조회 및 상태 검증
166+
User user = getValidUser(userId);
167+
168+
// 댓글 목록 조회 및 응답 반환
169+
Page<MyCommentResponse> page = commentRepository.findAllByUserId(user.getId(), pageable)
170+
.map(MyCommentResponse::from);
171+
return PageResponse.from(page);
172+
}
173+
153174
/**
154175
* 유효한 사용자 조회 및 상태 검증
155176
*

0 commit comments

Comments
 (0)