Skip to content

Commit 2b14a38

Browse files
authored
refactor/#251 User, Post, Comment, FileEntity간 관계 매핑을 ID 참조로 변경 (#259)
* refactor: Post에서 File을 fileId를 참조하도록 변경 * refactor: Post-User 엔티티 매핑 제거 * refactor: Comment-User&Post 엔티티 매핑 제거 * refactor: Post-Comment 엔티티 매핑 제거 * refactor: 불필요한 import 제거 * test: 테스트용 작성자 ID 상수화 * refactor: updateFile 메서드 명 변경 * refactor: @column name속성 제거 * test: 테스트용 작성자ID, 게시글 ID 상수화 * test: 테스트용 User 정보 추가 * refactor: 만약 시간이 동일할 경우 id를 기준으로 직전/직후 포스트를 조회하도록 쿼리 수정 * refactor: 만약 시간이 동일할 경우 id를 기준으로 조회된 포스트를 정렬하도록 쿼리 수정 * test: FakePostRepository의 게시글 조회 시 고정 여부를 정렬 기준에 포함 * fix: PostQueryService 의존성 추가에 맞춰 FileRepository/FileQueryService 공급자 등록 * fix: Post의 authorId 길이 속성을 10으로 설정
1 parent 85246d2 commit 2b14a38

File tree

29 files changed

+230
-151
lines changed

29 files changed

+230
-151
lines changed

