From 4fcd4e511595c41634b86e7b1a47511e6acd0414 Mon Sep 17 00:00:00 2001 From: sangchu Date: Mon, 20 May 2024 17:30:04 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=BD=94=EB=A9=98=ED=8A=B8=20=EC=8B=A0?= =?UTF-8?q?=EA=B7=9C=20=EC=83=9D=EC=84=B1=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 사용자가 작성한 정보로 코멘트 생성하고 id를 지정하여 코멘트 상세정보 반환 --- .../comment/CommentController.java | 29 +++++++++++++++++++ .../comment/dto/CommentCreateRequest.java | 18 ++++++++++++ .../issuetracker/comment/entity/Comment.java | 2 +- .../comment/service/CommentService.java | 24 +++++++++++++-- .../comment/util/CommentMapper.java | 7 +++++ .../issue/repository/IssueRepository.java | 3 ++ .../issue/service/IssueQueryService.java | 6 ++++ 7 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 be/issue-tracker/src/main/java/com/issuetracker/comment/CommentController.java create mode 100644 be/issue-tracker/src/main/java/com/issuetracker/comment/dto/CommentCreateRequest.java diff --git a/be/issue-tracker/src/main/java/com/issuetracker/comment/CommentController.java b/be/issue-tracker/src/main/java/com/issuetracker/comment/CommentController.java new file mode 100644 index 000000000..48499a55a --- /dev/null +++ b/be/issue-tracker/src/main/java/com/issuetracker/comment/CommentController.java @@ -0,0 +1,29 @@ +package com.issuetracker.comment; + +import com.issuetracker.comment.dto.CommentCreateRequest; +import com.issuetracker.comment.dto.CommentDetailDto; +import com.issuetracker.comment.service.CommentService; +import jakarta.validation.Valid; +import java.net.URI; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequestMapping("/api/comments") +@RequiredArgsConstructor +@RestController +public class CommentController { + private final CommentService commentService; + + @PostMapping + public ResponseEntity createComment( + @Valid @RequestBody CommentCreateRequest commentCreateRequest) { + CommentDetailDto commentDetailDto = commentService.createComment(commentCreateRequest); + + URI location = URI.create(String.format("/api/comments/%s", commentDetailDto.getId())); + return ResponseEntity.created(location).body(commentDetailDto); + } +} diff --git a/be/issue-tracker/src/main/java/com/issuetracker/comment/dto/CommentCreateRequest.java b/be/issue-tracker/src/main/java/com/issuetracker/comment/dto/CommentCreateRequest.java new file mode 100644 index 000000000..4794b3c2d --- /dev/null +++ b/be/issue-tracker/src/main/java/com/issuetracker/comment/dto/CommentCreateRequest.java @@ -0,0 +1,18 @@ +package com.issuetracker.comment.dto; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Getter +public class CommentCreateRequest { + @NotBlank + private final String content; + @NotNull + private final Long issueId; + @NotNull + private final String writerId; // 현재 로그인 유지 기능이 없으므로 해당 데이터 주입받아야함. + private final Long fileId; +} diff --git a/be/issue-tracker/src/main/java/com/issuetracker/comment/entity/Comment.java b/be/issue-tracker/src/main/java/com/issuetracker/comment/entity/Comment.java index 44d1b2e43..f3576c42c 100644 --- a/be/issue-tracker/src/main/java/com/issuetracker/comment/entity/Comment.java +++ b/be/issue-tracker/src/main/java/com/issuetracker/comment/entity/Comment.java @@ -9,7 +9,7 @@ @Getter public class Comment { @Id - private Long id; + private final Long id; private final String content; private final LocalDateTime createDate; private final boolean isWriter; 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 d7ea4ab0e..fdefae68c 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,13 +1,16 @@ package com.issuetracker.comment.service; +import com.issuetracker.comment.dto.CommentCreateRequest; 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.issue.service.IssueQueryService; import com.issuetracker.member.dto.SimpleMemberDto; import com.issuetracker.member.service.MemberService; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import lombok.RequiredArgsConstructor; @@ -19,14 +22,31 @@ @Slf4j public class CommentService { private final MemberService memberService; + private final IssueQueryService issueQueryService; private final CommentRepository commentRepository; private final FileService fileService; - public List getCommentDetails(Long id) { - List comments = commentRepository.findAllByIssueId(id); + public List getCommentDetails(Long issueId) { + List comments = commentRepository.findAllByIssueId(issueId); return toCommentDetails(comments); } + /** + * 새로운 코멘트를 작성한다. 작성시간은 현재 시간을 넣고, isWriter 변수는 이슈의 작성자와 동일한지 여부를 확인하여 입력한다. + */ + public CommentDetailDto createComment(CommentCreateRequest request) { + LocalDateTime createDate = LocalDateTime.now(); + boolean isWriter = issueQueryService.hasSameWriter(request.getIssueId(), request.getWriterId()); + + Comment comment = CommentMapper.toComment(request, createDate, isWriter); + Comment saved = commentRepository.save(comment); + log.info("새로운 코멘트가 작성되었습니다. {}", saved); + + SimpleMemberDto writer = memberService.getSimpleMemberById(request.getWriterId()); + UploadedFileDto file = getFileByComment(saved); + return CommentMapper.toCommentDetailDto(saved, writer, file); + } + private List toCommentDetails(List comments) { List commentDetails = new ArrayList<>(); for (Comment comment : comments) { 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 index 0b25f1312..060a062b3 100644 --- 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 @@ -1,9 +1,11 @@ package com.issuetracker.comment.util; +import com.issuetracker.comment.dto.CommentCreateRequest; import com.issuetracker.comment.dto.CommentDetailDto; import com.issuetracker.comment.entity.Comment; import com.issuetracker.file.dto.UploadedFileDto; import com.issuetracker.member.dto.SimpleMemberDto; +import java.time.LocalDateTime; public class CommentMapper { public static CommentDetailDto toCommentDetailDto(Comment comment, SimpleMemberDto writer, UploadedFileDto file) { @@ -16,4 +18,9 @@ public static CommentDetailDto toCommentDetailDto(Comment comment, SimpleMemberD .createDate(comment.getCreateDate()) .build(); } + + public static Comment toComment(CommentCreateRequest request, LocalDateTime createDate, boolean isWriter) { + return new Comment(null, request.getContent(), createDate, isWriter, request.getIssueId(), + request.getWriterId(), request.getFileId()); + } } diff --git a/be/issue-tracker/src/main/java/com/issuetracker/issue/repository/IssueRepository.java b/be/issue-tracker/src/main/java/com/issuetracker/issue/repository/IssueRepository.java index 5f7563d9c..36513f4ba 100644 --- a/be/issue-tracker/src/main/java/com/issuetracker/issue/repository/IssueRepository.java +++ b/be/issue-tracker/src/main/java/com/issuetracker/issue/repository/IssueRepository.java @@ -26,4 +26,7 @@ public interface IssueRepository extends CrudRepository { @Modifying @Query("UPDATE issue SET is_closed = :isClosed WHERE id = :id") void changeStatusById(Long id, boolean isClosed); + + @Query("SELECT member_id FROM issue WHERE id = :id") + String findWriterById(Long id); } diff --git a/be/issue-tracker/src/main/java/com/issuetracker/issue/service/IssueQueryService.java b/be/issue-tracker/src/main/java/com/issuetracker/issue/service/IssueQueryService.java index 24dc3dfb7..21c4fdf6f 100644 --- a/be/issue-tracker/src/main/java/com/issuetracker/issue/service/IssueQueryService.java +++ b/be/issue-tracker/src/main/java/com/issuetracker/issue/service/IssueQueryService.java @@ -62,4 +62,10 @@ public Issue getIssueOrThrow(Long id) { public Long countIssuesByMilestoneIdAndStatus(Long milestoneId, boolean isClosed) { return issueRepository.countByMilestoneIdAndIsClosed(milestoneId, isClosed); } + + @Transactional(readOnly = true) + public boolean hasSameWriter(Long issueId, String memberId) { + String writer = issueRepository.findWriterById(issueId); + return writer.equals(memberId); + } }