Skip to content

Commit

Permalink
#1 [refactor] refactoring comment domain model
Browse files Browse the repository at this point in the history
  • Loading branch information
SeorinY committed Oct 11, 2023
1 parent a9b54cf commit c4ecb46
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.qdang.adapter.comment;

import com.qdang.adapter.post.PostMapper;
import com.qdang.adapter.post.impl.PostRepository;
import com.qdang.adapter.user.UserMapper;
import com.qdang.adapter.user.impl.UserRepository;
import com.qdang.application.noticeboard.domain.Comment;
import com.qdang.application.noticeboard.domain.Post;
import com.qdang.application.noticeboard.exception.NotFoundPostException;
import com.qdang.application.user.domain.User;
import com.qdang.application.user.exception.NotFoundUserException;
import com.qdang.global.mapper.Mapper;
import com.qdang.library.mapper.GenericJpaMapper;
Expand All @@ -14,10 +18,13 @@

@Mapper
@RequiredArgsConstructor
public class CommentMapper implements GenericJpaMapper<Comment, CommentJpaEntity> {
public class CommentMapper implements
GenericJpaMapper<Comment, CommentJpaEntity> {

private final UserRepository userRepository;
private final UserMapper userMapper;
private final PostRepository postRepository;
private final PostMapper postMapper;

@Override
public Comment mapToDomainEntity(CommentJpaEntity jpaEntity) {
Expand All @@ -26,8 +33,8 @@ public Comment mapToDomainEntity(CommentJpaEntity jpaEntity) {
}
Comment.CommentBuilder comment = Comment.builder();
comment.id(jpaEntity.getId());
comment.userId(jpaEntity.getUser().getId());
comment.postId(jpaEntity.getPost().getId());
comment.user(getUser(jpaEntity));
comment.post(getPost(jpaEntity));
comment.content(jpaEntity.getContent());
comment.isDeleted(jpaEntity.getIsDeleted());
comment.createdAt(jpaEntity.getCreatedAt());
Expand All @@ -40,18 +47,44 @@ public CommentJpaEntity mapToJpaEntity(Comment domain) {
if (domain == null) {
return null;
}
UserJpaEntity userJpaEntity = userRepository.findById(domain.getUserId())
.orElseThrow(NotFoundUserException::new);
PostJpaEntity postJpaEntity = postRepository.findById(domain.getPostId())
.orElseThrow(NotFoundPostException::new);
CommentJpaEntity.CommentJpaEntityBuilder commentJpaEntity = CommentJpaEntity.builder();
commentJpaEntity.id(domain.getId());
commentJpaEntity.user(userJpaEntity);
commentJpaEntity.post(postJpaEntity);
commentJpaEntity.user(getUserJpaEntity(domain));
commentJpaEntity.post(getPostJpaEntity(domain));
commentJpaEntity.content(domain.getContent());
commentJpaEntity.isDeleted(domain.getIsDeleted());
commentJpaEntity.createdAt(domain.getCreatedAt());
commentJpaEntity.updatedAt(domain.getUpdatedAt());
return commentJpaEntity.build();
}

private User getUser(CommentJpaEntity jpaEntity) {
if (jpaEntity.getUser().getClass() == UserJpaEntity.class) {
return userMapper.mapToDomainEntity(jpaEntity.getUser());
}
return User.init(jpaEntity.getUser().getId());
}

private Post getPost(CommentJpaEntity jpaEntity) {
if (jpaEntity.getPost().getClass() == PostJpaEntity.class) {
return postMapper.mapToDomainEntity(jpaEntity.getPost());
}
return Post.init(jpaEntity.getPost().getId());
}

private UserJpaEntity getUserJpaEntity(Comment domain) {
if (domain.getUser() == null) {
return null;
}
return userRepository.findById(domain.getUser().getId())
.orElseThrow(NotFoundUserException::new);
}

private PostJpaEntity getPostJpaEntity(Comment domain) {
if (domain.getPost() == null) {
return null;
}
return postRepository.findById(domain.getPost().getId())
.orElseThrow(NotFoundPostException::new);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qdang.application.noticeboard.domain;

import com.qdang.application.user.domain.User;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -11,10 +12,16 @@
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Comment {
private Long id;
private Long userId;
private Long postId;
private User user;
private Post post;
private String content;
private Boolean isDeleted;
protected LocalDateTime createdAt;
protected LocalDateTime updatedAt;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;

public static Comment init(Long id) {
return Comment.builder()
.id(id)
.build();
}
}

0 comments on commit c4ecb46

Please sign in to comment.