diff --git a/umbba-api/src/main/java/sopt/org/umbba/api/service/closer/CloserService.java b/umbba-api/src/main/java/sopt/org/umbba/api/service/closer/CloserService.java index f6d40e39..fbf8a27e 100644 --- a/umbba-api/src/main/java/sopt/org/umbba/api/service/closer/CloserService.java +++ b/umbba-api/src/main/java/sopt/org/umbba/api/service/closer/CloserService.java @@ -16,6 +16,7 @@ import sopt.org.umbba.domain.domain.user.User; import sopt.org.umbba.domain.domain.user.repository.UserRepository; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -36,6 +37,10 @@ public TodayCloserQnAResponseDto getTodayCloserQnA(Long userId) { throw new CustomException(ErrorType.USER_HAVE_NO_PARENTCHILD); } + if (parentchild.getCloserQnaList().isEmpty()) { + addFirstCloserQnA(parentchild); + } + if (user.isMeChild()) { int closerCount = parentchild.getCloserChildCount(); CloserQnA todayQnA = parentchild.getCloserQnaList().get(closerCount); @@ -62,6 +67,20 @@ public TodayCloserQnAResponseDto getTodayCloserQnA(Long userId) { } } + @Transactional + public void addFirstCloserQnA(Parentchild parentchild) { + CloserQuestion firstCloserQuestion = closerQuestionRepository.findRandomExceptIds(new ArrayList<>()) + .orElseThrow(() -> new CustomException(ErrorType.NOT_FOUND_CLOSER_QUESTION)); + + CloserQnA newCloserQnA = CloserQnA.builder() + .closerQuestion(firstCloserQuestion) + .isParentAnswer(false) + .isChildAnswer(false) + .build(); + closerQnARepository.save(newCloserQnA); + parentchild.addCloserQna(newCloserQnA); + } + @Transactional public void answerTodayCloserQnA(Long userId, TodayCloserAnswerRequestDto request) { User user = getUserById(userId);