diff --git a/src/main/java/com/dnd/namuiwiki/domain/option/entity/Option.java b/src/main/java/com/dnd/namuiwiki/domain/option/entity/Option.java index fa578bf4..9229e9f8 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/option/entity/Option.java +++ b/src/main/java/com/dnd/namuiwiki/domain/option/entity/Option.java @@ -17,6 +17,7 @@ public class Option extends BaseTimeEntity { private String id; private Object value; private String text; + private String name; private int order; @Override diff --git a/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java b/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java index e38e5b05..4dd178e2 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java @@ -144,6 +144,7 @@ public GetAnswersByQuestionResponse getAnswersByQuestion(String wikiId, String q .createdAt(survey.getWrittenAt()) .answer(convertAnswerToText(question, answerOfQuestion)) .reason(answerOfQuestion.getReason()) + .optionName(question, answerOfQuestion) .build(); }); diff --git a/src/main/java/com/dnd/namuiwiki/domain/survey/model/dto/GetSurveyResponse.java b/src/main/java/com/dnd/namuiwiki/domain/survey/model/dto/GetSurveyResponse.java index ee58fd02..0fc669c0 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/survey/model/dto/GetSurveyResponse.java +++ b/src/main/java/com/dnd/namuiwiki/domain/survey/model/dto/GetSurveyResponse.java @@ -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; @@ -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()) @@ -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 questions) { diff --git a/src/main/java/com/dnd/namuiwiki/domain/survey/model/dto/SingleAnswerWithSurveyDetailDto.java b/src/main/java/com/dnd/namuiwiki/domain/survey/model/dto/SingleAnswerWithSurveyDetailDto.java index 8c9ed927..7581ab84 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/survey/model/dto/SingleAnswerWithSurveyDetailDto.java +++ b/src/main/java/com/dnd/namuiwiki/domain/survey/model/dto/SingleAnswerWithSurveyDetailDto.java @@ -1,5 +1,9 @@ package com.dnd.namuiwiki.domain.survey.model.dto; +import com.dnd.namuiwiki.common.exception.ApplicationErrorException; +import com.dnd.namuiwiki.common.exception.ApplicationErrorType; +import com.dnd.namuiwiki.domain.question.entity.Question; +import com.dnd.namuiwiki.domain.survey.model.entity.Answer; import com.dnd.namuiwiki.domain.survey.type.Period; import com.dnd.namuiwiki.domain.survey.type.Relation; import lombok.Builder; @@ -16,4 +20,28 @@ public class SingleAnswerWithSurveyDetailDto { private LocalDateTime createdAt; private String answer; private String reason; + private String optionName; + + public static class SingleAnswerWithSurveyDetailDtoBuilder { + public SingleAnswerWithSurveyDetailDtoBuilder optionName(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(); + } + } + this.optionName = optionName; + return this; + } + } + }