diff --git a/src/main/java/leets/weeth/domain/board/application/usecase/NoticeUsecaseImpl.java b/src/main/java/leets/weeth/domain/board/application/usecase/NoticeUsecaseImpl.java index f0987ac2c..1de01a884 100644 --- a/src/main/java/leets/weeth/domain/board/application/usecase/NoticeUsecaseImpl.java +++ b/src/main/java/leets/weeth/domain/board/application/usecase/NoticeUsecaseImpl.java @@ -143,7 +143,11 @@ private CommentDTO.Response mapToDtoWithChildren(Comment comment, Map mapToDtoWithChildren(child, commentMap)) .collect(Collectors.toList()); - return commentMapper.toCommentDto(comment, children); + List files = fileGetService.findAllByComment(comment.getId()).stream() + .map(fileMapper::toFileResponse) + .toList(); + + return commentMapper.toCommentDto(comment, children, files); } } diff --git a/src/main/java/leets/weeth/domain/board/application/usecase/PostUseCaseImpl.java b/src/main/java/leets/weeth/domain/board/application/usecase/PostUseCaseImpl.java index 55bf65738..bf6a2fea5 100644 --- a/src/main/java/leets/weeth/domain/board/application/usecase/PostUseCaseImpl.java +++ b/src/main/java/leets/weeth/domain/board/application/usecase/PostUseCaseImpl.java @@ -1,10 +1,5 @@ package leets.weeth.domain.board.application.usecase; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; import leets.weeth.domain.board.application.dto.PartPostDTO; import leets.weeth.domain.board.application.dto.PostDTO; import leets.weeth.domain.board.application.exception.CategoryAccessDeniedException; @@ -34,14 +29,16 @@ import leets.weeth.domain.user.domain.service.UserCardinalGetService; import leets.weeth.domain.user.domain.service.UserGetService; import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Slice; -import org.springframework.data.domain.SliceImpl; -import org.springframework.data.domain.Sort; +import org.springframework.data.domain.*; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + @Service @RequiredArgsConstructor public class PostUseCaseImpl implements PostUsecase { @@ -106,7 +103,6 @@ public PostDTO.Response findPost(Long postId) { .map(fileMapper::toFileResponse) .toList(); - return mapper.toPostDto(post, response, filterParentComments(post.getComments())); } @@ -232,7 +228,11 @@ private CommentDTO.Response mapToDtoWithChildren(Comment comment, Map mapToDtoWithChildren(child, commentMap)) .collect(Collectors.toList()); - return commentMapper.toCommentDto(comment, children); + List files = fileGetService.findAllByComment(comment.getId()).stream() + .map(fileMapper::toFileResponse) + .toList(); + + return commentMapper.toCommentDto(comment, children, files); } private void validatePageNumber(int pageNumber){ diff --git a/src/main/java/leets/weeth/domain/comment/application/dto/CommentDTO.java b/src/main/java/leets/weeth/domain/comment/application/dto/CommentDTO.java index 7524c71ee..639e377d1 100644 --- a/src/main/java/leets/weeth/domain/comment/application/dto/CommentDTO.java +++ b/src/main/java/leets/weeth/domain/comment/application/dto/CommentDTO.java @@ -1,6 +1,10 @@ package leets.weeth.domain.comment.application.dto; +import jakarta.validation.Valid; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import leets.weeth.domain.file.application.dto.request.FileSaveRequest; +import leets.weeth.domain.file.application.dto.response.FileResponse; import leets.weeth.domain.user.domain.entity.enums.Position; import leets.weeth.domain.user.domain.entity.enums.Role; import lombok.Builder; @@ -13,12 +17,14 @@ public class CommentDTO { @Builder public record Save( Long parentCommentId, - @NotBlank String content + @NotBlank String content, + @Valid List<@NotNull FileSaveRequest> files ){} @Builder public record Update( - @NotBlank String content + @NotBlank String content, + @Valid List<@NotNull FileSaveRequest> files ){} @Builder @@ -29,6 +35,7 @@ public record Response( Role role, String content, LocalDateTime time, //modifiedAt + List fileUrls, List children ){} diff --git a/src/main/java/leets/weeth/domain/comment/application/mapper/CommentMapper.java b/src/main/java/leets/weeth/domain/comment/application/mapper/CommentMapper.java index 6afc099ad..84941b7c7 100644 --- a/src/main/java/leets/weeth/domain/comment/application/mapper/CommentMapper.java +++ b/src/main/java/leets/weeth/domain/comment/application/mapper/CommentMapper.java @@ -4,6 +4,7 @@ import leets.weeth.domain.board.domain.entity.Post; import leets.weeth.domain.comment.application.dto.CommentDTO; import leets.weeth.domain.comment.domain.entity.Comment; +import leets.weeth.domain.file.application.dto.response.FileResponse; import leets.weeth.domain.user.domain.entity.User; import org.mapstruct.*; @@ -44,5 +45,5 @@ public interface CommentMapper { @Mapping(target = "role", source = "comment.user.role") @Mapping(target = "time", source = "comment.modifiedAt") @Mapping(target = "children", source = "children") - CommentDTO.Response toCommentDto(Comment comment, List children); + CommentDTO.Response toCommentDto(Comment comment, List children, List fileUrls); } diff --git a/src/main/java/leets/weeth/domain/comment/application/usecase/NoticeCommentUsecaseImpl.java b/src/main/java/leets/weeth/domain/comment/application/usecase/NoticeCommentUsecaseImpl.java index 7cd8fde1e..43c5f4c61 100644 --- a/src/main/java/leets/weeth/domain/comment/application/usecase/NoticeCommentUsecaseImpl.java +++ b/src/main/java/leets/weeth/domain/comment/application/usecase/NoticeCommentUsecaseImpl.java @@ -3,15 +3,20 @@ import leets.weeth.domain.board.domain.entity.Notice; import leets.weeth.domain.board.domain.service.NoticeFindService; import leets.weeth.domain.comment.application.dto.CommentDTO; +import leets.weeth.domain.comment.application.exception.CommentNotFoundException; import leets.weeth.domain.comment.application.mapper.CommentMapper; import leets.weeth.domain.comment.domain.entity.Comment; import leets.weeth.domain.comment.domain.service.CommentDeleteService; import leets.weeth.domain.comment.domain.service.CommentFindService; import leets.weeth.domain.comment.domain.service.CommentSaveService; +import leets.weeth.domain.file.application.mapper.FileMapper; +import leets.weeth.domain.file.domain.entity.File; +import leets.weeth.domain.file.domain.service.FileDeleteService; +import leets.weeth.domain.file.domain.service.FileGetService; +import leets.weeth.domain.file.domain.service.FileSaveService; +import leets.weeth.domain.user.application.exception.UserNotMatchException; import leets.weeth.domain.user.domain.entity.User; import leets.weeth.domain.user.domain.service.UserGetService; -import leets.weeth.domain.comment.application.exception.CommentNotFoundException; -import leets.weeth.domain.user.application.exception.UserNotMatchException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -26,6 +31,11 @@ public class NoticeCommentUsecaseImpl implements NoticeCommentUsecase { private final CommentFindService commentFindService; private final CommentDeleteService commentDeleteService; + private final FileSaveService fileSaveService; + private final FileGetService fileGetService; + private final FileDeleteService fileDeleteService; + private final FileMapper fileMapper; + private final NoticeFindService noticeFindService; private final UserGetService userGetService; @@ -44,6 +54,9 @@ public void saveNoticeComment(CommentDTO.Save dto, Long noticeId, Long userId) { Comment comment = commentMapper.fromCommentDto(dto, notice, user, parentComment); commentSaveService.save(comment); + List files = fileMapper.toFileList(dto.files(), comment); + fileSaveService.save(files); + // 부모 댓글이 없다면 새 댓글로 추가 if(parentComment == null) { notice.addComment(comment); @@ -61,6 +74,12 @@ public void updateNoticeComment(CommentDTO.Update dto, Long noticeId, Long comme Notice notice = noticeFindService.find(noticeId); Comment comment = validateOwner(commentId, userId); + List fileList = getFiles(commentId); + fileDeleteService.delete(fileList); + + List files = fileMapper.toFileList(dto.files(), comment); + fileSaveService.save(files); + comment.update(dto); } @@ -109,4 +128,7 @@ private Comment validateOwner(Long commentId, Long userId) throws UserNotMatchEx return comment; } + private List getFiles(Long commentId) { + return fileGetService.findAllByComment(commentId); + } } diff --git a/src/main/java/leets/weeth/domain/comment/application/usecase/PostCommentUsecaseImpl.java b/src/main/java/leets/weeth/domain/comment/application/usecase/PostCommentUsecaseImpl.java index 314987a29..c2b655499 100644 --- a/src/main/java/leets/weeth/domain/comment/application/usecase/PostCommentUsecaseImpl.java +++ b/src/main/java/leets/weeth/domain/comment/application/usecase/PostCommentUsecaseImpl.java @@ -3,15 +3,20 @@ import leets.weeth.domain.board.domain.entity.Post; import leets.weeth.domain.board.domain.service.PostFindService; import leets.weeth.domain.comment.application.dto.CommentDTO; +import leets.weeth.domain.comment.application.exception.CommentNotFoundException; import leets.weeth.domain.comment.application.mapper.CommentMapper; import leets.weeth.domain.comment.domain.entity.Comment; import leets.weeth.domain.comment.domain.service.CommentDeleteService; import leets.weeth.domain.comment.domain.service.CommentFindService; import leets.weeth.domain.comment.domain.service.CommentSaveService; +import leets.weeth.domain.file.application.mapper.FileMapper; +import leets.weeth.domain.file.domain.entity.File; +import leets.weeth.domain.file.domain.service.FileDeleteService; +import leets.weeth.domain.file.domain.service.FileGetService; +import leets.weeth.domain.file.domain.service.FileSaveService; +import leets.weeth.domain.user.application.exception.UserNotMatchException; import leets.weeth.domain.user.domain.entity.User; import leets.weeth.domain.user.domain.service.UserGetService; -import leets.weeth.domain.comment.application.exception.CommentNotFoundException; -import leets.weeth.domain.user.application.exception.UserNotMatchException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -26,6 +31,11 @@ public class PostCommentUsecaseImpl implements PostCommentUsecase { private final CommentFindService commentFindService; private final CommentDeleteService commentDeleteService; + private final FileSaveService fileSaveService; + private final FileGetService fileGetService; + private final FileDeleteService fileDeleteService; + private final FileMapper fileMapper; + private final UserGetService userGetService; private final PostFindService postFindService; @@ -45,6 +55,9 @@ public void savePostComment(CommentDTO.Save dto, Long postId, Long userId) { Comment comment = commentMapper.fromCommentDto(dto, post, user, parentComment); commentSaveService.save(comment); + List files = fileMapper.toFileList(dto.files(), comment); + fileSaveService.save(files); + // 부모 댓글이 없다면 새 댓글로 추가 if (parentComment == null) { post.addComment(comment); @@ -62,6 +75,12 @@ public void updatePostComment(CommentDTO.Update dto, Long postId, Long commentId Post post = postFindService.find(postId); Comment comment = validateOwner(commentId, userId); + List fileList = getFiles(commentId); + fileDeleteService.delete(fileList); + + List files = fileMapper.toFileList(dto.files(), comment); + fileSaveService.save(files); + comment.update(dto); } @@ -120,4 +139,8 @@ private Comment validateOwner(Long commentId, Long userId) throws UserNotMatchEx return comment; } + private List getFiles(Long commentId) { + return fileGetService.findAllByComment(commentId); + } + } diff --git a/src/main/java/leets/weeth/domain/file/application/mapper/FileMapper.java b/src/main/java/leets/weeth/domain/file/application/mapper/FileMapper.java index 13fd228a4..3158efb75 100644 --- a/src/main/java/leets/weeth/domain/file/application/mapper/FileMapper.java +++ b/src/main/java/leets/weeth/domain/file/application/mapper/FileMapper.java @@ -4,6 +4,7 @@ import leets.weeth.domain.board.domain.entity.Notice; import leets.weeth.domain.board.domain.entity.Post; import leets.weeth.domain.comment.application.mapper.CommentMapper; +import leets.weeth.domain.comment.domain.entity.Comment; import leets.weeth.domain.file.application.dto.request.FileSaveRequest; import leets.weeth.domain.file.application.dto.response.FileResponse; import leets.weeth.domain.file.application.dto.response.UrlResponse; @@ -15,6 +16,7 @@ import java.util.Collections; import java.util.List; +import java.util.function.Function; import java.util.stream.Collectors; @Mapper(componentModel = MappingConstants.ComponentModel.SPRING, uses = CommentMapper.class, unmappedTargetPolicy = ReportingPolicy.IGNORE) @@ -32,39 +34,39 @@ public interface FileMapper { @Mapping(target = "receipt", source = "receipt") File toFileWithReceipt(String fileName, String fileUrl, Receipt receipt); + @Mapping(target = "id", ignore = true) + @Mapping(target = "comment", source = "comment") + @Mapping(target = "notice", ignore = true) // notice 필드는 매핑하지 않도록 명시 + @Mapping(target = "post", ignore = true) // post 필드는 매핑하지 않도록 명시 + File toFileWithComment(String fileName, String fileUrl, Comment comment); + @Mapping(target = "fileId", source = "file.id") FileResponse toFileResponse(File file); UrlResponse toUrlResponse(String fileName, String putUrl); - default List toFileList(List requests, Post post) { - List dto = requests; - if (dto == null || dto.isEmpty()) { + private List mapRequestsToFiles(List requests, Function mapper) { + if (requests == null || requests.isEmpty()) { return Collections.emptyList(); } - - return dto.stream() - .map(request -> toFileWithPost(request.fileName(), request.fileUrl(), post)) + return requests.stream() + .map(mapper) .collect(Collectors.toList()); } - default List toFileList(List requests, Notice notice) { - if (requests == null || requests.isEmpty()) { - return Collections.emptyList(); - } + default List toFileList(List requests, Post post) { + return mapRequestsToFiles(requests, request -> toFileWithPost(request.fileName(), request.fileUrl(), post)); + } - return requests.stream() - .map(request -> toFileWithNotice(request.fileName(), request.fileUrl(), notice)) - .collect(Collectors.toList()); + default List toFileList(List requests, Notice notice) { + return mapRequestsToFiles(requests, request -> toFileWithNotice(request.fileName(), request.fileUrl(), notice)); } default List toFileList(List requests, Receipt receipt) { - if (requests == null || requests.isEmpty()) { - return Collections.emptyList(); - } + return mapRequestsToFiles(requests, request -> toFileWithReceipt(request.fileName(), request.fileUrl(), receipt)); + } - return requests.stream() - .map(request -> toFileWithReceipt(request.fileName(), request.fileUrl(), receipt)) - .collect(Collectors.toList()); + default List toFileList(List requests, Comment comment) { + return mapRequestsToFiles(requests, request -> toFileWithComment(request.fileName(), request.fileUrl(), comment)); } } diff --git a/src/main/java/leets/weeth/domain/file/domain/entity/File.java b/src/main/java/leets/weeth/domain/file/domain/entity/File.java index 059a5dd35..490921864 100644 --- a/src/main/java/leets/weeth/domain/file/domain/entity/File.java +++ b/src/main/java/leets/weeth/domain/file/domain/entity/File.java @@ -4,8 +4,12 @@ import leets.weeth.domain.account.domain.entity.Receipt; import leets.weeth.domain.board.domain.entity.Notice; import leets.weeth.domain.board.domain.entity.Post; +import leets.weeth.domain.comment.domain.entity.Comment; import leets.weeth.global.common.entity.BaseEntity; -import lombok.*; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; @Getter @@ -35,6 +39,10 @@ public class File extends BaseEntity { @JoinColumn(name = "receipt_id") private Receipt receipt; + @ManyToOne + @JoinColumn(name = "comment_id") + private Comment comment; + public void update(String fileName, String fileUrl) { this.fileName = fileName; this.fileUrl = fileUrl; diff --git a/src/main/java/leets/weeth/domain/file/domain/repository/FileRepository.java b/src/main/java/leets/weeth/domain/file/domain/repository/FileRepository.java index 2daee1019..a9097038a 100644 --- a/src/main/java/leets/weeth/domain/file/domain/repository/FileRepository.java +++ b/src/main/java/leets/weeth/domain/file/domain/repository/FileRepository.java @@ -12,4 +12,6 @@ public interface FileRepository extends JpaRepository { List findAllByNoticeId(Long noticeId); List findAllByReceiptId(Long receiptId); + + List findAllByCommentId(Long commentId); } diff --git a/src/main/java/leets/weeth/domain/file/domain/service/FileGetService.java b/src/main/java/leets/weeth/domain/file/domain/service/FileGetService.java index a2f546e74..eb7212f93 100644 --- a/src/main/java/leets/weeth/domain/file/domain/service/FileGetService.java +++ b/src/main/java/leets/weeth/domain/file/domain/service/FileGetService.java @@ -1,6 +1,5 @@ package leets.weeth.domain.file.domain.service; -import leets.weeth.domain.board.domain.entity.Notice; import leets.weeth.domain.file.domain.entity.File; import leets.weeth.domain.file.domain.repository.FileRepository; import lombok.RequiredArgsConstructor; @@ -26,4 +25,7 @@ public List findAllByReceipt(Long receiptId) { return fileRepository.findAllByReceiptId(receiptId); } + public List findAllByComment(Long commentId) { + return fileRepository.findAllByCommentId(commentId); + } }