Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : CommentDslDto 삭제 후 CommentDto로 통합 #145

Merged
merged 1 commit into from
Apr 29, 2024
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
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.nawabali.nawabali.controller;

import com.nawabali.nawabali.dto.CommentDto;
import com.nawabali.nawabali.dto.querydsl.CommentDslDto;
import com.nawabali.nawabali.security.UserDetailsImpl;
import com.nawabali.nawabali.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -41,9 +40,9 @@ public CommentDto.ResponseDto createComment (@PathVariable("postId") Long postId
@Parameter(name = "size", description = "페이지 당 댓글의 수", example = "5")
})
@GetMapping("/check/posts/{postId}")
public ResponseEntity<Slice<CommentDslDto.ResponseDto>> getComments(@PathVariable Long postId,
public ResponseEntity<Slice<CommentDto.GetResponseDto>> getComments(@PathVariable Long postId,
@PageableDefault(size = 5) Pageable pageable) {
Slice<CommentDslDto.ResponseDto> comments = commentService.getComments(postId, pageable);
Slice<CommentDto.GetResponseDto> comments = commentService.getComments(postId, pageable);
return ResponseEntity.ok(comments);
}

Expand Down
57 changes: 53 additions & 4 deletions src/main/java/com/nawabali/nawabali/dto/CommentDto.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.nawabali.nawabali.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.nawabali.nawabali.constant.UserRankEnum;
import com.nawabali.nawabali.domain.Comment;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import com.nawabali.nawabali.exception.CustomException;
import com.nawabali.nawabali.exception.ErrorCode;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j(topic = "CommentDto 로그")
public class CommentDto {
Expand Down Expand Up @@ -79,5 +80,53 @@ public static class DeleteResponseDto{
private Long commentId;
private String message;
}

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class GetResponseDto {

private Long commentId;
private Long postId;
private Long userId;
private Long parentId;
private String nickname;
private String contents;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
private LocalDateTime createdAt;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
private LocalDateTime modifiedAt;
private String profileImageUrl;
private String userInfo;
private List<CommentDto.GetResponseDto> children = new ArrayList<>();

public GetResponseDto(Comment comment) {
this.commentId = comment.getId();
this.contents = comment.getContents();
this.postId = comment.getPost().getId();
this.userId = comment.getUser().getId();
this.parentId = comment.getParent() != null ? comment.getParent().getId() : null;
this.nickname = comment.getUser().getNickname();
this.createdAt = comment.getCreatedAt();
this.modifiedAt = comment.getModifiedAt();
this.profileImageUrl = comment.getUser().getProfileImage().getImgUrl();
this.userInfo = comment.getUser().getAddress().getDistrict()+" "+RankInKorean(comment);
this.children = comment.getChildren().stream()
.map(CommentDto.GetResponseDto::new)
.collect(Collectors.toList());
}

public String RankInKorean(Comment comment) {
UserRankEnum rank = comment.getUser().getRank();
return switch (rank) {
case RESIDENT -> "주민";
case NATIVE_PERSON -> "토박이";
case LOCAL_ELDER -> "터줏대감";
default -> throw new CustomException(ErrorCode.RANK_NOT_FOUND);
};
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.nawabali.nawabali.repository.querydsl.comment;

import com.nawabali.nawabali.dto.querydsl.CommentDslDto;
import com.nawabali.nawabali.dto.CommentDto;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

public interface CommentDslRepositoryCustom {
//findCommentByPostIdWithParentOrderByParentIdAscNullsFirstCreatedAtAsc
Slice<CommentDslDto.ResponseDto> findCommentsByPostId(Long postId, Pageable pageable);
Slice<CommentDto.GetResponseDto> findCommentsByPostId(Long postId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.nawabali.nawabali.domain.Comment;
import com.nawabali.nawabali.domain.QComment;
import com.nawabali.nawabali.dto.querydsl.CommentDslDto;
import com.nawabali.nawabali.dto.CommentDto;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand All @@ -20,7 +20,7 @@ public class CommentDslRepositoryCustomImpl implements CommentDslRepositoryCusto
private final JPAQueryFactory queryFactory;

@Override
public Slice<CommentDslDto.ResponseDto> findCommentsByPostId(Long postId, Pageable pageable) {
public Slice<CommentDto.GetResponseDto> findCommentsByPostId(Long postId, Pageable pageable) {
QComment comment = QComment.comment;

List<Comment> comments = queryFactory
Expand All @@ -41,8 +41,8 @@ public Slice<CommentDslDto.ResponseDto> findCommentsByPostId(Long postId, Pageab
comments.remove(comments.size() - 1);
}

List<CommentDslDto.ResponseDto> responseDtos = comments.stream()
.map(CommentDslDto.ResponseDto::new)
List<CommentDto.GetResponseDto> responseDtos = comments.stream()
.map(CommentDto.GetResponseDto::new)
.collect(Collectors.toList());

return new SliceImpl<>(responseDtos, pageable, hasNext);
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/nawabali/nawabali/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.nawabali.nawabali.domain.Post;
import com.nawabali.nawabali.domain.User;
import com.nawabali.nawabali.dto.CommentDto;
import com.nawabali.nawabali.dto.querydsl.CommentDslDto;
import com.nawabali.nawabali.exception.CustomException;
import com.nawabali.nawabali.exception.ErrorCode;
import com.nawabali.nawabali.repository.CommentRepository;
Expand Down Expand Up @@ -120,16 +119,16 @@ public CommentDto.DeleteResponseDto deleteComment(Long commentId, Long userId) {

// 댓글 조회(무한 스크롤)
@Transactional(readOnly = true)
public Slice<CommentDslDto.ResponseDto> getComments(Long postId, Pageable pageable) {
public Slice<CommentDto.GetResponseDto> getComments(Long postId, Pageable pageable) {
postRepository.findById(postId).orElseThrow(()-> new CustomException(ErrorCode.POST_NOT_FOUND));
return convertNestedStructure(commentRepository.findCommentsByPostId(postId, pageable));
}

private Slice<CommentDslDto.ResponseDto> convertNestedStructure(Slice<CommentDslDto.ResponseDto> comments) {
private Slice<CommentDto.GetResponseDto> convertNestedStructure(Slice<CommentDto.GetResponseDto> comments) {
// 중첩 구조로 변환된 댓글 목록을 저장할 리스트입니다.
List<CommentDslDto.ResponseDto> result = new ArrayList<>();
List<CommentDto.GetResponseDto> result = new ArrayList<>();
// 댓글을 부모-자식 구조로 변환하는 데 사용할 맵 : 키는 댓글의 식별자(commentId)이고, 값은 실제 댓글 객체입니다.
Map<Long, CommentDslDto.ResponseDto> map = new HashMap<>();
Map<Long, CommentDto.GetResponseDto> map = new HashMap<>();
// 댓글을 parent comment를 기준으로 순회하여 부모-자식 관계를 형성합니다.
comments.forEach(dto -> {
// 현재 댓글을 맵에 추가합니다.
Expand All @@ -138,7 +137,7 @@ private Slice<CommentDslDto.ResponseDto> convertNestedStructure(Slice<CommentDsl
// 부모 댓글인 경우, 결과 목록에 추가합니다.
if (dto.getParentId() != null) {
// 부모 댓글을 찾아 해당 자식 댓글을 추가합니다.
map.computeIfAbsent(dto.getParentId(), k -> new CommentDslDto.ResponseDto()).getChildren().add(dto);
map.computeIfAbsent(dto.getParentId(), k -> new CommentDto.GetResponseDto()).getChildren().add(dto);
} else {
// 부모 댓글이 없는 경우: 결과 목록에 바로 추가합니다.
result.add(dto);
Expand Down