Skip to content

Commit

Permalink
Merge pull request #65 from Team-Umbba/fix/#64-edit_custom_question_l…
Browse files Browse the repository at this point in the history
…ogic

[MODIFY] 사용자 맞춤형 질문 제공을 위한 로직 추가
  • Loading branch information
ddongseop authored Jul 19, 2023
2 parents 414a757 + b432e72 commit 8f42a50
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public OnboardingReceiveResponseDto onboardReceive(Long userId, OnboardingReceiv
/*if (!ParentchildRelation.validate(parentChildUsers, parentchild.getRelation())) {
throw new CustomException(ErrorType.INVALID_PARENT_CHILD_RELATION);
}*/
fcmScheduler.pushTodayQna();
fcmScheduler.pushTodayQna();


return OnboardingReceiveResponseDto.of(parentchild, user, parentChildUsers);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/sopt/org/umbbaServer/domain/qna/dao/QnADao.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public Optional<List<QnA>> findQnASByUserId(Long userId) {
TypedQuery<QnA> query = em.createQuery(jpql, QnA.class);

log.info("query 실행 성공: {}", query);
List<QnA> qnAList = query
List<QnA> qnaList = query
.setParameter("id", userId)
.getResultList();
log.info("query 실행 결과: {}", qnAList.toString());
log.info("query 실행 결과: {}", qnaList.toString());

return Optional.ofNullable(qnAList);
return Optional.of(qnaList);
} catch (NoResultException e) {

return Optional.empty();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/sopt/org/umbbaServer/domain/qna/model/QnA.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ public void saveChildAnswer(String answer) {
this.childAnswer = answer;
this.isChildAnswer = true;
}

public void changeQuestion(Question question) {
this.question = question;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum QuestionSection {
YOUNG(1L, "어린시절", 1),
SCHOOL(2L, "학창시절", 2),
SCHOOL(2L, "학창시절", 1),
GOLDEN(3L, "청춘시절", 2),
COUPLE(4L, "연애시절", 1),
MARRIAGE(5L, "우리가만나고", 1);
MARRIAGE(5L, "우리가 만나고", 1),
MARRIAGE2(6L, "우리가 만나고", 1); // 전연령 - 우리가 만나고
;

private final Long sectionId;
private final String value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum QuestionType {

TYPE1(1L, "타입1"),
TYPE2(2L, "타입2"),
TYPE3(3L, "타입3"),
TYPE4(4L, "타입4"),
TYPE5(5L, "타입5"),
TYPE6(6L, "타입6"),
TYPE7(7L, "타입7");
TYPE1(1L, "타입 1"),
TYPE2(2L, "타입 2"),
TYPE3(3L, "타입 3"),
TYPE4(4L, "타입 4"),
TYPE5(5L, "타입 5"),
MAIN(10L, "메인 타입"),
FIX(11L, "고정된 질문"),
YET(12L, "아직 사용하지 않는 질문");

private final Long typeId;
private final String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import sopt.org.umbbaServer.domain.qna.model.QuestionSection;
import sopt.org.umbbaServer.domain.qna.model.QuestionType;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.*;
import java.util.stream.Collectors;


public interface QuestionRepository extends Repository<Question, Long> {
Expand All @@ -17,6 +15,8 @@ public interface QuestionRepository extends Repository<Question, Long> {

List<Question> findBySectionAndType(QuestionSection section, QuestionType type);

List<Question> findByType(QuestionType type);

default List<Question> findBySectionAndTypeRandom(QuestionSection section, QuestionType type, int size) {
List<Question> matchingQuestions = findBySectionAndType(section, type);
List<Question> selectedQuestions = new ArrayList<>();
Expand All @@ -35,4 +35,11 @@ default List<Question> findBySectionAndTypeRandom(QuestionSection section, Quest

return selectedQuestions;
}

default List<Question> findByTypeOrderBySectionId(QuestionType type) {
return findByType(type)
.stream()
.sorted(Comparator.comparing(question -> question.getSection().getSectionId()))
.collect(Collectors.toList());
}
}
108 changes: 69 additions & 39 deletions src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import java.util.Optional;
import java.util.stream.Collectors;

import static sopt.org.umbbaServer.domain.qna.model.OnboardingAnswer.YES;
import static sopt.org.umbbaServer.domain.qna.model.QuestionSection.YOUNG;
import static sopt.org.umbbaServer.domain.qna.model.OnboardingAnswer.*;
import static sopt.org.umbbaServer.domain.qna.model.QuestionSection.*;
import static sopt.org.umbbaServer.domain.qna.model.QuestionType.*;

@Slf4j
Expand Down Expand Up @@ -88,6 +88,7 @@ public List<QnAListResponseDto> getQnaList(Long userId, Long sectionId) {
List<QnA> qnaList = getQnAListByParentchild(parentchild);

return qnaList.stream()
.limit(parentchild.getCount() - 1) // index까지만 요소를 처리
.filter(qna -> Objects.equals(qna.getQuestion().getSection().getSectionId(), sectionId))
.map(qna -> {
return QnAListResponseDto.builder()
Expand All @@ -104,7 +105,7 @@ public SingleQnAResponseDto getSingleQna(Long userId, Long qnaId) {
Parentchild parentchild = getParentchildByUser(myUser);

User opponentUser = getOpponentByParentchild(parentchild, userId);
QnA targetQnA = getQnAById(qnaId); // 이거 qnA로 할건지 qna로 할건지 통일 필요
QnA targetQnA = getQnAById(qnaId);
Question todayQuestion = targetQnA.getQuestion();

List<QnA> qnaList = getQnAListByParentchild(parentchild);
Expand Down Expand Up @@ -159,25 +160,28 @@ public void filterAllQuestion(Long userId, List<String> onboardingAnswerStringLi
List<OnboardingAnswer> childList = parentchild.getChildOnboardingAnswerList();
List<OnboardingAnswer> parentList = parentchild.getParentOnboardingAnswerList();

// 질문 그룹을 선택
QuestionType selectedType = selectType(childList, parentList);
System.out.println("선택된 질문 타입: " + selectedType);

for (QuestionSection section : QuestionSection.values()) {
if (section == YOUNG) continue;

List<Question> selectedQuestions = questionRepository.findBySectionAndTypeRandom(section, selectedType, section.getQuestionCount());

for (Question question : selectedQuestions) {
// 커스텀되기 전의 메인 질문 리스트를 가져옴
if (parentchild.getQnaList().size() == 1) {
List<Question> mainQuestions = questionRepository.findByTypeOrderBySectionId(MAIN);
for (Question mainQuestion : mainQuestions) {
QnA newQnA = QnA.builder()
.question(question)
.question(mainQuestion)
.isParentAnswer(false)
.isChildAnswer(false)
.build();
qnARepository.save(newQnA);
parentchild.addQnA(newQnA);
}
}

// 선택 질문에 따라 질문 리스트가 커스텀됨
customQuestion(childList, parentList, parentchild.getQnaList());

log.info("선택된 질문 리스트");
List<QnA> forLogging= parentchild.getQnaList();
for (QnA qnA : forLogging) {
log.info(qnA.getQuestion().getParentQuestion());
}
}

/*
Expand Down Expand Up @@ -205,21 +209,21 @@ private Parentchild getParentchildByUserId(Long userId) {
}

private List<QnA> getQnAListByParentchild(Parentchild parentchild) {
List<QnA> qnAList = parentchild.getQnaList();
if (qnAList == null || qnAList.isEmpty()) {
List<QnA> qnaList = parentchild.getQnaList();
if (qnaList == null || qnaList.isEmpty()) {
throw new CustomException(ErrorType.PARENTCHILD_HAVE_NO_QNALIST);
}

return qnAList;
return qnaList;
}

private QnA getTodayQnAByParentchild(Parentchild parentchild) {
List<QnA> qnAList = parentchild.getQnaList();
if (qnAList == null || qnAList.isEmpty()) {
List<QnA> qnaList = parentchild.getQnaList();
if (qnaList == null || qnaList.isEmpty()) {
throw new CustomException(ErrorType.PARENTCHILD_HAVE_NO_QNALIST);
}

return qnAList.get(parentchild.getCount() - 1); // 가장 최근의 QnA를 가져옴
return qnaList.get(parentchild.getCount() - 1); // 가장 최근의 QnA를 가져옴
}

private QnA getQnAById(Long qnaId) {
Expand All @@ -240,28 +244,54 @@ private User getOpponentByParentchild(Parentchild parentchild, Long userId) {
return opponentUserList.get(0);
}

private QuestionType selectType(List<OnboardingAnswer> childList, List<OnboardingAnswer> parentList) {

// 그룹 선택 알고리즘
if (childList.get(0) == YES && parentList.get(0) == YES) {
return TYPE1;
}
if (childList.get(1) == YES && parentList.get(1) == YES) {
return TYPE2;
@Transactional
private void customQuestion(List<OnboardingAnswer> childList, List<OnboardingAnswer> parentList, List<QnA> qnAList) {

// Type 1 : 1번째 선택 질문인 거주 현황에 대해 한명이라도 아니/애매해라고 답한 경우
if (childList.get(0) != YES || parentList.get(0) !=YES) {
log.info("Type1의 질문 세트가 적용됨");
Question selectedQuestion = questionRepository.findBySectionAndTypeRandom(SCHOOL, TYPE1, 1).get(0);
log.debug("이거 들어감: " + selectedQuestion.getParentQuestion());
qnAList.get(1).changeQuestion(selectedQuestion);
}
if (childList.get(2) == YES && parentList.get(2) == YES) {
return TYPE3;
// Type 2 : 2번째 선택 질문인 학력 현황에 대해 한명이라도 아니/애매해라고 답한 경우
else if (childList.get(1) != YES || parentList.get(1) != YES) {
log.info("Type2의 질문 세트가 적용됨");
Question selectedQuestion = questionRepository.findBySectionAndTypeRandom(SCHOOL, TYPE2, 1).get(0);
log.debug("이거 들어감: " + selectedQuestion.getParentQuestion());
qnAList.get(1).changeQuestion(selectedQuestion);
}
if (childList.get(3) == YES && parentList.get(3) == YES) {
return TYPE4;

// Type 3 : 3번째 선택 질문인 결혼 가치관에 대해 한명이라도 아니라고 답한 경우
if (childList.get(2) == NO || parentList.get(2) == NO) {
log.info("Type3의 질문 세트가 적용됨");
Question selectedQuestion = questionRepository.findBySectionAndTypeRandom(COUPLE, TYPE3, 1).get(0);
log.debug("이거 들어감: " + selectedQuestion.getParentQuestion());
qnAList.get(4).changeQuestion(selectedQuestion);
}
if (childList.get(4) == YES && parentList.get(4) == YES) {
return TYPE5;

// Type 5 : 5번째 선택 질문인 후회 여부에 대해 한명이라도 아니라고 답한 경우
if (childList.get(4) == NO || parentList.get(4) == NO) {
log.info("Type5의 질문 세트가 적용됨");
Question selectedQuestion = questionRepository.findBySectionAndTypeRandom(GOLDEN, TYPE5, 1).get(0);
log.debug("이거 들어감: " + selectedQuestion.getParentQuestion());
qnAList.get(3).changeQuestion(selectedQuestion);
}
if (childList.get(5) == YES && parentList.get(5) == YES) {
return TYPE6;
// Type 4 : 4번째 선택 질문인 포기 여부에 대해 한명이라도 아니/애매해라고 답한 경우
else if (childList.get(3) != YES || parentList.get(3) != YES) {
log.info("Type4의 질문 세트가 적용됨");
Question selectedQuestion = questionRepository.findBySectionAndTypeRandom(GOLDEN, TYPE4, 1).get(0);
log.debug("이거 들어감: " + selectedQuestion.getParentQuestion());
qnAList.get(3).changeQuestion(selectedQuestion);

selectedQuestion = questionRepository.findBySectionAndTypeRandom(MARRIAGE, TYPE4, 1).get(0);
log.debug("이거 들어감: " + selectedQuestion.getParentQuestion());
qnAList.get(5).changeQuestion(selectedQuestion);

selectedQuestion = questionRepository.findBySectionAndTypeRandom(MARRIAGE2, TYPE4, 1).get(0);
log.debug("이거 들어감: " + selectedQuestion.getParentQuestion());
qnAList.get(6).changeQuestion(selectedQuestion);
}
return TYPE7;
}

/*
Expand All @@ -272,9 +302,9 @@ private QuestionType selectType(List<OnboardingAnswer> childList, List<Onboardin
public GetMainViewResponseDto getMainInfo(Long userId) {

Parentchild parentchild = getParentchildByUserId(userId);
List<QnA> qnAList = getQnAListByParentchild(parentchild);
List<QnA> qnaList = getQnAListByParentchild(parentchild);

QnA lastQna = qnAList.get(parentchild.getCount() - 1);
QnA lastQna = qnaList.get(parentchild.getCount() - 1);

return GetMainViewResponseDto.of(lastQna, parentchild.getCount());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void schedulePushAlarm(String cronExpression, Long parentchildId) {

QnA todayQnA = parentchild.getQnaList().get(parentchild.getCount() - 1);
if (todayQnA == null) {
log.error("{}번째 Parentchild의 QnAList가 존재하지 않음!", parentchild.getId());
log.error("{}번째 Parentchild의 QnaList가 존재하지 않음!", parentchild.getId());
}

List<User> parentChildUsers = userRepository.findUserByParentChild(parentchild);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ spring:
naming_strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
properties:
hibernate:
format_sql: true
format_sql: false

kakao:
client-id: ${kakao-id}
Expand Down

0 comments on commit 8f42a50

Please sign in to comment.