From d8d980f7d1ba48b0d9cfb58d925688564a7b3f23 Mon Sep 17 00:00:00 2001 From: Amepistheo Date: Sat, 20 Jul 2024 18:10:08 +0900 Subject: [PATCH 1/2] =?UTF-8?q?:heavy=5Fplus=5Fsign:=20Add:=20=EB=AA=A8?= =?UTF-8?q?=EB=93=A0=20=EB=8C=93=EA=B8=80=20return=ED=95=A0=20=EB=95=8C=20?= =?UTF-8?q?=EC=A7=88=EB=AC=B8=EB=8F=84=20=ED=95=A8=EA=BB=98=20=EC=A3=BC?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../opinion/controller/OpinionController.java | 6 ++---- .../dto/response/CommentResponseList.java | 18 ++++++++++++++++++ .../server/opinion/service/OpinionService.java | 11 +++++------ 3 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/ticle/server/opinion/dto/response/CommentResponseList.java diff --git a/src/main/java/com/ticle/server/opinion/controller/OpinionController.java b/src/main/java/com/ticle/server/opinion/controller/OpinionController.java index c49c8e5..14eaf69 100644 --- a/src/main/java/com/ticle/server/opinion/controller/OpinionController.java +++ b/src/main/java/com/ticle/server/opinion/controller/OpinionController.java @@ -3,7 +3,7 @@ import com.ticle.server.global.dto.ResponseTemplate; import com.ticle.server.opinion.domain.type.Order; import com.ticle.server.opinion.dto.request.CommentUploadRequest; -import com.ticle.server.opinion.dto.response.CommentResponse; +import com.ticle.server.opinion.dto.response.CommentResponseList; import com.ticle.server.opinion.dto.response.OpinionResponseList; import com.ticle.server.opinion.service.OpinionService; import com.ticle.server.user.service.CustomUserDetails; @@ -17,8 +17,6 @@ import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; -import java.util.List; - import static com.ticle.server.global.dto.ResponseTemplate.EMPTY_RESPONSE; @Slf4j @@ -67,7 +65,7 @@ public ResponseEntity> getComments( @AuthenticationPrincipal CustomUserDetails userDetails, @RequestParam(required = false, defaultValue = "TIME") Order orderBy) { - List responses = opinionService.getCommentsByOpinion(opinionId, userDetails, orderBy); + CommentResponseList responses = opinionService.getCommentsByOpinion(opinionId, userDetails, orderBy); return ResponseEntity .status(HttpStatus.OK) diff --git a/src/main/java/com/ticle/server/opinion/dto/response/CommentResponseList.java b/src/main/java/com/ticle/server/opinion/dto/response/CommentResponseList.java new file mode 100644 index 0000000..405805f --- /dev/null +++ b/src/main/java/com/ticle/server/opinion/dto/response/CommentResponseList.java @@ -0,0 +1,18 @@ +package com.ticle.server.opinion.dto.response; + +import lombok.Builder; + +import java.util.List; + +@Builder +public record CommentResponseList( + String question, + List responseList +) { + public static CommentResponseList of(String question, List responseList) { + return CommentResponseList.builder() + .question(question) + .responseList(responseList) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/ticle/server/opinion/service/OpinionService.java b/src/main/java/com/ticle/server/opinion/service/OpinionService.java index 753d9b3..934fac2 100644 --- a/src/main/java/com/ticle/server/opinion/service/OpinionService.java +++ b/src/main/java/com/ticle/server/opinion/service/OpinionService.java @@ -5,10 +5,7 @@ import com.ticle.server.opinion.domain.Opinion; import com.ticle.server.opinion.domain.type.Order; import com.ticle.server.opinion.dto.request.CommentUploadRequest; -import com.ticle.server.opinion.dto.response.CommentResponse; -import com.ticle.server.opinion.dto.response.HeartResponse; -import com.ticle.server.opinion.dto.response.OpinionResponse; -import com.ticle.server.opinion.dto.response.OpinionResponseList; +import com.ticle.server.opinion.dto.response.*; import com.ticle.server.opinion.exception.CommentNotFoundException; import com.ticle.server.opinion.exception.OpinionNotFoundException; import com.ticle.server.opinion.repository.CommentRepository; @@ -82,7 +79,7 @@ public void heartComment(Long commentId, CustomUserDetails userDetails) { } @Transactional - public List getCommentsByOpinion(Long opinionId, CustomUserDetails userDetails, Order orderBy) { + public CommentResponseList getCommentsByOpinion(Long opinionId, CustomUserDetails userDetails, Order orderBy) { Opinion opinion = opinionRepository.findByOpinionIdWithFetch(opinionId) .orElseThrow(() -> new OpinionNotFoundException(OPINION_NOT_FOUND)); @@ -93,7 +90,7 @@ public List getCommentsByOpinion(Long opinionId, CustomUserDeta List comments = commentRepository.findAllByOpinion(opinion, sort); - return comments.stream() + List responses = comments.stream() .map(comment -> { boolean isHeart = false; @@ -108,6 +105,8 @@ public List getCommentsByOpinion(Long opinionId, CustomUserDeta return CommentResponse.of(comment, isHeart); }) .toList(); + + return CommentResponseList.of(opinion.getQuestion(), responses); } public OpinionResponseList getOpinionsByPage(int page) { From 6e5a652312cc95043902426e6786d05562e7424c Mon Sep 17 00:00:00 2001 From: Amepistheo Date: Sun, 21 Jul 2024 01:37:38 +0900 Subject: [PATCH 2/2] =?UTF-8?q?:heavy=5Fplus=5Fsign:=20Add:=20=EC=A7=88?= =?UTF-8?q?=EB=AC=B8=EC=97=90=20=EB=8C=80=ED=95=9C=20=EB=AA=A8=EB=93=A0=20?= =?UTF-8?q?=EB=8C=93=EA=B8=80=20return=ED=95=A0=20=EB=95=8C=20=ED=98=84?= =?UTF-8?q?=EC=9E=AC=20=EC=9C=A0=EC=A0=80=EC=9D=98=20nickname=EB=8F=84=20?= =?UTF-8?q?=EC=A3=BC=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/opinion/dto/response/CommentResponseList.java | 4 +++- .../com/ticle/server/opinion/service/OpinionService.java | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/ticle/server/opinion/dto/response/CommentResponseList.java b/src/main/java/com/ticle/server/opinion/dto/response/CommentResponseList.java index 405805f..36a0210 100644 --- a/src/main/java/com/ticle/server/opinion/dto/response/CommentResponseList.java +++ b/src/main/java/com/ticle/server/opinion/dto/response/CommentResponseList.java @@ -7,11 +7,13 @@ @Builder public record CommentResponseList( String question, + String userNickname, List responseList ) { - public static CommentResponseList of(String question, List responseList) { + public static CommentResponseList of(String question, String userNickname, List responseList) { return CommentResponseList.builder() .question(question) + .userNickname(userNickname) .responseList(responseList) .build(); } diff --git a/src/main/java/com/ticle/server/opinion/service/OpinionService.java b/src/main/java/com/ticle/server/opinion/service/OpinionService.java index 934fac2..7482a28 100644 --- a/src/main/java/com/ticle/server/opinion/service/OpinionService.java +++ b/src/main/java/com/ticle/server/opinion/service/OpinionService.java @@ -17,6 +17,7 @@ import com.ticle.server.user.service.CustomUserDetails; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.ObjectUtils; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -94,7 +95,7 @@ public CommentResponseList getCommentsByOpinion(Long opinionId, CustomUserDetail .map(comment -> { boolean isHeart = false; - if (userDetails != null) { + if (ObjectUtils.isNotEmpty(userDetails)) { User user = userRepository.findById(userDetails.getUserId()) .orElseThrow(() -> new UserNotFoundException(USER_NOT_FOUND)); @@ -106,7 +107,7 @@ public CommentResponseList getCommentsByOpinion(Long opinionId, CustomUserDetail }) .toList(); - return CommentResponseList.of(opinion.getQuestion(), responses); + return CommentResponseList.of(opinion.getQuestion(), ObjectUtils.isNotEmpty(userDetails) ? userDetails.getUsername() : "", responses); } public OpinionResponseList getOpinionsByPage(int page) {