diff --git a/umbba-api/src/main/java/sopt/org/umbba/api/service/qna/QnAService.java b/umbba-api/src/main/java/sopt/org/umbba/api/service/qna/QnAService.java index a8c451cf..9f6846d5 100644 --- a/umbba-api/src/main/java/sopt/org/umbba/api/service/qna/QnAService.java +++ b/umbba-api/src/main/java/sopt/org/umbba/api/service/qna/QnAService.java @@ -184,14 +184,30 @@ public List getQnaList(Long userId, Long sectionId) { public SingleQnAResponseDto getSingleQna(Long userId, Long qnaId) { User myUser = getUserById(userId); Parentchild parentchild = getParentchildByUser(myUser); - - User opponentUser = getOpponentByParentchild(parentchild, userId); QnA targetQnA = getQnAById(qnaId); Question todayQuestion = targetQnA.getQuestion(); - List qnaList = getQnAListByParentchild(parentchild); - return SingleQnAResponseDto.of(myUser, opponentUser, qnaList.indexOf(targetQnA) + 1, targetQnA, todayQuestion); + List opponentUserList = userRepository.findUserByParentChild(parentchild) + .stream() + .filter(user -> !user.getId().equals(userId)) + .collect(Collectors.toList()); + + + if (opponentUserList.isEmpty()) { + boolean isFirstQnA = qnaList.get(0).equals(targetQnA); + + if (isFirstQnA) { + return SingleQnAResponseDto.of(myUser, null, 1, targetQnA, todayQuestion); + } else { + throw new CustomException(ErrorType.PARENTCHILD_HAVE_NO_OPPONENT); + } + } + + int index = qnaList.indexOf(targetQnA) + 1; + User opponentUser = opponentUserList.get(0); + + return SingleQnAResponseDto.of(myUser, opponentUser, index, targetQnA, todayQuestion); } @Transactional