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
Expand Up @@ -143,7 +143,11 @@ private CommentDTO.Response mapToDtoWithChildren(Comment comment, Map<Long, List
.map(child -> mapToDtoWithChildren(child, commentMap))
.collect(Collectors.toList());

return commentMapper.toCommentDto(comment, children);
List<FileResponse> files = fileGetService.findAllByComment(comment.getId()).stream()
.map(fileMapper::toFileResponse)
.toList();

return commentMapper.toCommentDto(comment, children, files);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -106,7 +103,6 @@ public PostDTO.Response findPost(Long postId) {
.map(fileMapper::toFileResponse)
.toList();


return mapper.toPostDto(post, response, filterParentComments(post.getComments()));
}

Expand Down Expand Up @@ -232,7 +228,11 @@ private CommentDTO.Response mapToDtoWithChildren(Comment comment, Map<Long, List
.map(child -> mapToDtoWithChildren(child, commentMap))
.collect(Collectors.toList());

return commentMapper.toCommentDto(comment, children);
List<FileResponse> files = fileGetService.findAllByComment(comment.getId()).stream()
.map(fileMapper::toFileResponse)
.toList();

return commentMapper.toCommentDto(comment, children, files);
}

private void validatePageNumber(int pageNumber){
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -29,6 +35,7 @@ public record Response(
Role role,
String content,
LocalDateTime time, //modifiedAt
List<FileResponse> fileUrls,
List<CommentDTO.Response> children
){}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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<CommentDTO.Response> children);
CommentDTO.Response toCommentDto(Comment comment, List<CommentDTO.Response> children, List<FileResponse> fileUrls);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<File> files = fileMapper.toFileList(dto.files(), comment);
fileSaveService.save(files);

// 부모 댓글이 없다면 새 댓글로 추가
if(parentComment == null) {
notice.addComment(comment);
Expand All @@ -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<File> fileList = getFiles(commentId);
fileDeleteService.delete(fileList);

List<File> files = fileMapper.toFileList(dto.files(), comment);
fileSaveService.save(files);

comment.update(dto);
}

Expand Down Expand Up @@ -109,4 +128,7 @@ private Comment validateOwner(Long commentId, Long userId) throws UserNotMatchEx
return comment;
}

private List<File> getFiles(Long commentId) {
return fileGetService.findAllByComment(commentId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<File> files = fileMapper.toFileList(dto.files(), comment);
fileSaveService.save(files);

// 부모 댓글이 없다면 새 댓글로 추가
if (parentComment == null) {
post.addComment(comment);
Expand All @@ -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<File> fileList = getFiles(commentId);
fileDeleteService.delete(fileList);

List<File> files = fileMapper.toFileList(dto.files(), comment);
fileSaveService.save(files);
Comment on lines +79 to +82
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

파일을 update 하지 않고 삭제한 뒤 새로 저장한 이유가 궁금합니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정하는 게시글 파일 수가 달라지는 경우는 update로 대응할 수 없어서 깔끔하게 지우고 다시 저장하는 방향으로 설정 했습니당


comment.update(dto);
}

Expand Down Expand Up @@ -120,4 +139,8 @@ private Comment validateOwner(Long commentId, Long userId) throws UserNotMatchEx
return comment;
}

private List<File> getFiles(Long commentId) {
return fileGetService.findAllByComment(commentId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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<File> toFileList(List<FileSaveRequest> requests, Post post) {
List<FileSaveRequest> dto = requests;
if (dto == null || dto.isEmpty()) {
private List<File> mapRequestsToFiles(List<FileSaveRequest> requests, Function<FileSaveRequest, File> 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<File> toFileList(List<FileSaveRequest> requests, Notice notice) {
if (requests == null || requests.isEmpty()) {
return Collections.emptyList();
}
default List<File> toFileList(List<FileSaveRequest> 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<File> toFileList(List<FileSaveRequest> requests, Notice notice) {
return mapRequestsToFiles(requests, request -> toFileWithNotice(request.fileName(), request.fileUrl(), notice));
}

default List<File> toFileList(List<FileSaveRequest> 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<File> toFileList(List<FileSaveRequest> requests, Comment comment) {
return mapRequestsToFiles(requests, request -> toFileWithComment(request.fileName(), request.fileUrl(), comment));
}
}
10 changes: 9 additions & 1 deletion src/main/java/leets/weeth/domain/file/domain/entity/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface FileRepository extends JpaRepository<File, Long> {
List<File> findAllByNoticeId(Long noticeId);

List<File> findAllByReceiptId(Long receiptId);

List<File> findAllByCommentId(Long commentId);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,4 +25,7 @@ public List<File> findAllByReceipt(Long receiptId) {
return fileRepository.findAllByReceiptId(receiptId);
}

public List<File> findAllByComment(Long commentId) {
return fileRepository.findAllByCommentId(commentId);
}
}