From 903f8725ffdf2693c7eb8ec0c968033100de0a41 Mon Sep 17 00:00:00 2001 From: sangchu Date: Mon, 20 May 2024 16:36:01 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=BD=94=EB=A9=98=ED=8A=B8=20?= =?UTF-8?q?=ED=8C=A8=ED=82=A4=EC=A7=80=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - domain -> entity로 패키지명 변경 - mapping 메서드 mapper클래스에서 수행하도록 변경 --- .../comment/{domain => entity}/Comment.java | 4 +-- .../comment/repository/CommentRepository.java | 2 +- .../comment/service/CommentService.java | 35 +++++++++++++++++-- .../comment/util/CommentMapper.java | 19 ++++++++++ .../issue/service/IssueDetailService.java | 35 +------------------ 5 files changed, 54 insertions(+), 41 deletions(-) rename be/issue-tracker/src/main/java/com/issuetracker/comment/{domain => entity}/Comment.java (85%) create mode 100644 be/issue-tracker/src/main/java/com/issuetracker/comment/util/CommentMapper.java diff --git a/be/issue-tracker/src/main/java/com/issuetracker/comment/domain/Comment.java b/be/issue-tracker/src/main/java/com/issuetracker/comment/entity/Comment.java similarity index 85% rename from be/issue-tracker/src/main/java/com/issuetracker/comment/domain/Comment.java rename to be/issue-tracker/src/main/java/com/issuetracker/comment/entity/Comment.java index 8648b058b..44d1b2e43 100644 --- a/be/issue-tracker/src/main/java/com/issuetracker/comment/domain/Comment.java +++ b/be/issue-tracker/src/main/java/com/issuetracker/comment/entity/Comment.java @@ -1,16 +1,14 @@ -package com.issuetracker.comment.domain; +package com.issuetracker.comment.entity; import java.time.LocalDateTime; import lombok.Getter; import lombok.RequiredArgsConstructor; -import lombok.Setter; import org.springframework.data.annotation.Id; @RequiredArgsConstructor @Getter public class Comment { @Id - @Setter private Long id; private final String content; private final LocalDateTime createDate; diff --git a/be/issue-tracker/src/main/java/com/issuetracker/comment/repository/CommentRepository.java b/be/issue-tracker/src/main/java/com/issuetracker/comment/repository/CommentRepository.java index dcb933010..9810e8c15 100644 --- a/be/issue-tracker/src/main/java/com/issuetracker/comment/repository/CommentRepository.java +++ b/be/issue-tracker/src/main/java/com/issuetracker/comment/repository/CommentRepository.java @@ -1,6 +1,6 @@ package com.issuetracker.comment.repository; -import com.issuetracker.comment.domain.Comment; +import com.issuetracker.comment.entity.Comment; import java.util.List; import org.springframework.data.repository.CrudRepository; diff --git a/be/issue-tracker/src/main/java/com/issuetracker/comment/service/CommentService.java b/be/issue-tracker/src/main/java/com/issuetracker/comment/service/CommentService.java index 5f393bc25..d7ea4ab0e 100644 --- a/be/issue-tracker/src/main/java/com/issuetracker/comment/service/CommentService.java +++ b/be/issue-tracker/src/main/java/com/issuetracker/comment/service/CommentService.java @@ -1,7 +1,14 @@ package com.issuetracker.comment.service; -import com.issuetracker.comment.domain.Comment; +import com.issuetracker.comment.dto.CommentDetailDto; +import com.issuetracker.comment.entity.Comment; import com.issuetracker.comment.repository.CommentRepository; +import com.issuetracker.comment.util.CommentMapper; +import com.issuetracker.file.dto.UploadedFileDto; +import com.issuetracker.file.service.FileService; +import com.issuetracker.member.dto.SimpleMemberDto; +import com.issuetracker.member.service.MemberService; +import java.util.ArrayList; import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -11,9 +18,31 @@ @RequiredArgsConstructor @Slf4j public class CommentService { + private final MemberService memberService; private final CommentRepository commentRepository; + private final FileService fileService; - public List findCommentsById(Long id) { - return commentRepository.findAllByIssueId(id); + public List getCommentDetails(Long id) { + List comments = commentRepository.findAllByIssueId(id); + return toCommentDetails(comments); + } + + private List toCommentDetails(List comments) { + List commentDetails = new ArrayList<>(); + for (Comment comment : comments) { + SimpleMemberDto writer = memberService.getSimpleMemberById(comment.getMemberId()); + UploadedFileDto file = getFileByComment(comment); + CommentDetailDto commentDetail = CommentMapper.toCommentDetailDto(comment, writer, file); + commentDetails.add(commentDetail); + } + return commentDetails; + } + + private UploadedFileDto getFileByComment(Comment comment) { + Long fileId = comment.getFileId(); + if (fileId == null) { + return null; + } + return fileService.showFile(fileId); } } diff --git a/be/issue-tracker/src/main/java/com/issuetracker/comment/util/CommentMapper.java b/be/issue-tracker/src/main/java/com/issuetracker/comment/util/CommentMapper.java new file mode 100644 index 000000000..0b25f1312 --- /dev/null +++ b/be/issue-tracker/src/main/java/com/issuetracker/comment/util/CommentMapper.java @@ -0,0 +1,19 @@ +package com.issuetracker.comment.util; + +import com.issuetracker.comment.dto.CommentDetailDto; +import com.issuetracker.comment.entity.Comment; +import com.issuetracker.file.dto.UploadedFileDto; +import com.issuetracker.member.dto.SimpleMemberDto; + +public class CommentMapper { + public static CommentDetailDto toCommentDetailDto(Comment comment, SimpleMemberDto writer, UploadedFileDto file) { + return CommentDetailDto.builder() + .id(comment.getId()) + .content(comment.getContent()) + .writer(writer) + .isWriter(comment.isWriter()) + .file(file) + .createDate(comment.getCreateDate()) + .build(); + } +} diff --git a/be/issue-tracker/src/main/java/com/issuetracker/issue/service/IssueDetailService.java b/be/issue-tracker/src/main/java/com/issuetracker/issue/service/IssueDetailService.java index 5f96db306..b148f0bb6 100644 --- a/be/issue-tracker/src/main/java/com/issuetracker/issue/service/IssueDetailService.java +++ b/be/issue-tracker/src/main/java/com/issuetracker/issue/service/IssueDetailService.java @@ -1,6 +1,5 @@ package com.issuetracker.issue.service; -import com.issuetracker.comment.domain.Comment; import com.issuetracker.comment.dto.CommentDetailDto; import com.issuetracker.comment.service.CommentService; import com.issuetracker.file.dto.UploadedFileDto; @@ -14,7 +13,6 @@ import com.issuetracker.member.service.MemberService; import com.issuetracker.milestone.dto.SimpleMilestoneDto; import com.issuetracker.milestone.service.MilestoneService; -import java.util.ArrayList; import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -43,7 +41,7 @@ public IssueDetailDto showIssue(Long id) { UploadedFileDto file = getFileByIssue(issue); List