aics-admin/src/testFixtures/java/post/application/PostAdminFacadeTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void init() {
4444
FileQueryService fileQueryService = new FileQueryService(fakeFileRepository);
4545
this.postAdminFacade = new PostAdminFacade(
4646
new PostCommandService(userQueryService, fakePostRepository, fileQueryService),
47-
new PostQueryService(fakePostRepository),
47+
new PostQueryService(fakePostRepository, fakeFileRepository, fakeUserRepository),
4848
new PostSchedulingService(fakePostRepository)
4949
);
5050

@@ -65,7 +65,7 @@ public void init() {
6565

6666
fakePostRepository.save(
6767
Post.create(
68-
"post title", "post content", NOTIFICATION, author, null, false
68+
"post title", "post content", NOTIFICATION, author.getId(), null, false
6969
)
7070
);
7171
}

aics-api/src/main/java/kgu/developers/api/comment/application/CommentFacade.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package kgu.developers.api.comment.application;
22

33
import java.util.List;
4+
import java.util.Map;
5+
import java.util.stream.Collectors;
46

7+
import kgu.developers.domain.user.application.query.UserQueryService;
58
import org.springframework.stereotype.Component;
69
import org.springframework.transaction.annotation.Transactional;
710

@@ -22,6 +25,7 @@ public class CommentFacade {
2225
private final CommentCommandService commentCommandService;
2326
private final CommentQueryService commentQueryService;
2427
private final CommentSchedulingService commentSchedulingService;
28+
private final UserQueryService userQueryService;
2529

2630
public CommentPersistResponse createComment(CommentRequest request) {
2731
Long id = commentCommandService.createComment(request.content(), request.postId());
@@ -30,7 +34,11 @@ public CommentPersistResponse createComment(CommentRequest request) {
3034

3135
public CommentListResponse getComments(Long postId) {
3236
List<Comment> comments = commentQueryService.getComments(postId);
33-
return CommentListResponse.from(comments);
37+
38+
List<String> authorIds = comments.stream().map(Comment::getAuthorId).collect(Collectors.toList());
39+
40+
Map<String, String> nameMap = userQueryService.getUserNameMapByIds(authorIds);
41+
return CommentListResponse.from(comments,nameMap);
3442
}
3543

3644
public void updateComment(Long commentId, CommentUpdateRequest request) {

aics-api/src/main/java/kgu/developers/api/comment/presentation/response/CommentListResponse.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import lombok.Builder;
88

99
import java.util.List;
10+
import java.util.Map;
1011

1112
@Builder
1213
public record CommentListResponse(
@@ -19,10 +20,10 @@ public record CommentListResponse(
1920
requiredMode = REQUIRED)
2021
List<CommentResponse> contents
2122
) {
22-
public static CommentListResponse from(List<Comment> comments) {
23+
public static CommentListResponse from(List<Comment> comments, Map<String,String> nameMap) {
2324
return CommentListResponse.builder()
2425
.contents(comments.stream()
25-
.map(CommentResponse::from)
26+
.map(comment -> CommentResponse.from(comment,nameMap.get(comment.getAuthorId())))
2627
.toList())
2728
.build();
2829
}

aics-api/src/main/java/kgu/developers/api/comment/presentation/response/CommentResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public record CommentResponse(
2424
@Schema(description = "내용", example = "예시 코멘트입니다, 좋은 소식이네요!", requiredMode = REQUIRED)
2525
String content
2626
) {
27-
public static CommentResponse from(Comment comment) {
27+
public static CommentResponse from(Comment comment, String authorName) {
2828
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
2929
return CommentResponse.builder()
3030
.commentId(comment.getId())
31-
.author(comment.getAuthor().getName())
31+
.author(authorName)
3232
.createdAt(comment.getCreatedAt().format(formatter))
3333
.content(comment.getContent())
3434
.build();

aics-api/src/main/java/kgu/developers/api/post/application/PostFacade.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package kgu.developers.api.post.application;
22

33
import java.util.List;
4+
import java.util.Map;
5+
import java.util.stream.Collectors;
46

7+
import kgu.developers.domain.user.application.query.UserQueryService;
8+
import kgu.developers.domain.user.domain.User;
59
import org.springframework.data.domain.PageRequest;
610
import org.springframework.stereotype.Service;
711
import org.springframework.transaction.annotation.Transactional;
@@ -20,12 +24,26 @@
2024
public class PostFacade {
2125
private final PostCommandService postCommandService;
2226
private final PostQueryService postQueryService;
27+
private final UserQueryService userQueryService;
2328

2429
public PostSummaryPageResponse getPostsByKeywordAndCategory(PageRequest request, List<String> keywords,
2530
Category category) {
2631
PaginatedListResponse<Post> paginatedListResponse = postQueryService.getPostsByKeywordAndCategory(request,
2732
keywords, category);
28-
return PostSummaryPageResponse.of(paginatedListResponse.contents(), paginatedListResponse.pageable());
33+
34+
List<String> authorIds = paginatedListResponse.contents().stream()
35+
.map(Post::getAuthorId)
36+
.distinct()
37+
.toList();
38+
39+
Map<String, String> authorNameMap = userQueryService.getAllUsersByIds(authorIds).stream()
40+
.collect(Collectors.toMap(
41+
User::getId,
42+
User::getName
43+
));
44+
45+
return PostSummaryPageResponse.of(paginatedListResponse.contents(),
46+
paginatedListResponse.pageable(), authorNameMap);
2947
}
3048

3149
@Transactional

aics-api/src/main/java/kgu/developers/api/post/presentation/response/PostSummaryPageResponse.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import lombok.Builder;
99

1010
import java.util.List;
11+
import java.util.Map;
1112

1213
@Builder
1314
public record PostSummaryPageResponse<T>(
@@ -28,10 +29,10 @@ public record PostSummaryPageResponse<T>(
2829
@Schema(description = "페이징 정보", requiredMode = REQUIRED)
2930
PageableResponse<T> pageable
3031
) {
31-
public static <T> PostSummaryPageResponse<T> of(List<Post> posts, PageableResponse<T> pageable) {
32+
public static <T> PostSummaryPageResponse<T> of(List<Post> posts, PageableResponse<T> pageable, Map<String, String>authorNameMap) {
3233
return PostSummaryPageResponse.<T>builder()
3334
.contents(posts.stream()
34-
.map(PostSummaryResponse::from)
35+
.map(post -> PostSummaryResponse.from(post, authorNameMap.get(post.getAuthorId())))
3536
.toList())
3637
.pageable(pageable)
3738
.build();

aics-api/src/main/java/kgu/developers/api/post/presentation/response/PostSummaryResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public record PostSummaryResponse(
3939
@DateTimeFormat(pattern = "yyyy-MM-dd")
4040
String createdAt
4141
) {
42-
public static PostSummaryResponse from(Post post) {
42+
public static PostSummaryResponse from(Post post, String authorName) {
4343
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
4444

4545
String content = post.getContent();
@@ -49,7 +49,7 @@ public static PostSummaryResponse from(Post post) {
4949
.postId(post.getId())
5050
.category(post.getCategory().getDescription())
5151
.title(post.getTitle())
52-
.author(post.getAuthor().getName())
52+
.author(authorName)
5353
.description(description)
5454
.views(post.getViews())
5555
.hasAttachment(false) // TODO : 첨부파일 여부 확인

aics-api/src/testFixtures/java/comment/application/CommentFacadeTest.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.List;
88

9+
import mock.repository.FakeFileRepository;
910
import org.junit.jupiter.api.BeforeEach;
1011
import org.junit.jupiter.api.DisplayName;
1112
import org.junit.jupiter.api.Test;
@@ -39,17 +40,21 @@ public class CommentFacadeTest {
3940
public void init() {
4041
FakeCommentRepository fakeCommentRepository = new FakeCommentRepository();
4142
FakeUserRepository fakeUserRepository = new FakeUserRepository();
43+
FakeFileRepository fakeFileRepository = new FakeFileRepository();
4244
FakePostRepository fakePostRepository = new FakePostRepository();
4345

4446
UserQueryService userQueryService = new UserQueryService(fakeUserRepository);
47+
PostQueryService postQueryService = new PostQueryService(fakePostRepository, fakeFileRepository, fakeUserRepository);
48+
4549
commentFacade = new CommentFacade(
4650
new CommentCommandService(
47-
new PostQueryService(fakePostRepository),
51+
postQueryService,
4852
userQueryService,
4953
fakeCommentRepository
5054
),
5155
new CommentQueryService(fakeCommentRepository),
52-
new CommentSchedulingService(fakeCommentRepository)
56+
new CommentSchedulingService(fakeCommentRepository),
57+
userQueryService
5358
);
5459

5560
User author = fakeUserRepository.save(User.builder()
@@ -62,15 +67,15 @@ public void init() {
6267
.build());
6368

6469
Post post = fakePostRepository.save(Post.create(
65-
"테스트용 제목1", "테스트용 내용1", NEWS, author, null, false
70+
"테스트용 제목1", "테스트용 내용1", NEWS, author.getId(), null, false
6671
));
6772

6873
fakeCommentRepository.save(Comment.create(
69-
"test1", author, post
74+
"test1", author.getId(), post.getId()
7075
));
7176

7277
Comment delete = fakeCommentRepository.save(Comment.create(
73-
"test2", author, post
78+
"test2", author.getId(), post.getId()
7479
));
7580
delete.delete();
7681

aics-api/src/testFixtures/java/post/application/PostFacadeTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public void init() {
4848

4949
postFacade = new PostFacade(
5050
new PostCommandService(userQueryService, fakePostRepository, fileQueryService),
51-
new PostQueryService(fakePostRepository)
51+
new PostQueryService(fakePostRepository, fakeFileRepository, fakeUserRepository),
52+
new UserQueryService(fakeUserRepository)
5253
);
5354

5455
User author = fakeUserRepository.save(User.builder()
@@ -67,16 +68,16 @@ public void init() {
6768
);
6869

6970
fakePostRepository.save(Post.create(
70-
"first title", "first content", NEWS, author, null, false
71+
"first title", "first content", NEWS, author.getId(), null, false
7172
));
7273

7374
Post delete = fakePostRepository.save(Post.create(
74-
"second title", "second content", NEWS, author, null, false
75+
"second title", "second content", NEWS, author.getId(), null, false
7576
));
7677
delete.delete();
7778

7879
fakePostRepository.save(Post.create(
79-
"third title", "third content", NEWS, author, null, false
80+
"third title", "third content", NEWS, author.getId(), null, false
8081
));
8182
}
8283

aics-domain/src/main/java/kgu/developers/domain/comment/application/command/CommentCommandService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class CommentCommandService {
2020
public Long createComment(String content, Long postId) {
2121
Post post = postQueryService.getById(postId);
2222
User user = userQueryService.me();
23-
Comment comment = Comment.create(content, user, post);
23+
Comment comment = Comment.create(content, user.getId(), post.getId());
2424
return commentRepository.save(comment).getId();
2525
}
2626

0 commit comments

Comments
 (0)