Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package clap.server.adapter.inbound.web.comment;

import clap.server.adapter.inbound.security.SecurityUserDetails;
import clap.server.adapter.inbound.web.dto.task.DeleteCommentRequest;
import clap.server.adapter.inbound.web.dto.task.PostAndEditCommentRequest;
import clap.server.application.port.inbound.comment.CommandCommentUsecase;
import clap.server.common.annotation.architecture.WebAdapter;
Expand Down Expand Up @@ -40,9 +39,8 @@ public void editComment(
@Secured({"ROLE_MANAGER", "ROLE_USER"})
public void deleteComment(
@AuthenticationPrincipal SecurityUserDetails userInfo,
@PathVariable Long commentId,
@RequestBody DeleteCommentRequest request) {
commandCommentUsecase.deleteComment(userInfo.getUserId(), commentId, request);
@PathVariable Long commentId) {
commandCommentUsecase.deleteComment(userInfo.getUserId(), commentId);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ public List<Attachment> findAllByTaskIdAndCommentIsNullAndAttachmentId(final Lon
}

@Override
public List<Attachment> findAllyByTaskIdAndCommentIdAndAttachmentId(Long taskId, Long commentId, List<Long> attachmentIds) {
List<AttachmentEntity> attachmentEntities = attachmentRepository.findActiveAttachmentsByTask_TaskIdAndComment_CommentIdAndAttachmentIdIn(taskId, commentId, attachmentIds);
return attachmentEntities.stream()
.map(attachmentPersistenceMapper::toDomain)
.collect(Collectors.toList());
public Optional<Attachment> findByCommentId(Long commentId) {
Optional<AttachmentEntity> attachmentEntity = attachmentRepository.findByComment_CommentIdAndIsDeletedFalse(commentId);
return attachmentEntity.map(attachmentPersistenceMapper::toDomain);
}

@Override
Expand All @@ -68,4 +66,9 @@ public List<Attachment> findAllByTaskIdAndCommentIsNotNull(final Long taskId) {
.map(attachmentPersistenceMapper::toDomain)
.collect(Collectors.toList());
}

@Override
public boolean exitsByCommentId(Long commentId) {
return attachmentRepository.existsByComment_CommentId(commentId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public Comment saveComment(Comment comment) {
}

@Override
public Comment saveComment(Comment comment) {
CommentEntity commentEntity = commentRepository.save(commentPersistenceMapper.toEntity(comment));
return commentPersistenceMapper.toDomain(commentEntity);
public void deleteComment(Comment comment) {
commentRepository.delete(commentPersistenceMapper.toEntity(comment));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.SQLDelete;

@Entity
@Table(name = "comment")
@Getter
@SuperBuilder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@SQLDelete(sql = "UPDATE Comment SET is_Deleted = true WHERE comment_id = ?")
public class CommentEntity extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -34,5 +36,5 @@ public class CommentEntity extends BaseTimeEntity {
private boolean isModified;

@Column(name="is_deleted", nullable = false)
private boolean isDeleted;
private boolean isDeleted = Boolean.FALSE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface AttachmentRepository extends JpaRepository<AttachmentEntity, Long> {
List<AttachmentEntity> findAllByTask_TaskIdAndCommentIsNullAndIsDeletedIsFalse(Long taskId);
List<AttachmentEntity> findAllByTask_TaskIdAndCommentIsNullAndIsDeletedIsFalseAndAttachmentIdIn(Long task_taskId, List<Long> attachmentId);
List<AttachmentEntity> findActiveAttachmentsByTask_TaskIdAndComment_CommentIdAndAttachmentIdIn(Long task_taskId, Long comment_commentId, List<Long> attachmentIds);
Optional<AttachmentEntity> findByComment_CommentIdAndIsDeletedFalse(Long commentId);
boolean existsByComment_CommentId(Long commentId);
List<AttachmentEntity> findAllByTask_TaskIdAndCommentIsNotNullAndIsDeletedIsFalse(Long taskId);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package clap.server.application.port.inbound.comment;

import clap.server.adapter.inbound.web.dto.task.DeleteCommentRequest;
import clap.server.adapter.inbound.web.dto.task.PostAndEditCommentRequest;

import java.util.List;

public interface CommandCommentUsecase {

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

void deleteComment(Long userId, Long commentId, DeleteCommentRequest request);
void deleteComment(Long userId, Long commentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
public interface CommandCommentPort {

Comment saveComment(Comment comment);

void deleteComment(Comment comment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import clap.server.domain.model.task.Attachment;

import java.util.List;
import java.util.Optional;


public interface LoadAttachmentPort {
List<Attachment> findAllByTaskIdAndCommentIsNull(Long taskId);
List<Attachment> findAllByTaskIdAndCommentIsNullAndAttachmentId(Long taskId, List<Long> attachmentIds);
List<Attachment> findAllyByTaskIdAndCommentIdAndAttachmentId(Long taskId, Long commentId, List<Long> attachmentIds);
Optional<Attachment> findByCommentId(Long commentId);
List<Attachment> findAllByTaskIdAndCommentIsNotNull(Long taskId);
boolean exitsByCommentId(Long commentId);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package clap.server.application.service.comment;

import clap.server.adapter.inbound.web.dto.task.DeleteCommentRequest;
import clap.server.adapter.inbound.web.dto.task.PostAndEditCommentRequest;
import clap.server.adapter.outbound.persistense.entity.member.constant.MemberRole;
import clap.server.application.port.inbound.comment.CommandCommentUsecase;
import clap.server.application.port.inbound.domain.MemberService;
import clap.server.application.port.outbound.task.CommandAttachmentPort;
Expand All @@ -13,16 +11,11 @@
import clap.server.domain.model.member.Member;
import clap.server.domain.model.task.Attachment;
import clap.server.domain.model.task.Comment;
import clap.server.domain.model.task.Task;
import clap.server.exception.ApplicationException;
import clap.server.exception.code.CommentErrorCode;
import clap.server.exception.code.MemberErrorCode;
import clap.server.exception.code.TaskErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@ApplicationService
@RequiredArgsConstructor
public class CommandCommentService implements CommandCommentUsecase {
Expand All @@ -42,7 +35,7 @@ public void updateComment(Long userId, Long commentId, PostAndEditCommentRequest
Comment comment = loadCommentPort.findById(commentId)
.orElseThrow(() -> new ApplicationException(CommentErrorCode.COMMENT_NOT_FOUND));

if (checkCommenter(comment.getTask(), member)) {
if (Member.checkCommenter(comment.getTask(), member)) {

comment.updateComment(request.content());
commandCommentPort.saveComment(comment);
Expand All @@ -51,53 +44,25 @@ public void updateComment(Long userId, Long commentId, PostAndEditCommentRequest

@Transactional
@Override
public void deleteComment(Long userId, Long commentId, DeleteCommentRequest request) {
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 (checkCommenter(comment.getTask(), member)) {

// 삭제할 댓글이 첨부파일일 경우
if (!request.attachmentsToDelete().isEmpty()) {
deleteAttachments(request.attachmentsToDelete(), comment.getTask(), comment.getCommentId());
if (Member.checkCommenter(comment.getTask(), member)) {
if (loadAttachmentPort.exitsByCommentId(commentId)) {
deleteAttachments(commentId);
}

comment.softDelete();
commandCommentPort.saveComment(comment);
commandCommentPort.deleteComment(comment);
};
}

private void deleteAttachments(List<Long> attachmentIdsToDelete, Task task, Long commentId) {
List<Attachment> attachmentsToDelete = validateAndGetAttachments(attachmentIdsToDelete, task, commentId);
attachmentsToDelete.forEach(Attachment::softDelete);
commandAttachmentPort.saveAll(attachmentsToDelete);
}

private List<Attachment> validateAndGetAttachments(List<Long> attachmentIdsToDelete, Task task, Long commentId) {
List<Attachment> attachmentsOfTask = loadAttachmentPort.findAllyByTaskIdAndCommentIdAndAttachmentId(task.getTaskId(), commentId, attachmentIdsToDelete);
if (attachmentsOfTask.size() != attachmentIdsToDelete.size()) {
throw new ApplicationException(TaskErrorCode.TASK_ATTACHMENT_NOT_FOUND);
}
return attachmentsOfTask;
}

public Boolean checkCommenter(Task task, Member member) {
// 일반 회원일 경우 => 요청자인지 확인
// 담당자일 경우 => 처리자인지 확인
if ((member.getMemberInfo().getRole() == MemberRole.ROLE_MANAGER)
&& !(member.getMemberId() == task.getProcessor().getMemberId())) {
throw new ApplicationException(MemberErrorCode.NOT_A_COMMENTER);
}

else if ((member.getMemberInfo().getRole() == MemberRole.ROLE_USER)
&& !(member.getMemberId() == task.getRequester().getMemberId())) {
throw new ApplicationException(MemberErrorCode.NOT_A_COMMENTER);
}
else {
return true;
}
private void deleteAttachments(Long commentId) {
Attachment attachment = loadAttachmentPort.findByCommentId(commentId)
.orElseThrow(() -> new ApplicationException(CommentErrorCode.COMMENT_ATTACHMENT_NOT_FOUND));
attachment.softDelete();
commandAttachmentPort.save(attachment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import clap.server.adapter.inbound.web.dto.task.PostAndEditCommentRequest;
import clap.server.adapter.outbound.infrastructure.s3.S3UploadAdapter;
import clap.server.adapter.outbound.persistense.entity.member.constant.MemberRole;
import clap.server.adapter.outbound.persistense.entity.task.constant.TaskHistoryType;
import clap.server.application.mapper.AttachmentMapper;
import clap.server.application.port.inbound.comment.PostCommentUsecase;
Expand All @@ -18,8 +17,6 @@
import clap.server.domain.model.task.Comment;
import clap.server.domain.model.task.Task;
import clap.server.domain.model.task.TaskHistory;
import clap.server.exception.ApplicationException;
import clap.server.exception.code.MemberErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -45,7 +42,7 @@ public void save(Long userId, Long taskId, PostAndEditCommentRequest request) {

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

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

if (checkCommenter(task, member)) {
if (Member.checkCommenter(task, member)) {
Comment comment = Comment.createComment(member, task, "Attachment");
Comment savedComment = commandCommentPort.saveComment(comment);
saveAttachment(files, task, savedComment);
Expand All @@ -75,22 +72,4 @@ private void saveAttachment(List<MultipartFile> files, Task task, Comment commen
List<Attachment> attachments = AttachmentMapper.toCommentAttachments(task, comment, files, fileUrls);
commandAttachmentPort.saveAll(attachments);
}

public Boolean checkCommenter(Task task, Member member) {
// 일반 회원일 경우 => 요청자인지 확인
// 담당자일 경우 => 처리자인지 확인
if ((member.getMemberInfo().getRole() == MemberRole.ROLE_MANAGER)
&& !(member.getMemberId() == task.getProcessor().getMemberId())) {
throw new ApplicationException(MemberErrorCode.NOT_A_COMMENTER);
}

else if ((member.getMemberInfo().getRole() == MemberRole.ROLE_USER)
&& !(member.getMemberId() == task.getRequester().getMemberId())) {
throw new ApplicationException(MemberErrorCode.NOT_A_COMMENTER);
}
else {
return true;
}

}
}
22 changes: 22 additions & 0 deletions src/main/java/clap/server/domain/model/member/Member.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package clap.server.domain.model.member;

import clap.server.adapter.outbound.persistense.entity.member.constant.MemberRole;
import clap.server.adapter.outbound.persistense.entity.member.constant.MemberStatus;
import clap.server.domain.model.common.BaseTime;
import clap.server.domain.model.task.Task;
import clap.server.exception.ApplicationException;
import clap.server.exception.code.MemberErrorCode;
import lombok.*;
import lombok.experimental.SuperBuilder;

Expand Down Expand Up @@ -78,4 +82,22 @@ public void updateMemberInfo(String name, Boolean agitNotificationEnabled, Boole
public void setStatusDeleted() {
this.status = MemberStatus.DELETED;
}

public static Boolean checkCommenter(Task task, Member member) {
// 일반 회원일 경우 => 요청자인지 확인
// 담당자일 경우 => 처리자인지 확인
if ((member.getMemberInfo().getRole() == MemberRole.ROLE_MANAGER)
&& !(member.getMemberId() == task.getProcessor().getMemberId())) {
throw new ApplicationException(MemberErrorCode.NOT_A_COMMENTER);
}

else if ((member.getMemberInfo().getRole() == MemberRole.ROLE_USER)
&& !(member.getMemberId() == task.getRequester().getMemberId())) {
throw new ApplicationException(MemberErrorCode.NOT_A_COMMENTER);
}
else {
return true;
}

}
}
1 change: 1 addition & 0 deletions src/main/java/clap/server/domain/model/task/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.SQLDelete;

@Getter
@SuperBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
@AllArgsConstructor
public enum CommentErrorCode implements BaseErrorCode {

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


private final HttpStatus httpStatus;
Expand Down