Skip to content

Commit 705d629

Browse files
authored
refactor/KD-30 User, Comment, Post의 도메인과 JPA 엔티티 분리 (#265)
* refactor: Post 도메인 엔티티 분리 * refactor: Comment 엔티티 도메인 분리 * refactor: User 엔티티 도메인 분리 * refactor: PostRepository delete 메서드 제거 * test: delete후 data를 merge하도록 변경 * refactor: 불필요한 import 제거 * refactor: 불필요한 중복 FROM 제거 * test: FakePostRepository, FakeCommentRepository의 save 메서드 로직을 조회->삭제->저장 순으로 변경 * fix: SwaggerConfig 설정 관련 커밋 실수 undo * refactor: fromDomain 메서드명 toEntity로 변경 * refactor: CommentJpaEntity의 toDomain 파라미터명 올바르게 변경 * test: 테스트 통과를 위해 더미 post에 title, content 주입
1 parent 0f39385 commit 705d629

29 files changed

Lines changed: 427 additions & 121 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void init() {
4343
UserQueryService userQueryService = new UserQueryService(fakeUserRepository);
4444
FileQueryService fileQueryService = new FileQueryService(fakeFileRepository);
4545
this.postAdminFacade = new PostAdminFacade(
46-
new PostCommandService(userQueryService, fakePostRepository, fileQueryService),
46+
new PostCommandService(userQueryService, fakePostRepository),
4747
new PostQueryService(fakePostRepository, fakeFileRepository, fakeUserRepository),
4848
new PostSchedulingService(fakePostRepository)
4949
);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public void init() {
7878
"test2", author.getId(), post.getId()
7979
));
8080
delete.delete();
81+
fakeCommentRepository.save(delete);
8182

8283
UserDetails user = userQueryService.getUserById("202411345");
8384
SecurityContext context = SecurityContextHolder.getContext();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ public void init() {
4444
FakeFileRepository fakeFileRepository = new FakeFileRepository();
4545

4646
UserQueryService userQueryService = new UserQueryService(fakeUserRepository);
47-
FileQueryService fileQueryService = new FileQueryService(fakeFileRepository);
4847

4948
postFacade = new PostFacade(
50-
new PostCommandService(userQueryService, fakePostRepository, fileQueryService),
49+
new PostCommandService(userQueryService, fakePostRepository),
5150
new PostQueryService(fakePostRepository, fakeFileRepository, fakeUserRepository),
5251
new UserQueryService(fakeUserRepository)
5352
);
@@ -75,6 +74,7 @@ public void init() {
7574
"second title", "second content", NEWS, author.getId(), null, false
7675
));
7776
delete.delete();
77+
fakePostRepository.save(delete);
7878

7979
fakePostRepository.save(Post.create(
8080
"third title", "third content", NEWS, author.getId(), null, false

aics-common/src/main/java/kgu/developers/common/domain/BaseTimeEntity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import jakarta.persistence.EntityListeners;
55
import jakarta.persistence.MappedSuperclass;
66
import lombok.Getter;
7+
import lombok.Setter;
78
import org.hibernate.annotations.CreationTimestamp;
89
import org.hibernate.annotations.UpdateTimestamp;
910
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@@ -23,7 +24,8 @@ public abstract class BaseTimeEntity {
2324
@Column(nullable = false)
2425
protected LocalDateTime updatedAt;
2526

26-
@Column
27+
@Setter
28+
@Column
2729
protected LocalDateTime deletedAt;
2830

2931
public void delete(){

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ public Long createComment(String content, Long postId) {
2626

2727
public void updateComment(Comment comment, String content) {
2828
comment.updateContent(content);
29+
commentRepository.save(comment);
2930
}
3031

3132
public void deleteComment(Comment comment) {
3233
comment.delete();
34+
commentRepository.save(comment);
3335
}
3436
}
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,30 @@
11
package kgu.developers.domain.comment.domain;
22

3-
import static jakarta.persistence.GenerationType.IDENTITY;
4-
5-
import jakarta.persistence.Column;
6-
import jakarta.persistence.Entity;
7-
import jakarta.persistence.GeneratedValue;
8-
import jakarta.persistence.Id;
9-
import kgu.developers.common.domain.BaseTimeEntity;
103
import lombok.AllArgsConstructor;
114
import lombok.Builder;
125
import lombok.Getter;
136
import lombok.NoArgsConstructor;
147

15-
@Entity
8+
import java.time.LocalDateTime;
9+
1610
@Getter
1711
@Builder
1812
@NoArgsConstructor
1913
@AllArgsConstructor
20-
public class Comment extends BaseTimeEntity {
21-
@Id
22-
@GeneratedValue(strategy = IDENTITY)
14+
public class Comment {
15+
2316
private Long id;
2417

25-
@Column(nullable = false)
2618
private String content;
2719

28-
@Column(nullable = false)
2920
private Long postId;
3021

31-
@Column(nullable = false)
3222
private String authorId;
3323

24+
protected LocalDateTime createdAt;
25+
protected LocalDateTime updatedAt;
26+
protected LocalDateTime deletedAt;
27+
3428
public static Comment create(String content, String authorId, Long postId) {
3529
return Comment.builder()
3630
.content(content)
@@ -43,4 +37,8 @@ public void updateContent(String content) {
4337
this.content = content;
4438
}
4539

40+
public void delete() {
41+
this.deletedAt = LocalDateTime.now();
42+
}
43+
4644
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package kgu.developers.domain.comment.infrastructure;
2+
3+
import jakarta.persistence.Column;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.GeneratedValue;
6+
import jakarta.persistence.Id;
7+
import jakarta.persistence.Table;
8+
import kgu.developers.common.domain.BaseTimeEntity;
9+
import kgu.developers.domain.comment.domain.Comment;
10+
import lombok.AllArgsConstructor;
11+
import lombok.Builder;
12+
import lombok.Getter;
13+
import lombok.NoArgsConstructor;
14+
15+
import static jakarta.persistence.GenerationType.IDENTITY;
16+
17+
@Entity
18+
@Getter
19+
@Builder
20+
@Table(name = "comment")
21+
@NoArgsConstructor
22+
@AllArgsConstructor
23+
public class CommentJpaEntity extends BaseTimeEntity {
24+
@Id
25+
@GeneratedValue(strategy = IDENTITY)
26+
private Long id;
27+
28+
@Column(nullable = false)
29+
private String content;
30+
31+
@Column(nullable = false)
32+
private Long postId;
33+
34+
@Column(nullable = false)
35+
private String authorId;
36+
37+
public static CommentJpaEntity toEntity(final Comment comment) {
38+
39+
if ( comment == null ) {
40+
return null;
41+
}
42+
43+
CommentJpaEntity entity = CommentJpaEntity.builder()
44+
.id(comment.getId())
45+
.content(comment.getContent())
46+
.postId(comment.getPostId())
47+
.authorId(comment.getAuthorId())
48+
.build();
49+
50+
entity.setDeletedAt(comment.getDeletedAt());
51+
52+
return entity;
53+
}
54+
55+
public static Comment toDomain(CommentJpaEntity entity) {
56+
57+
if ( entity == null ) {
58+
return null;
59+
}
60+
61+
return Comment.builder()
62+
.id(entity.getId())
63+
.content(entity.getContent())
64+
.postId(entity.getPostId())
65+
.authorId(entity.getAuthorId())
66+
.createdAt(entity.getCreatedAt())
67+
.updatedAt(entity.getUpdatedAt())
68+
.deletedAt(entity.getDeletedAt())
69+
.build();
70+
}
71+
72+
}

aics-domain/src/main/java/kgu/developers/domain/comment/infrastructure/CommentRepositoryImpl.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ public class CommentRepositoryImpl implements CommentRepository {
1717

1818
@Override
1919
public Comment save(Comment comment) {
20-
return jpaCommentRepository.save(comment);
20+
CommentJpaEntity entity = CommentJpaEntity.toEntity(comment);
21+
CommentJpaEntity savedEntity = jpaCommentRepository.save(entity);
22+
return CommentJpaEntity.toDomain(savedEntity);
2123
}
2224

2325
@Override
2426
public Optional<Comment> findByIdAndDeletedAtIsNull(Long commentId) {
25-
return jpaCommentRepository.findByIdAndDeletedAtIsNull(commentId);
27+
28+
return jpaCommentRepository.findByIdAndDeletedAtIsNull(commentId)
29+
.map(CommentJpaEntity::toDomain);
2630
}
2731

2832
@Override
@@ -32,7 +36,9 @@ public void deleteAllByDeletedAtBefore(int retentionDays) {
3236

3337
@Override
3438
public List<Comment> findAllByPostIdAndDeletedAtIsNull(Long postId) {
35-
return jpaCommentRepository.findAllByPostIdAndDeletedAtIsNull(postId);
39+
return jpaCommentRepository.findAllByPostIdAndDeletedAtIsNull(postId).stream()
40+
.map(CommentJpaEntity::toDomain)
41+
.toList();
3642
}
3743

3844
}

aics-domain/src/main/java/kgu/developers/domain/comment/infrastructure/JpaCommentRepository.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
import org.springframework.data.jpa.repository.JpaRepository;
77

8-
import kgu.developers.domain.comment.domain.Comment;
8+
public interface JpaCommentRepository extends JpaRepository<CommentJpaEntity, Long> {
9+
List<CommentJpaEntity> findAllByPostIdAndDeletedAtIsNull(Long postId);
910

10-
public interface JpaCommentRepository extends JpaRepository<Comment, Long> {
11-
List<Comment> findAllByPostIdAndDeletedAtIsNull(Long postId);
12-
13-
Optional<Comment> findByIdAndDeletedAtIsNull(Long commentId);
11+
Optional<CommentJpaEntity> findByIdAndDeletedAtIsNull(Long commentId);
1412
}

aics-domain/src/main/java/kgu/developers/domain/comment/infrastructure/QueryCommentRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package kgu.developers.domain.comment.infrastructure;
22

3-
import static kgu.developers.domain.comment.domain.QComment.*;
4-
53
import java.time.LocalDateTime;
64

75
import org.springframework.stereotype.Repository;
@@ -17,6 +15,8 @@ public class QueryCommentRepository {
1715
private final JPAQueryFactory queryFactory;
1816

1917
public void deleteAllByDeletedAtBefore(int retentionDays) {
18+
19+
QCommentJpaEntity comment = QCommentJpaEntity.commentJpaEntity;
2020
LocalDateTime thresholdDate = LocalDateTime.now().minusDays(retentionDays);
2121

2222
queryFactory.delete(comment)

0 commit comments

Comments
 (0)