Skip to content

Commit

Permalink
HOTFIX: 대댓글 중복 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
hyxklee committed Aug 13, 2024
1 parent b7462ef commit f368e25
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import leets.weeth.domain.board.application.dto.NoticeDTO;
import leets.weeth.domain.board.domain.entity.Notice;
import leets.weeth.domain.comment.application.dto.CommentDTO;
import leets.weeth.domain.comment.application.mapper.CommentMapper;
import leets.weeth.domain.comment.domain.entity.Comment;
import leets.weeth.domain.user.domain.entity.User;
import org.mapstruct.*;

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

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING, uses = CommentMapper.class, unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface NoticeMapper {
Expand All @@ -23,9 +27,46 @@ public interface NoticeMapper {

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

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) // 자식 댓글 포함하여 매핑
.collect(Collectors.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();
}

@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 @@ -2,11 +2,15 @@

import leets.weeth.domain.board.application.dto.PostDTO;
import leets.weeth.domain.board.domain.entity.Post;
import leets.weeth.domain.comment.application.dto.CommentDTO;
import leets.weeth.domain.comment.application.mapper.CommentMapper;
import leets.weeth.domain.comment.domain.entity.Comment;
import leets.weeth.domain.user.domain.entity.User;
import org.mapstruct.*;

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

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING, uses = CommentMapper.class, unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface PostMapper {
Expand All @@ -23,9 +27,47 @@ public interface PostMapper {

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

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) // 자식 댓글 포함하여 매핑
.collect(Collectors.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();
}

@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 @@ -10,8 +10,7 @@
import leets.weeth.domain.file.service.FileSaveService;
import leets.weeth.domain.user.domain.entity.User;
import leets.weeth.domain.user.domain.service.UserGetService;
import leets.weeth.global.common.error.exception.custom.InvalidAccessException;
import leets.weeth.global.common.error.exception.custom.PostNotFoundException;
import leets.weeth.global.common.error.exception.custom.NoticeNotFoundException;
import leets.weeth.global.common.error.exception.custom.UserNotMatchException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -59,7 +58,7 @@ public List<NoticeDTO.Response> findNotices(Long noticeId, Integer count) {
noticeId = finalNoticeId + 1;
}
if(noticeId < 1 || noticeId > finalNoticeId + 1){
throw new PostNotFoundException(); // postId가 1 이하이거나 최대값보다 클경우
throw new NoticeNotFoundException(); // postId가 1 이하이거나 최대값보다 클경우
}

Pageable pageable = PageRequest.of(0, count); // 첫 페이지, 페이지당 15개 게시글
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ private Comment findParentComment(Long commentId) {
return null; // 부모 댓글을 찾지 못한 경우
}

// 업데이트 메소드를 엔티티 안에서 변경감지로 사용하기로 했기 때문에, 반환 값이 필요 없짐 -> 나머지도 다 수정
private Comment validateOwner(Long commentId, Long userId) throws UserNotMatchException {
Comment comment = commentFindService.find(commentId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();

configuration.setAllowedOrigins(Arrays.asList("http://localhost:3000", "https://weeth.site", "https://www.weeth.site", "https://api.weeth.site","http://localhost:3000/"));
configuration.setAllowedOrigins(Arrays.asList("http://localhost:3000", "https://weeth.site", "https://www.weeth.site", "https://api.weeth.site"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PATCH", "DELETE"));
configuration.setAllowedHeaders(Arrays.asList("*"));
configuration.setExposedHeaders(Arrays.asList("Authorization, Authorization-refresh"));
Expand Down

0 comments on commit f368e25

Please sign in to comment.