Skip to content

Commit

Permalink
feat: 코멘트 신규 생성 기능 구현
Browse files Browse the repository at this point in the history
- 사용자가 작성한 정보로 코멘트 생성하고 id를 지정하여 코멘트 상세정보 반환
  • Loading branch information
parksangchu committed May 20, 2024
1 parent 61102e9 commit 4fcd4e5
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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<CommentDetailDto> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<CommentDetailDto> getCommentDetails(Long id) {
List<Comment> comments = commentRepository.findAllByIssueId(id);
public List<CommentDetailDto> getCommentDetails(Long issueId) {
List<Comment> 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<CommentDetailDto> toCommentDetails(List<Comment> comments) {
List<CommentDetailDto> commentDetails = new ArrayList<>();
for (Comment comment : comments) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ public interface IssueRepository extends CrudRepository<Issue, Long> {
@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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 4fcd4e5

Please sign in to comment.