Skip to content

Commit

Permalink
feat: Survey 상세 조회 응답에 optionName 추가(#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
eun-seong committed Mar 19, 2024
1 parent fb3fe42 commit 3c6e49b
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.dnd.namuiwiki.domain.question.type.QuestionName;
import com.dnd.namuiwiki.domain.survey.model.entity.Answer;
import com.dnd.namuiwiki.domain.survey.model.entity.Survey;
import com.dnd.namuiwiki.domain.survey.type.AnswerType;
import com.dnd.namuiwiki.domain.survey.type.Period;
import com.dnd.namuiwiki.domain.survey.type.Relation;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -36,15 +35,19 @@ private static class SingleQuestionAndAnswer {
private Object value;
private String reason;
private QuestionName questionName;
private String optionName;

static SingleQuestionAndAnswer from(Question question, Answer surveyAnswer) {
if (surveyAnswer.getType().equals(AnswerType.MANUAL)) {
String optionName = getOptionName(question, surveyAnswer);

if (surveyAnswer.getType().isManual()) {
return new SingleQuestionAndAnswer(
question.getTitle(),
surveyAnswer.getAnswer().toString(),
surveyAnswer.getAnswer(),
surveyAnswer.getReason(),
question.getName()
question.getName(),
optionName
);
}
Option option = question.getOption(surveyAnswer.getAnswer().toString())
Expand All @@ -54,9 +57,29 @@ static SingleQuestionAndAnswer from(Question question, Answer surveyAnswer) {
option.getText(),
option.getValue(),
surveyAnswer.getReason(),
question.getName()
question.getName(),
optionName
);
}

private static String getOptionName(Question question, Answer surveyAnswer) {
String optionName = null;

if (question.getType().isChoiceType()) {
if (surveyAnswer.getType().isOption()) {
optionName = question.getOption(surveyAnswer.getAnswer().toString())
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_OPTION_ID))
.getName();
} else {
optionName = question.getOptions().values().stream()
.filter(option -> option.getName().contains("MANUAL"))
.findFirst()
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_OPTION_ID))
.getName();
}
}
return optionName;
}
}

public static GetSurveyResponse from(Survey survey, List<Question> questions) {
Expand Down

0 comments on commit 3c6e49b

Please sign in to comment.