Skip to content

Commit

Permalink
feat: 코멘트 삭제 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
parksangchu committed May 21, 2024
1 parent b372359 commit 6343a17
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.net.URI;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
Expand Down Expand Up @@ -36,4 +37,10 @@ public ResponseEntity<Void> modifyComment(@PathVariable Long id,
commentService.modifyComment(id, commentModifyRequest);
return ResponseEntity.ok().build();
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteComment(@PathVariable Long id) {
commentService.deleteComment(id);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -36,6 +37,7 @@ public List<CommentDetailDto> getCommentDetails(Long issueId) {
/**
* 새로운 코멘트를 작성한다. 작성시간은 현재 시간을 넣고, isWriter 변수는 이슈의 작성자와 동일한지 여부를 확인하여 입력한다.
*/
@Transactional
public CommentDetailDto createComment(CommentCreateRequest request) {
LocalDateTime createDate = LocalDateTime.now();
boolean isWriter = issueQueryService.hasSameWriter(request.getIssueId(), request.getWriterId());
Expand All @@ -49,6 +51,7 @@ public CommentDetailDto createComment(CommentCreateRequest request) {
return CommentMapper.toCommentDetailDto(saved, writer, file);
}

@Transactional
public void modifyComment(Long id, CommentModifyRequest commentModifyRequest) {
int affectedRow = commentRepository.updateBodyById(id, commentModifyRequest.getContent(),
commentModifyRequest.getFileId());
Expand All @@ -57,6 +60,12 @@ public void modifyComment(Long id, CommentModifyRequest commentModifyRequest) {
}
}

@Transactional
public void deleteComment(Long id) {
validateCommentExists(id);
commentRepository.deleteById(id);
}

private List<CommentDetailDto> toCommentDetails(List<Comment> comments) {
List<CommentDetailDto> commentDetails = new ArrayList<>();
for (Comment comment : comments) {
Expand All @@ -75,4 +84,10 @@ private UploadedFileDto getFileByComment(Comment comment) {
}
return fileService.showFile(fileId);
}

private void validateCommentExists(Long id) {
if (!commentRepository.existsById(id)) {
throw new CommentNotFoundException();
}
}
}

0 comments on commit 6343a17

Please sign in to comment.