Skip to content

Commit a2514bd

Browse files
committed
CLAP-185 fix: 댓글 삭제 logic 리팩토링 진행
1 parent 623b323 commit a2514bd

File tree

10 files changed

+32
-56
lines changed

10 files changed

+32
-56
lines changed

src/main/java/clap/server/adapter/inbound/web/comment/CommandCommentController.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package clap.server.adapter.inbound.web.comment;
22

33
import clap.server.adapter.inbound.security.SecurityUserDetails;
4-
import clap.server.adapter.inbound.web.dto.task.DeleteCommentRequest;
54
import clap.server.adapter.inbound.web.dto.task.PostAndEditCommentRequest;
65
import clap.server.application.port.inbound.comment.CommandCommentUsecase;
76
import clap.server.common.annotation.architecture.WebAdapter;
@@ -40,9 +39,8 @@ public void editComment(
4039
@Secured({"ROLE_MANAGER", "ROLE_USER"})
4140
public void deleteComment(
4241
@AuthenticationPrincipal SecurityUserDetails userInfo,
43-
@PathVariable Long commentId,
44-
@RequestBody DeleteCommentRequest request) {
45-
commandCommentUsecase.deleteComment(userInfo.getUserId(), commentId, request);
42+
@PathVariable Long commentId) {
43+
commandCommentUsecase.deleteComment(userInfo.getUserId(), commentId);
4644
}
4745

4846
}

src/main/java/clap/server/adapter/inbound/web/dto/task/DeleteCommentRequest.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/main/java/clap/server/adapter/outbound/persistense/AttachmentPersistenceAdapter.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,9 @@ public List<Attachment> findAllByTaskIdAndCommentIsNullAndAttachmentId(final Lon
5454
}
5555

5656
@Override
57-
public List<Attachment> findAllyByTaskIdAndCommentIdAndAttachmentId(Long taskId, Long commentId, List<Long> attachmentIds) {
58-
List<AttachmentEntity> attachmentEntities = attachmentRepository.findActiveAttachmentsByTask_TaskIdAndComment_CommentIdAndAttachmentIdIn(taskId, commentId, attachmentIds);
59-
return attachmentEntities.stream()
60-
.map(attachmentPersistenceMapper::toDomain)
61-
.collect(Collectors.toList());
57+
public Optional<Attachment> findByCommentId(Long commentId) {
58+
Optional<AttachmentEntity> attachmentEntity = attachmentRepository.findByComment_CommentIdAndIsDeletedFalse(commentId);
59+
return attachmentEntity.map(attachmentPersistenceMapper::toDomain);
6260
}
6361

6462
@Override
@@ -68,4 +66,9 @@ public List<Attachment> findAllByTaskIdAndCommentIsNotNull(final Long taskId) {
6866
.map(attachmentPersistenceMapper::toDomain)
6967
.collect(Collectors.toList());
7068
}
69+
70+
@Override
71+
public boolean exitsByCommentId(Long commentId) {
72+
return attachmentRepository.existsByComment_CommentId(commentId);
73+
}
7174
}

src/main/java/clap/server/adapter/outbound/persistense/repository/task/AttachmentRepository.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import org.springframework.stereotype.Repository;
55

66
import java.util.List;
7+
import java.util.Optional;
78

