Skip to content

Commit

Permalink
Merge pull request #111 from Leets-Official/refactor#105
Browse files Browse the repository at this point in the history
Refactor#111 ๊ธ€ ์กฐํšŒ์‹œ ํฌ์ง€์…˜๊ณผ ์—ญํ•  ๊ฐ™์ด ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ˆ˜์ •
  • Loading branch information
jj0526 authored Jan 18, 2025
2 parents a480acb + 8712a3e commit da91da2
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import leets.weeth.domain.comment.application.dto.CommentDTO;
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;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -32,6 +34,8 @@ public record Update(
public record Response(
Long id,
String name,
Position position,
Role role,
String title,
String content,
LocalDateTime time,//modifiedAt
Expand All @@ -45,6 +49,8 @@ public record Response(
public record ResponseAll(
Long id,
String name,
Position position,
Role role,
String title,
String content,
LocalDateTime time,//modifiedAt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import leets.weeth.domain.file.application.dto.request.FileSaveRequest;
import leets.weeth.domain.file.application.dto.request.FileUpdateRequest;
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;

import java.time.LocalDateTime;
Expand All @@ -31,6 +33,8 @@ public record Update(
public record Response(
Long id,
String name,
Position position,
Role role,
String title,
String content,
LocalDateTime time,//modifiedAt
Expand All @@ -43,6 +47,8 @@ public record Response(
public record ResponseAll(
Long id,
String name,
Position position,
Role role,
String title,
String content,
LocalDateTime time,//modifiedAt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,19 @@ public interface NoticeMapper {

@Mappings({
@Mapping(target = "name", source = "notice.user.name"),
@Mapping(target = "position", source = "notice.user.position"),
@Mapping(target = "role", source = "notice.user.role"),
@Mapping(target = "time", source = "notice.modifiedAt"),
@Mapping(target = "hasFile", expression = "java(fileExists)")
})
NoticeDTO.ResponseAll toAll(Notice notice, boolean fileExists);

@Mappings({
@Mapping(target = "name", source = "notice.user.name"),
@Mapping(target = "comments", expression = "java(filterParentComments(notice.getComments()))"),
@Mapping(target = "position", source = "notice.user.position"),
@Mapping(target = "role", source = "notice.user.role"),
@Mapping(target = "time", source = "notice.modifiedAt")
})
NoticeDTO.Response toNoticeDto(Notice notice, List<FileResponse> fileUrls);

default List<CommentDTO.Response> filterParentComments(List<Comment> comments) {
if (comments == null || comments.isEmpty()) {
return Collections.emptyList();
}

// ๋ถ€๋ชจ ๋Œ“๊ธ€๋งŒ ํ•„ํ„ฐ๋งํ•˜๊ณ , ๊ฐ ๋ถ€๋ชจ ๋Œ“๊ธ€์— ๋Œ€ํ•ด ์ž์‹ ๋Œ“๊ธ€์„ ๋งคํ•‘
return comments.stream()
.filter(comment -> comment.getParent() == null) // ๋ถ€๋ชจ ๋Œ“๊ธ€๋งŒ ํ•„ํ„ฐ๋ง
.map(this::mapCommentWithChildren) // ์ž์‹ ๋Œ“๊ธ€ ํฌํ•จํ•˜์—ฌ ๋งคํ•‘
.toList();
}

default CommentDTO.Response mapCommentWithChildren(Comment comment) {
if (comment == null) {
return null;
}

// ๊ธฐ๋ณธ ๋Œ“๊ธ€ ์ •๋ณด ๋งคํ•‘
CommentDTO.Response.ResponseBuilder response = CommentDTO.Response.builder();

response.name(comment.getUser().getName());
response.time(comment.getModifiedAt());
response.id(comment.getId());
response.content(comment.getContent());

// ์ž์‹ ๋Œ“๊ธ€๋“ค์„ ์žฌ๊ท€์ ์œผ๋กœ ๋งคํ•‘ํ•˜์—ฌ children ํ•„๋“œ์— ์„ค์ •
List<CommentDTO.Response> childrenResponses = comment.getChildren().stream()
.map(this::mapCommentWithChildren) // ์ž์‹ ๋Œ“๊ธ€๋„ ๋™์ผํ•˜๊ฒŒ ์ฒ˜๋ฆฌ
.collect(Collectors.toList());
response.children(childrenResponses);

return response.build();
}
NoticeDTO.Response toNoticeDto(Notice notice, List<FileResponse> fileUrls, List<CommentDTO.Response> comments);

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface PostMapper {

@Mappings({
@Mapping(target = "name", source = "post.user.name"),
@Mapping(target = "position", source = "post.user.position"),
@Mapping(target = "role", source = "post.user.role"),
@Mapping(target = "time", source = "post.modifiedAt"),
@Mapping(target = "hasFile", expression = "java(fileExists)")
})
Expand All @@ -33,43 +35,10 @@ public interface PostMapper {

@Mappings({
@Mapping(target = "name", source = "post.user.name"),
@Mapping(target = "comments", expression = "java(filterParentComments(post.getComments()))"),
@Mapping(target = "position", source = "post.user.position"),
@Mapping(target = "role", source = "post.user.role"),
@Mapping(target = "time", source = "post.modifiedAt")
})
PostDTO.Response toPostDto(Post post, List<FileResponse> fileUrls);

default List<CommentDTO.Response> filterParentComments(List<Comment> comments) {
if (comments == null || comments.isEmpty()) {
return Collections.emptyList();
}

// ๋ถ€๋ชจ ๋Œ“๊ธ€๋งŒ ํ•„ํ„ฐ๋งํ•˜๊ณ , ๊ฐ ๋ถ€๋ชจ ๋Œ“๊ธ€์— ๋Œ€ํ•ด ์ž์‹ ๋Œ“๊ธ€์„ ๋งคํ•‘
return comments.stream()
.filter(comment -> comment.getParent() == null) // ๋ถ€๋ชจ ๋Œ“๊ธ€๋งŒ ํ•„ํ„ฐ๋ง
.map(this::mapCommentWithChildren) // ์ž์‹ ๋Œ“๊ธ€ ํฌํ•จํ•˜์—ฌ ๋งคํ•‘
.toList();
}

default CommentDTO.Response mapCommentWithChildren(Comment comment) {
if (comment == null) {
return null;
}

// ๊ธฐ๋ณธ ๋Œ“๊ธ€ ์ •๋ณด ๋งคํ•‘
CommentDTO.Response.ResponseBuilder response = CommentDTO.Response.builder();

response.name(comment.getUser().getName());
response.time(comment.getModifiedAt());
response.id(comment.getId());
response.content(comment.getContent());

// ์ž์‹ ๋Œ“๊ธ€๋“ค์„ ์žฌ๊ท€์ ์œผ๋กœ ๋งคํ•‘ํ•˜์—ฌ children ํ•„๋“œ์— ์„ค์ •
List<CommentDTO.Response> childrenResponses = comment.getChildren().stream()
.map(this::mapCommentWithChildren) // ์ž์‹ ๋Œ“๊ธ€๋„ ๋™์ผํ•˜๊ฒŒ ์ฒ˜๋ฆฌ
.collect(Collectors.toList());
response.children(childrenResponses);

return response.build();
}
PostDTO.Response toPostDto(Post post, List<FileResponse> fileUrls, List<CommentDTO.Response> comments);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import leets.weeth.domain.board.domain.service.NoticeFindService;
import leets.weeth.domain.board.domain.service.NoticeSaveService;
import leets.weeth.domain.board.domain.service.NoticeUpdateService;
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.file.application.mapper.FileMapper;
import leets.weeth.domain.file.domain.entity.File;
Expand All @@ -26,7 +28,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -67,7 +71,7 @@ public NoticeDTO.Response findNotice(Long noticeId) {
.map(fileMapper::toFileResponse)
.toList();

return mapper.toNoticeDto(notice, response);
return mapper.toNoticeDto(notice, response, filterParentComments(notice.getComments()));
}

@Override
Expand Down Expand Up @@ -117,8 +121,42 @@ private Notice validateOwner(Long noticeId, Long userId) {
return notice;
}

public boolean checkFileExistsByNotice(Long noticeId){
private boolean checkFileExistsByNotice(Long noticeId){
return !fileGetService.findAllByNotice(noticeId).isEmpty();
}

private List<CommentDTO.Response> filterParentComments(List<Comment> comments) {
if (comments == null || comments.isEmpty()) {
return Collections.emptyList();
}

// ๋ถ€๋ชจ ๋Œ“๊ธ€๋งŒ ํ•„ํ„ฐ๋งํ•˜๊ณ , ๊ฐ ๋ถ€๋ชจ ๋Œ“๊ธ€์— ๋Œ€ํ•ด ์ž์‹ ๋Œ“๊ธ€์„ ๋งคํ•‘
return comments.stream()
.filter(comment -> comment.getParent() == null) // ๋ถ€๋ชจ ๋Œ“๊ธ€๋งŒ ํ•„ํ„ฐ๋ง
.map(this::mapCommentWithChildren) // ์ž์‹ ๋Œ“๊ธ€ ํฌํ•จํ•˜์—ฌ ๋งคํ•‘
.toList();
}

private CommentDTO.Response mapCommentWithChildren(Comment comment) {
if (comment == null) {
return null;
}

// ๊ธฐ๋ณธ ๋Œ“๊ธ€ ์ •๋ณด ๋งคํ•‘
CommentDTO.Response.ResponseBuilder response = CommentDTO.Response.builder();

response.name(comment.getUser().getName());
response.time(comment.getModifiedAt());
response.id(comment.getId());
response.content(comment.getContent());

// ์ž์‹ ๋Œ“๊ธ€๋“ค์„ ์žฌ๊ท€์ ์œผ๋กœ ๋งคํ•‘ํ•˜์—ฌ children ํ•„๋“œ์— ์„ค์ •
List<CommentDTO.Response> childrenResponses = comment.getChildren().stream()
.map(this::mapCommentWithChildren) // ์ž์‹ ๋Œ“๊ธ€๋„ ๋™์ผํ•˜๊ฒŒ ์ฒ˜๋ฆฌ
.collect(Collectors.toList());
response.children(childrenResponses);

return response.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import leets.weeth.domain.board.domain.service.PostFindService;
import leets.weeth.domain.board.domain.service.PostSaveService;
import leets.weeth.domain.board.domain.service.PostUpdateService;
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.file.application.mapper.FileMapper;
import leets.weeth.domain.file.domain.entity.File;
Expand All @@ -23,7 +25,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -64,7 +68,8 @@ public PostDTO.Response findPost(Long postId) {
.map(fileMapper::toFileResponse)
.toList();

return mapper.toPostDto(post, response);

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

@Override
Expand Down Expand Up @@ -120,4 +125,38 @@ public boolean checkFileExistsByPost(Long postId){
return !fileGetService.findAllByPost(postId).isEmpty();
}

private List<CommentDTO.Response> filterParentComments(List<Comment> comments) {
if (comments == null || comments.isEmpty()) {
return Collections.emptyList();
}

// ๋ถ€๋ชจ ๋Œ“๊ธ€๋งŒ ํ•„ํ„ฐ๋งํ•˜๊ณ , ๊ฐ ๋ถ€๋ชจ ๋Œ“๊ธ€์— ๋Œ€ํ•ด ์ž์‹ ๋Œ“๊ธ€์„ ๋งคํ•‘
return comments.stream()
.filter(comment -> comment.getParent() == null) // ๋ถ€๋ชจ ๋Œ“๊ธ€๋งŒ ํ•„ํ„ฐ๋ง
.map(this::mapCommentWithChildren) // ์ž์‹ ๋Œ“๊ธ€ ํฌํ•จํ•˜์—ฌ ๋งคํ•‘
.toList();
}

private CommentDTO.Response mapCommentWithChildren(Comment comment) {
if (comment == null) {
return null;
}

// ๊ธฐ๋ณธ ๋Œ“๊ธ€ ์ •๋ณด ๋งคํ•‘
CommentDTO.Response.ResponseBuilder response = CommentDTO.Response.builder();

response.name(comment.getUser().getName());
response.time(comment.getModifiedAt());
response.id(comment.getId());
response.content(comment.getContent());

// ์ž์‹ ๋Œ“๊ธ€๋“ค์„ ์žฌ๊ท€์ ์œผ๋กœ ๋งคํ•‘ํ•˜์—ฌ children ํ•„๋“œ์— ์„ค์ •
List<CommentDTO.Response> childrenResponses = comment.getChildren().stream()
.map(this::mapCommentWithChildren) // ์ž์‹ ๋Œ“๊ธ€๋„ ๋™์ผํ•˜๊ฒŒ ์ฒ˜๋ฆฌ
.collect(Collectors.toList());
response.children(childrenResponses);

return response.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package leets.weeth.domain.comment.application.dto;

import jakarta.validation.constraints.NotBlank;
import leets.weeth.domain.user.domain.entity.enums.Position;
import leets.weeth.domain.user.domain.entity.enums.Role;
import lombok.Builder;

import java.time.LocalDateTime;
Expand All @@ -23,6 +25,8 @@ public record Update(
public record Response(
Long id,
String name,
Position position,
Role role,
String content,
LocalDateTime time, //modifiedAt
List<CommentDTO.Response> children
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public interface CommentMapper {
})
Comment fromCommentDto(CommentDTO.Save dto, Notice notice, User user, Comment parent);

@Mapping(target = "name", source = "user.name")
@Mapping(target = "name", source = "comment.user.name")
@Mapping(target = "position", source = "comment.user.position")
@Mapping(target = "role", source = "comment.user.role")
@Mapping(target = "time", source = "modifiedAt")
CommentDTO.Response toCommentDto(Comment comment);
}

0 comments on commit da91da2

Please sign in to comment.