Skip to content

Commit

Permalink
Merge pull request #46 from Leets-Official/#45/refactor/대댓글-중복제거
Browse files Browse the repository at this point in the history
#46 Refactor: 대댓글 중복제거 코드 리팩토링
  • Loading branch information
hyxklee authored Aug 13, 2024
2 parents 3ce97d3 + f31bf52 commit 05a1a52
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,15 @@ public record Response(
List<String> fileUrls,
List<CommentDTO.Response> comments
){}

@Builder
public record ResponseAll(
Long id,
String name,
String title,
String content,
LocalDateTime time,//modifiedAt
Integer commentCount
){}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ public record Response(
List<CommentDTO.Response> comments
){}

@Builder
public record ResponseAll(
Long id,
String name,
String title,
String content,
LocalDateTime time,//modifiedAt
Integer commentCount
){}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public interface NoticeMapper {
})
Notice fromNoticeDto(NoticeDTO.Save dto, List<String> fileUrls, User user);

// @Mapping(target = "id", source = "noticeId")
// @Mapping(target = "user", source = "user")
// Notice update(Long noticeId, NoticeDTO.Update dto, List<String> fileUrls, User user);
@Mappings({
@Mapping(target = "name", source = "user.name"),
@Mapping(target = "time", source = "modifiedAt")
})
NoticeDTO.ResponseAll toAll(Notice notice);

@Mappings({
@Mapping(target = "name", source = "user.name"),
Expand All @@ -41,7 +43,7 @@ default List<CommentDTO.Response> filterParentComments(List<Comment> comments) {
return comments.stream()
.filter(comment -> comment.getParent() == null) // 부모 댓글만 필터링
.map(this::mapCommentWithChildren) // 자식 댓글 포함하여 매핑
.collect(Collectors.toList());
.toList();
}

default CommentDTO.Response mapCommentWithChildren(Comment comment) {
Expand All @@ -66,7 +68,4 @@ default CommentDTO.Response mapCommentWithChildren(Comment comment) {
return response.build();
}

@Mapping(target = "name", source = "user.name")
@Mapping(target = "time", source = "modifiedAt")
CommentDTO.Response mapComment(Comment comment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public interface PostMapper {
})
Post fromPostDto(PostDTO.Save dto, List<String> fileUrls, User user);

// @Mapping(target = "id", source = "postId")
// @Mapping(target = "user", source = "user")
// Post update(Long postId, PostDTO.Update dto, User user);
@Mappings({
@Mapping(target = "name", source = "user.name"),
@Mapping(target = "time", source = "modifiedAt")
})
PostDTO.ResponseAll toAll(Post post);

@Mappings({
@Mapping(target = "name", source = "user.name"),
Expand All @@ -41,7 +43,7 @@ default List<CommentDTO.Response> filterParentComments(List<Comment> comments) {
return comments.stream()
.filter(comment -> comment.getParent() == null) // 부모 댓글만 필터링
.map(this::mapCommentWithChildren) // 자식 댓글 포함하여 매핑
.collect(Collectors.toList());
.toList();
}

default CommentDTO.Response mapCommentWithChildren(Comment comment) {
Expand All @@ -66,8 +68,4 @@ default CommentDTO.Response mapCommentWithChildren(Comment comment) {
return response.build();
}

@Mapping(target = "name", source = "user.name")
@Mapping(target = "time", source = "modifiedAt")
CommentDTO.Response mapComment(Comment comment);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface NoticeUsecase {

NoticeDTO.Response findNotice(Long noticeId);

List<NoticeDTO.Response> findNotices(Long noticeId, Integer count);
List<NoticeDTO.ResponseAll> findNotices(Long noticeId, Integer count);

void update(Long noticeId, NoticeDTO.Update dto, List<MultipartFile> files, Long userId) throws UserNotMatchException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public NoticeDTO.Response findNotice(Long noticeId) {
}

@Override
public List<NoticeDTO.Response> findNotices(Long noticeId, Integer count) {
public List<NoticeDTO.ResponseAll> findNotices(Long noticeId, Integer count) {

Long finalNoticeId = noticeFindService.findFinalNoticeId();

Expand All @@ -66,7 +66,7 @@ public List<NoticeDTO.Response> findNotices(Long noticeId, Integer count) {
List<Notice> notices = noticeFindService.findRecentNotices(noticeId, pageable);

return notices.stream()
.map(mapper::toNoticeDto)
.map(mapper::toAll)
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public PostDTO.Response findPost(Long postId) {
}

@Override
public List<PostDTO.Response> findPosts(Long postId, Integer count) {
public List<PostDTO.ResponseAll> findPosts(Long postId, Integer count) {

Long finalPostId = postFindService.findFinalPostId();

Expand All @@ -68,7 +68,7 @@ public List<PostDTO.Response> findPosts(Long postId, Integer count) {
List<Post> posts = postFindService.findRecentPosts(postId, pageable);

return posts.stream()
.map(mapper::toPostDto)
.map(mapper::toAll)
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface PostUsecase {

PostDTO.Response findPost(Long postId);

List<PostDTO.Response> findPosts(Long postId, Integer count);
List<PostDTO.ResponseAll> findPosts(Long postId, Integer count);

void update(Long postId, PostDTO.Update dto, List<MultipartFile> files, Long userId) throws UserNotMatchException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class NoticeController {
private final NoticeUsecase noticeUsecase;

@GetMapping
public CommonResponse<List<NoticeDTO.Response>> findNotices(@RequestParam(required = false) Long noticeId, @RequestParam Integer count) {
public CommonResponse<List<NoticeDTO.ResponseAll>> findNotices(@RequestParam(required = false) Long noticeId, @RequestParam Integer count) {
return CommonResponse.createSuccess(noticeUsecase.findNotices(noticeId, count));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public CommonResponse<String> save(@RequestPart @Valid PostDTO.Save dto,
}

@GetMapping
public CommonResponse<List<PostDTO.Response>> findPosts(@RequestParam(required = false) Long postId, @RequestParam Integer count) {
public CommonResponse<List<PostDTO.ResponseAll>> findPosts(@RequestParam(required = false) Long postId, @RequestParam Integer count) {
return CommonResponse.createSuccess(postUsecase.findPosts(postId, count));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface CommentMapper {
@Mapping(target = "user", source = "user"),
@Mapping(target = "parent", source = "parent"),
@Mapping(target = "content", source = "dto.content"),
@Mapping(target = "post", source = "post"),
@Mapping(target = "post", source = "post")
})
Comment fromCommentDto(CommentDTO.Save dto, Post post, User user, Comment parent);

Expand All @@ -32,13 +32,11 @@ public interface CommentMapper {
@Mapping(target = "user", source = "user"),
@Mapping(target = "parent", source = "parent"),
@Mapping(target = "content", source = "dto.content"),
@Mapping(target = "notice", source = "notice"),

@Mapping(target = "notice", source = "notice")
})
Comment fromCommentDto(CommentDTO.Save dto, Notice notice, User user, Comment parent);

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void saveNoticeComment(CommentDTO.Save dto, Long noticeId, Long userId) {
notice.incrementCommentCount();
}


@Override
@Transactional
public void updateNoticeComment(CommentDTO.Update dto, Long noticeId, Long commentId, Long userId) throws UserNotMatchException {
Expand Down

0 comments on commit 05a1a52

Please sign in to comment.