89
@Repository
910
public interface AttachmentRepository extends JpaRepository<AttachmentEntity, Long> {
1011
List<AttachmentEntity> findAllByTask_TaskIdAndCommentIsNullAndIsDeletedIsFalse(Long taskId);
1112
List<AttachmentEntity> findAllByTask_TaskIdAndCommentIsNullAndIsDeletedIsFalseAndAttachmentIdIn(Long task_taskId, List<Long> attachmentId);
12-
List<AttachmentEntity> findActiveAttachmentsByTask_TaskIdAndComment_CommentIdAndAttachmentIdIn(Long task_taskId, Long comment_commentId, List<Long> attachmentIds);
13+
Optional<AttachmentEntity> findByComment_CommentIdAndIsDeletedFalse(Long commentId);
14+
boolean existsByComment_CommentId(Long commentId);
1315
List<AttachmentEntity> findAllByTask_TaskIdAndCommentIsNotNullAndIsDeletedIsFalse(Long taskId);
1416
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package clap.server.application.port.inbound.comment;
22

3-
import clap.server.adapter.inbound.web.dto.task.DeleteCommentRequest;
43
import clap.server.adapter.inbound.web.dto.task.PostAndEditCommentRequest;
54

6-
import java.util.List;
7-
85
public interface CommandCommentUsecase {
96

107
void updateComment(Long userId, Long commentId, PostAndEditCommentRequest request);
118

12-
void deleteComment(Long userId, Long commentId, DeleteCommentRequest request);
9+
void deleteComment(Long userId, Long commentId);
1310
}

src/main/java/clap/server/application/port/outbound/task/CommandCommentPort.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
public interface CommandCommentPort {
66

77
Comment saveComment(Comment comment);
8+
9+
void deleteComment(Comment comment);
810
}

src/main/java/clap/server/application/port/outbound/task/LoadAttachmentPort.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import clap.server.domain.model.task.Attachment;
44

55
import java.util.List;
6+
import java.util.Optional;
67

78

89
public interface LoadAttachmentPort {
910
List<Attachment> findAllByTaskIdAndCommentIsNull(Long taskId);
1011
List<Attachment> findAllByTaskIdAndCommentIsNullAndAttachmentId(Long taskId, List<Long> attachmentIds);
11-
List<Attachment> findAllyByTaskIdAndCommentIdAndAttachmentId(Long taskId, Long commentId, List<Long> attachmentIds);
12+
Optional<Attachment> findByCommentId(Long commentId);
1213
List<Attachment> findAllByTaskIdAndCommentIsNotNull(Long taskId);
14+
boolean exitsByCommentId(Long commentId);
1315
}
Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package clap.server.application.service.comment;
22

3-
import clap.server.adapter.inbound.web.dto.task.DeleteCommentRequest;
43
import clap.server.adapter.inbound.web.dto.task.PostAndEditCommentRequest;
5-
import clap.server.adapter.outbound.persistense.entity.member.constant.MemberRole;
64
import clap.server.application.port.inbound.comment.CommandCommentUsecase;
75
import clap.server.application.port.inbound.domain.MemberService;
86
import clap.server.application.port.outbound.task.CommandAttachmentPort;
@@ -13,16 +11,11 @@
1311
import clap.server.domain.model.member.Member;
1412
import clap.server.domain.model.task.Attachment;
1513
import clap.server.domain.model.task.Comment;
16-
import clap.server.domain.model.task.Task;
1714
import clap.server.exception.ApplicationException;
1815
import clap.server.exception.code.CommentErrorCode;
19-
import clap.server.exception.code.MemberErrorCode;
20-
import clap.server.exception.code.TaskErrorCode;
2116
import lombok.RequiredArgsConstructor;
2217
import org.springframework.transaction.annotation.Transactional;
2318

24-
import java.util.List;
25-
2619
@ApplicationService
2720
@RequiredArgsConstructor
2821
public class CommandCommentService implements CommandCommentUsecase {
@@ -51,36 +44,25 @@ public void updateComment(Long userId, Long commentId, PostAndEditCommentRequest
5144

5245
@Transactional
5346
@Override
54-
public void deleteComment(Long userId, Long commentId, DeleteCommentRequest request) {
47+
public void deleteComment(Long userId, Long commentId) {
5548
Member member = memberService.findActiveMember(userId);
5649

5750

5851
Comment comment = loadCommentPort.findById(commentId)
5952
.orElseThrow(() -> new ApplicationException(CommentErrorCode.COMMENT_NOT_FOUND));
6053

6154
if (Member.checkCommenter(comment.getTask(), member)) {
62-
63-
// 삭제할 댓글이 첨부파일일 경우
64-
if (!request.attachmentsToDelete().isEmpty()) {
65-
deleteAttachments(request.attachmentsToDelete(), comment.getTask(), comment.getCommentId());
55+
if (loadAttachmentPort.exitsByCommentId(commentId)) {
56+
deleteAttachments(commentId);
6657
}
67-
68-
comment.softDelete();
69-
commandCommentPort.saveComment(comment);
58+
commandCommentPort.deleteComment(comment);
7059
};
7160
}
7261

73-
private void deleteAttachments(List<Long> attachmentIdsToDelete, Task task, Long commentId) {
74-
List<Attachment> attachmentsToDelete = validateAndGetAttachments(attachmentIdsToDelete, task, commentId);
75-
attachmentsToDelete.forEach(Attachment::softDelete);
76-
commandAttachmentPort.saveAll(attachmentsToDelete);
77-
}
78-
79-
private List<Attachment> validateAndGetAttachments(List<Long> attachmentIdsToDelete, Task task, Long commentId) {
80-
List<Attachment> attachmentsOfTask = loadAttachmentPort.findAllyByTaskIdAndCommentIdAndAttachmentId(task.getTaskId(), commentId, attachmentIdsToDelete);
81-
if (attachmentsOfTask.size() != attachmentIdsToDelete.size()) {
82-
throw new ApplicationException(TaskErrorCode.TASK_ATTACHMENT_NOT_FOUND);
83-
}
84-
return attachmentsOfTask;
62+
private void deleteAttachments(Long commentId) {
63+
Attachment attachment = loadAttachmentPort.findByCommentId(commentId)
64+
.orElseThrow(() -> new ApplicationException(CommentErrorCode.COMMENT_ATTACHMENT_NOT_FOUND));
65+
attachment.softDelete();
66+
commandAttachmentPort.save(attachment);
8567
}
8668
}

src/main/java/clap/server/domain/model/task/Comment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import lombok.Getter;
88
import lombok.NoArgsConstructor;
99
import lombok.experimental.SuperBuilder;
10+
import org.hibernate.annotations.SQLDelete;
1011

1112
@Getter
1213
@SuperBuilder

src/main/java/clap/server/exception/code/CommentErrorCode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
@AllArgsConstructor
99
public enum CommentErrorCode implements BaseErrorCode {
1010

11-
COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "COMMENT_001", "댓글을 찾을 수 없습니다.");
11+
COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "COMMENT_001", "댓글을 찾을 수 없습니다."),
12+
COMMENT_ATTACHMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "COMMENT_002", "댓글 첨부파일을 찾을 수 없습니다.")
13+
;
1214

1315

1416
private final HttpStatus httpStatus;

0 commit comments

Comments
 (0)