Skip to content

Commit

Permalink
refactor: 코멘트 패키지 리팩토링
Browse files Browse the repository at this point in the history
- domain -> entity로 패키지명 변경
- mapping 메서드 mapper클래스에서 수행하도록 변경
  • Loading branch information
parksangchu committed May 20, 2024
1 parent e49d7a1 commit 903f872
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,9 +18,31 @@
@RequiredArgsConstructor
@Slf4j
public class CommentService {
private final MemberService memberService;
private final CommentRepository commentRepository;
private final FileService fileService;

public List<Comment> findCommentsById(Long id) {
return commentRepository.findAllByIssueId(id);
public List<CommentDetailDto> getCommentDetails(Long id) {
List<Comment> comments = commentRepository.findAllByIssueId(id);
return toCommentDetails(comments);
}

private List<CommentDetailDto> toCommentDetails(List<Comment> comments) {
List<CommentDetailDto> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -43,7 +41,7 @@ public IssueDetailDto showIssue(Long id) {
UploadedFileDto file = getFileByIssue(issue);
List<Label> labels = getIssueLabels(id);
List<SimpleMemberDto> assignees = getIssueAssignees(id);
List<CommentDetailDto> comments = getCommentDetails(id);
List<CommentDetailDto> comments = commentService.getCommentDetails(id);

return IssueMapper.toIssueDetailDto(issue, writer, milestone, file, labels, assignees, comments);
}
Expand All @@ -52,29 +50,6 @@ private SimpleMemberDto getWriter(String memberId) {
return memberService.getSimpleMemberById(memberId);
}

private List<CommentDetailDto> getCommentDetails(Long id) {
List<CommentDetailDto> commentDetails = new ArrayList<>();
List<Comment> comments = commentService.findCommentsById(id);
addCommentDetail(comments, commentDetails);
return commentDetails;
}

private void addCommentDetail(List<Comment> comments, List<CommentDetailDto> commentDetails) {
for (Comment comment : comments) {
SimpleMemberDto writer = memberService.getSimpleMemberById(comment.getMemberId());
UploadedFileDto file = getFileByComment(comment);
CommentDetailDto commentDetail = CommentDetailDto.builder()
.id(comment.getId())
.content(comment.getContent())
.writer(writer)
.isWriter(comment.isWriter())
.file(file)
.createDate(comment.getCreateDate())
.build();
commentDetails.add(commentDetail);
}
}

private SimpleMilestoneDto getMilestone(Issue issue) {
Long milestoneId = issue.getMilestoneId();
if (milestoneId == null) {
Expand All @@ -91,14 +66,6 @@ private UploadedFileDto getFileByIssue(Issue issue) {
return fileService.showFile(fileId);
}

private UploadedFileDto getFileByComment(Comment comment) {
Long fileId = comment.getFileId();
if (fileId == null) {
return null;
}
return fileService.showFile(fileId);
}

private List<Label> getIssueLabels(Long id) {
List<Long> issueLabelIds = issueQueryService.findLabelIdsByIssueId(id);
return labelService.findLabelsByIds(issueLabelIds);
Expand Down

0 comments on commit 903f872

Please sign in to comment.