-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCommandCommentService.java
More file actions
77 lines (66 loc) · 3.22 KB
/
CommandCommentService.java
File metadata and controls
77 lines (66 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package clap.server.application.service.history;
import clap.server.adapter.inbound.web.dto.history.request.EditCommentRequest;
import clap.server.application.port.inbound.domain.CommentService;
import clap.server.application.port.inbound.history.DeleteCommentUsecase;
import clap.server.application.port.inbound.history.EditCommentUsecase;
import clap.server.application.port.inbound.domain.MemberService;
import clap.server.application.port.outbound.task.CommandAttachmentPort;
import clap.server.application.port.outbound.task.CommandCommentPort;
import clap.server.application.port.outbound.task.LoadAttachmentPort;
import clap.server.application.port.outbound.task.LoadCommentPort;
import clap.server.application.port.outbound.taskhistory.CommandTaskHistoryPort;
import clap.server.common.annotation.architecture.ApplicationService;
import clap.server.domain.model.member.Member;
import clap.server.domain.model.task.Attachment;
import clap.server.domain.model.task.Comment;
import clap.server.exception.ApplicationException;
import clap.server.exception.DomainException;
import clap.server.exception.code.CommentErrorCode;
import clap.server.exception.code.MemberErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import java.util.Objects;
@ApplicationService
@RequiredArgsConstructor
@Slf4j
public class CommandCommentService implements EditCommentUsecase, DeleteCommentUsecase {
private final MemberService memberService;
private final CommentService commentService;
private final CommandCommentPort commandCommentPort;
private final LoadAttachmentPort loadAttachmentPort;
private final CommandAttachmentPort commandAttachmentPort;
private final CommandTaskHistoryPort commandTaskHistoryPort;
@Transactional
@Override
public void editComment(Long userId, Long commentId, EditCommentRequest request) {
Member member = memberService.findActiveMember(userId);
Comment comment = commentService.findById(commentId);
if (Member.checkCommenter(comment.getTask(), member)) {
comment.updateComment(request.content());
commandCommentPort.saveComment(comment);
};
}
@Transactional
@Override
public void deleteComment(Long userId, Long commentId) {
Member member = memberService.findActiveMember(userId);
Comment comment = commentService.findById(commentId);
if (Member.checkCommenter(comment.getTask(), member)) {
// 첨부파일이 있을 경우 삭제
if (loadAttachmentPort.exitsByCommentId(commentId)) {
deleteAttachments(commentId);
}
// comment 삭제
commandCommentPort.deleteComment(comment);
// comment와 관련된 taskHistory도 함께 삭제
commandTaskHistoryPort.deleteTaskHistoryByCommentId(commentId);
}
}
private void deleteAttachments(Long commentId) {
Attachment attachment = loadAttachmentPort.findByCommentId(commentId)
.orElseThrow(() -> new ApplicationException(CommentErrorCode.COMMENT_ATTACHMENT_NOT_FOUND));
attachment.softDelete();
commandAttachmentPort.save(attachment);
}
}