Skip to content

Commit 49411fc

Browse files
authored
Merge pull request #187 from TaskFlow-CLAP/CLAP-185
ClAP-185 Comment logic 리팩토링 작업 진행
2 parents 9c4a552 + a2514bd commit 49411fc

File tree

12 files changed

+59
-99
lines changed

12 files changed

+59
-99
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/entity/task/CommentEntity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import lombok.Getter;
88
import lombok.NoArgsConstructor;
99
import lombok.experimental.SuperBuilder;
10+
import org.hibernate.annotations.SQLDelete;
1011

1112
@Entity
1213
@Table(name = "comment")
1314
@Getter
1415
@SuperBuilder
1516
@NoArgsConstructor(access = AccessLevel.PROTECTED)
17+
@SQLDelete(sql = "UPDATE Comment SET is_Deleted = true WHERE comment_id = ?")
1618
public class CommentEntity extends BaseTimeEntity {
1719
@Id
1820
@GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -34,5 +36,5 @@ public class CommentEntity extends BaseTimeEntity {
3436
private boolean isModified;
3537

3638
@Column(name="is_deleted", nullable = false)
37-
private boolean isDeleted;
39+
private boolean isDeleted = Boolean.FALSE;
3840
}

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/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: 11 additions & 46 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 {
@@ -42,7 +35,7 @@ public void updateComment(Long userId, Long commentId, PostAndEditCommentRequest
4235
Comment comment = loadCommentPort.findById(commentId)
4336
.orElseThrow(() -> new ApplicationException(CommentErrorCode.COMMENT_NOT_FOUND));
4437

45-
if (checkCommenter(comment.getTask(), member)) {
38+
if (Member.checkCommenter(comment.getTask(), member)) {
4639

4740
comment.updateComment(request.content());
4841
commandCommentPort.saveComment(comment);
@@ -51,53 +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

61-
if (checkCommenter(comment.getTask(), member)) {
62-
63-
// 삭제할 댓글이 첨부파일일 경우
64-
if (!request.attachmentsToDelete().isEmpty()) {
65-
deleteAttachments(request.attachmentsToDelete(), comment.getTask(), comment.getCommentId());
54+
if (Member.checkCommenter(comment.getTask(), member)) {
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;
85-
}
86-
87-
public Boolean checkCommenter(Task task, Member member) {
88-
// 일반 회원일 경우 => 요청자인지 확인
89-
// 담당자일 경우 => 처리자인지 확인
90-
if ((member.getMemberInfo().getRole() == MemberRole.ROLE_MANAGER)
91-
&& !(member.getMemberId() == task.getProcessor().getMemberId())) {
92-
throw new ApplicationException(MemberErrorCode.NOT_A_COMMENTER);
93-
}
94-
95-
else if ((member.getMemberInfo().getRole() == MemberRole.ROLE_USER)
96-
&& !(member.getMemberId() == task.getRequester().getMemberId())) {
97-
throw new ApplicationException(MemberErrorCode.NOT_A_COMMENTER);
98-
}
99-
else {
100-
return true;
101-
}
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);
10267
}
10368
}

src/main/java/clap/server/application/service/comment/PostCommentService.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import clap.server.adapter.inbound.web.dto.task.PostAndEditCommentRequest;
44
import clap.server.adapter.outbound.infrastructure.s3.S3UploadAdapter;
5-
import clap.server.adapter.outbound.persistense.entity.member.constant.MemberRole;
65
import clap.server.adapter.outbound.persistense.entity.task.constant.TaskHistoryType;
76
import clap.server.application.mapper.AttachmentMapper;
87
import clap.server.application.port.inbound.comment.PostCommentUsecase;
@@ -18,8 +17,6 @@
1817
import clap.server.domain.model.task.Comment;
1918
import clap.server.domain.model.task.Task;
2019
import clap.server.domain.model.task.TaskHistory;
21-
import clap.server.exception.ApplicationException;
22-
import clap.server.exception.code.MemberErrorCode;
2320
import lombok.RequiredArgsConstructor;
2421
import org.springframework.transaction.annotation.Transactional;
2522
import org.springframework.web.multipart.MultipartFile;
@@ -45,7 +42,7 @@ public void save(Long userId, Long taskId, PostAndEditCommentRequest request) {
4542

4643
// 일반 회원일 경우 => 요청자인지 확인
4744
// 담당자일 경우 => 처리자인지 확인
48-
if (checkCommenter(task, member)) {
45+
if (Member.checkCommenter(task, member)) {
4946
Comment comment = Comment.createComment(member, task, request.content());
5047
Comment savedComment = commandCommentPort.saveComment(comment);
5148

@@ -60,7 +57,7 @@ public void saveCommentAttachment(Long userId, Long taskId, List<MultipartFile>
6057
Task task = taskService.findById(taskId);
6158
Member member = memberService.findActiveMember(userId);
6259

63-
if (checkCommenter(task, member)) {
60+
if (Member.checkCommenter(task, member)) {
6461
Comment comment = Comment.createComment(member, task, "Attachment");
6562
Comment savedComment = commandCommentPort.saveComment(comment);
6663
saveAttachment(files, task, savedComment);
@@ -75,22 +72,4 @@ private void saveAttachment(List<MultipartFile> files, Task task, Comment commen
7572
List<Attachment> attachments = AttachmentMapper.toCommentAttachments(task, comment, files, fileUrls);
7673
commandAttachmentPort.saveAll(attachments);
7774
}
78-
79-
public Boolean checkCommenter(Task task, Member member) {
80-
// 일반 회원일 경우 => 요청자인지 확인
81-
// 담당자일 경우 => 처리자인지 확인
82-
if ((member.getMemberInfo().getRole() == MemberRole.ROLE_MANAGER)
83-
&& !(member.getMemberId() == task.getProcessor().getMemberId())) {
84-
throw new ApplicationException(MemberErrorCode.NOT_A_COMMENTER);
85-
}
86-
87-
else if ((member.getMemberInfo().getRole() == MemberRole.ROLE_USER)
88-
&& !(member.getMemberId() == task.getRequester().getMemberId())) {
89-
throw new ApplicationException(MemberErrorCode.NOT_A_COMMENTER);
90-
}
91-
else {
92-
return true;
93-
}
94-
95-
}
9675
}

src/main/java/clap/server/domain/model/member/Member.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package clap.server.domain.model.member;
22

3+
import clap.server.adapter.outbound.persistense.entity.member.constant.MemberRole;
34
import clap.server.adapter.outbound.persistense.entity.member.constant.MemberStatus;
45
import clap.server.domain.model.common.BaseTime;
6+
import clap.server.domain.model.task.Task;
7+
import clap.server.exception.ApplicationException;
8+
import clap.server.exception.code.MemberErrorCode;
59
import lombok.*;
610
import lombok.experimental.SuperBuilder;
711

@@ -78,4 +82,22 @@ public void updateMemberInfo(String name, Boolean agitNotificationEnabled, Boole
7882
public void setStatusDeleted() {
7983
this.status = MemberStatus.DELETED;
8084
}
85+
86+
public static Boolean checkCommenter(Task task, Member member) {
87+
// 일반 회원일 경우 => 요청자인지 확인
88+
// 담당자일 경우 => 처리자인지 확인
89+
if ((member.getMemberInfo().getRole() == MemberRole.ROLE_MANAGER)
90+
&& !(member.getMemberId() == task.getProcessor().getMemberId())) {
91+
throw new ApplicationException(MemberErrorCode.NOT_A_COMMENTER);
92+
}
93+
94+
else if ((member.getMemberInfo().getRole() == MemberRole.ROLE_USER)
95+
&& !(member.getMemberId() == task.getRequester().getMemberId())) {
96+
throw new ApplicationException(MemberErrorCode.NOT_A_COMMENTER);
97+
}
98+
else {
99+
return true;
100+
}
101+
102+
}
81103
}

0 commit comments

Comments
 (0)