Skip to content

Commit

Permalink
Merge pull request #47 from olmangjolmang/OMJM-78-home
Browse files Browse the repository at this point in the history
티클문답 관련 수정사항
  • Loading branch information
Amepistheo authored Jul 21, 2024
2 parents 22deb94 + 6e5a652 commit 4f7feca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -67,7 +65,7 @@ public ResponseEntity<ResponseTemplate<Object>> getComments(
@AuthenticationPrincipal CustomUserDetails userDetails,
@RequestParam(required = false, defaultValue = "TIME") Order orderBy) {

List<CommentResponse> responses = opinionService.getCommentsByOpinion(opinionId, userDetails, orderBy);
CommentResponseList responses = opinionService.getCommentsByOpinion(opinionId, userDetails, orderBy);

return ResponseEntity
.status(HttpStatus.OK)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.ticle.server.opinion.dto.response;

import lombok.Builder;

import java.util.List;

@Builder
public record CommentResponseList(
String question,
String userNickname,
List<CommentResponse> responseList
) {
public static CommentResponseList of(String question, String userNickname, List<CommentResponse> responseList) {
return CommentResponseList.builder()
.question(question)
.userNickname(userNickname)
.responseList(responseList)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,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;
Expand Down Expand Up @@ -82,7 +80,7 @@ public void heartComment(Long commentId, CustomUserDetails userDetails) {
}

@Transactional
public List<CommentResponse> 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));

Expand All @@ -93,11 +91,11 @@ public List<CommentResponse> getCommentsByOpinion(Long opinionId, CustomUserDeta

List<Comment> comments = commentRepository.findAllByOpinion(opinion, sort);

return comments.stream()
List<CommentResponse> responses = comments.stream()
.map(comment -> {
boolean isHeart = false;

if (userDetails != null) {
if (ObjectUtils.isNotEmpty(userDetails)) {
User user = userRepository.findById(userDetails.getUserId())
.orElseThrow(() -> new UserNotFoundException(USER_NOT_FOUND));

Expand All @@ -108,6 +106,8 @@ public List<CommentResponse> getCommentsByOpinion(Long opinionId, CustomUserDeta
return CommentResponse.of(comment, isHeart);
})
.toList();

return CommentResponseList.of(opinion.getQuestion(), ObjectUtils.isNotEmpty(userDetails) ? userDetails.getUsername() : "", responses);
}

public OpinionResponseList getOpinionsByPage(int page) {
Expand Down

0 comments on commit 4f7feca

Please sign in to comment.