Skip to content

Commit ada4a59

Browse files
committed
✨ list user comments
1 parent d1fc5c5 commit ada4a59

File tree

6 files changed

+78
-1
lines changed

6 files changed

+78
-1
lines changed

backend/src/main/java/net/pengcook/comment/controller/CommentController.java

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import lombok.RequiredArgsConstructor;
66
import net.pengcook.authentication.domain.UserInfo;
77
import net.pengcook.authentication.resolver.LoginUser;
8+
import net.pengcook.comment.dto.CommentOfUserResponse;
89
import net.pengcook.comment.dto.CommentOfRecipeResponse;
910
import net.pengcook.comment.dto.CreateCommentRequest;
1011
import net.pengcook.comment.service.CommentService;
@@ -41,4 +42,9 @@ public void createComment(@RequestBody @Valid CreateCommentRequest request, @Log
4142
public void deleteComment(@PathVariable long commentId, @LoginUser UserInfo userInfo) {
4243
commentService.deleteComment(commentId, userInfo);
4344
}
45+
46+
@GetMapping("/mine")
47+
public List<CommentOfUserResponse> readCommentsOfMe(@LoginUser UserInfo userInfo) {
48+
return commentService.readCommentsOfUser(userInfo);
49+
}
4450
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package net.pengcook.comment.dto;
2+
3+
import java.time.LocalDateTime;
4+
import net.pengcook.comment.domain.Comment;
5+
6+
public record CommentOfUserResponse(
7+
long commentId,
8+
long recipeId,
9+
String recipeTitle,
10+
LocalDateTime createdAt,
11+
String message) {
12+
13+
public CommentOfUserResponse(Comment comment) {
14+
this(
15+
comment.getId(),
16+
comment.getRecipe().getId(),
17+
comment.getRecipe().getTitle(),
18+
comment.getCreatedAt(),
19+
comment.getMessage());
20+
}
21+
}

backend/src/main/java/net/pengcook/comment/repository/CommentRepository.java

+2
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
1111
void deleteByRecipeId(long recipeId);
1212

1313
void deleteByUserId(long userId);
14+
15+
List<Comment> findAllByUserId(long userId);
1416
}

backend/src/main/java/net/pengcook/comment/service/CommentService.java

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import lombok.RequiredArgsConstructor;
66
import net.pengcook.authentication.domain.UserInfo;
77
import net.pengcook.comment.domain.Comment;
8+
import net.pengcook.comment.dto.CommentOfUserResponse;
89
import net.pengcook.comment.dto.CommentOfRecipeResponse;
910
import net.pengcook.comment.dto.CreateCommentRequest;
1011
import net.pengcook.comment.exception.NotFoundException;
@@ -72,6 +73,15 @@ public void deleteCommentsByUser(long userId) {
7273
commentRepository.deleteByUserId(userId);
7374
}
7475

76+
@Transactional(readOnly = true)
77+
public List<CommentOfUserResponse> readCommentsOfUser(UserInfo userInfo) {
78+
List<Comment> comments = commentRepository.findAllByUserId(userInfo.getId());
79+
80+
return comments.stream()
81+
.map(CommentOfUserResponse::new)
82+
.toList();
83+
}
84+
7585
private boolean isCommentOwner(UserInfo userInfo, Comment comment) {
7686
User owner = comment.getUser();
7787
long userId = userInfo.getId();

backend/src/test/java/net/pengcook/comment/controller/CommentControllerTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
class CommentControllerTest extends RestDocsSetting {
2727

2828
private static final int COMMENT_COUNT_OF_FIRST_RECIPE = 3;
29+
private static final int COMMENT_COUNT_OF_LOKI = 2;
2930

3031
@Test
3132
@WithLoginUser(email = "ela@pengcook.net")
@@ -172,4 +173,25 @@ void deleteCommentWhenNotCommentOwner() {
172173
.then().log().all()
173174
.statusCode(403);
174175
}
176+
177+
@Test
178+
@WithLoginUser(email = "loki@pengcook.net")
179+
@DisplayName("로그인한 사용자의 댓글을 조회한다.")
180+
void readCommentsOfUser() {
181+
RestAssured.given(spec).log().all()
182+
.filter(document(DEFAULT_RESTDOCS_PATH,
183+
"로그인한 사용자의 댓글을 조회합니다.",
184+
"내 댓글 조회 API",
185+
responseFields(
186+
fieldWithPath("[]").description("댓글 목록"),
187+
fieldWithPath("[].commentId").description("댓글 아이디"),
188+
fieldWithPath("[].recipeId").description("레시피 아이디"),
189+
fieldWithPath("[].recipeTitle").description("레시피 제목"),
190+
fieldWithPath("[].createdAt").description("작성 시간"),
191+
fieldWithPath("[].message").description("내용")
192+
)))
193+
.when().get("/comments/mine")
194+
.then().log().all()
195+
.body("size()", is(COMMENT_COUNT_OF_LOKI));
196+
}
175197
}

backend/src/test/java/net/pengcook/comment/service/CommentServiceTest.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99
import net.pengcook.authentication.domain.UserInfo;
1010
import net.pengcook.comment.dto.CommentOfRecipeResponse;
11+
import net.pengcook.comment.dto.CommentOfUserResponse;
1112
import net.pengcook.comment.dto.CreateCommentRequest;
1213
import net.pengcook.comment.exception.NotFoundException;
1314
import net.pengcook.comment.exception.UnauthorizedDeletionException;
@@ -81,7 +82,8 @@ void deleteComment() {
8182

8283
assertAll(
8384
() -> assertThat(commentRepository.count()).isEqualTo(INITIAL_COMMENT_COUNT - 1),
84-
() -> assertThat(recipeRepository.findById(recipeId).orElseThrow().getCommentCount()).isEqualTo(before - 1)
85+
() -> assertThat(recipeRepository.findById(recipeId).orElseThrow().getCommentCount()).isEqualTo(
86+
before - 1)
8587
);
8688
}
8789

@@ -120,4 +122,18 @@ void deleteCommentsByUser() {
120122

121123
assertThat(commentRepository.count()).isEqualTo(INITIAL_COMMENT_COUNT - 2);
122124
}
125+
126+
@Test
127+
@DisplayName("특정 사용자의 댓글을 조회한다.")
128+
void readCommentsOfUser() {
129+
UserInfo userInfo = new UserInfo(2, "loki@pengcook.net");
130+
List<CommentOfUserResponse> expect = List.of(
131+
new CommentOfUserResponse(1L, 1L, "김밥", LocalDateTime.of(2024, 1, 1, 0, 0, 0), "great"),
132+
new CommentOfUserResponse(3L, 2L, "김치찌개", LocalDateTime.of(2024, 5, 5, 0, 0, 0), "good")
133+
);
134+
135+
List<CommentOfUserResponse> actual = commentService.readCommentsOfUser(userInfo);
136+
137+
assertThat(actual).containsExactlyInAnyOrderElementsOf(expect);
138+
}
123139
}

0 commit comments

Comments
 (0)