-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCommandCommentService.java
More file actions
68 lines (55 loc) · 2.75 KB
/
CommandCommentService.java
File metadata and controls
68 lines (55 loc) · 2.75 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
package clap.server.application.service.comment;
import clap.server.adapter.inbound.web.dto.task.PostAndEditCommentRequest;
import clap.server.application.port.inbound.comment.CommandCommentUsecase;
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.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.code.CommentErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
@ApplicationService
@RequiredArgsConstructor
public class CommandCommentService implements CommandCommentUsecase {
private final MemberService memberService;
private final LoadCommentPort loadCommentPort;
private final CommandCommentPort commandCommentPort;
private final LoadAttachmentPort loadAttachmentPort;
private final CommandAttachmentPort commandAttachmentPort;
@Transactional
@Override
public void updateComment(Long userId, Long commentId, PostAndEditCommentRequest request) {
Member member = memberService.findActiveMember(userId);
Comment comment = loadCommentPort.findById(commentId)
.orElseThrow(() -> new ApplicationException(CommentErrorCode.COMMENT_NOT_FOUND));
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 = loadCommentPort.findById(commentId)
.orElseThrow(() -> new ApplicationException(CommentErrorCode.COMMENT_NOT_FOUND));
if (Member.checkCommenter(comment.getTask(), member)) {
if (loadAttachmentPort.exitsByCommentId(commentId)) {
deleteAttachments(commentId);
}
commandCommentPort.deleteComment(comment);
};
}
private void deleteAttachments(Long commentId) {
Attachment attachment = loadAttachmentPort.findByCommentId(commentId)
.orElseThrow(() -> new ApplicationException(CommentErrorCode.COMMENT_ATTACHMENT_NOT_FOUND));
attachment.softDelete();
commandAttachmentPort.save(attachment);
}
}