Skip to content

Commit

Permalink
feat: 대시보드 조회 시 사용자 응답 타입을 Object로 변경(#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
eun-seong committed Sep 10, 2024
1 parent 2c55de1 commit 5b1c65c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public boolean containsAnswerType(AnswerType answerType) {
}

public boolean isChoiceType() {
return this == MULTIPLE_CHOICE || this == OX || this == NUMERIC_CHOICE || this == RANK;
return this == MULTIPLE_CHOICE || this == OX || this == NUMERIC_CHOICE;
}

public boolean isNumericType() {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dnd.namuiwiki.common.dto.PageableDto;
import com.dnd.namuiwiki.common.exception.ApplicationErrorException;
import com.dnd.namuiwiki.common.exception.ApplicationErrorType;
import com.dnd.namuiwiki.common.util.ListUtils;
import com.dnd.namuiwiki.domain.jwt.JwtService;
import com.dnd.namuiwiki.domain.jwt.dto.TokenUserInfoDto;
import com.dnd.namuiwiki.domain.option.OptionRepository;
Expand Down Expand Up @@ -225,7 +226,16 @@ private Object convertAnswer(Question question, Answer answer) {
return answer.getAnswer();
}

Option option = question.getOption(answer.getAnswer().toString())
if (question.getType().isListType()) {
return (ListUtils.convertList(answer.getAnswer()).stream()
.map(optionId -> getOptionValue(question, optionId)).toList());
}

return getOptionValue(question, answer.getAnswer());
}

private Object getOptionValue(Question question, Object answer) {
Option option = question.getOption(answer.toString())
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_OPTION_ID));
if (question.getType().isNumericType()) {
return option.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dnd.namuiwiki.common.exception.ApplicationErrorException;
import com.dnd.namuiwiki.common.exception.ApplicationErrorType;
import com.dnd.namuiwiki.common.util.ListUtils;
import com.dnd.namuiwiki.domain.option.entity.Option;
import com.dnd.namuiwiki.domain.question.entity.Question;
import com.dnd.namuiwiki.domain.question.type.QuestionName;
Expand Down Expand Up @@ -33,55 +34,46 @@ public class GetSurveyResponse {
@AllArgsConstructor
private static class SingleQuestionAndAnswer {
private String questionTitle;
private String text;
private Object value;
private Object answer;
private String reason;
private QuestionName questionName;
private String optionName;

static SingleQuestionAndAnswer from(Question question, Answer surveyAnswer) {
String optionName = getOptionName(question, surveyAnswer);
Object answer = convertAnswerToObject(question, surveyAnswer);

if (surveyAnswer.getType().isManual()) {
return new SingleQuestionAndAnswer(
question.getTitle(),
surveyAnswer.getAnswer().toString(),
surveyAnswer.getAnswer(),
surveyAnswer.getReason(),
question.getName(),
optionName
);
}
Option option = question.getOption(surveyAnswer.getAnswer().toString())
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_OPTION_ID));
return new SingleQuestionAndAnswer(
question.getTitle(),
option.getText(),
option.getValue(),
answer,
surveyAnswer.getReason(),
question.getName(),
optionName
question.getName()
);
}

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();
}
private static Object convertAnswerToObject(Question question, Answer surveyAnswer) {
Object optionValue;
if (surveyAnswer.getType().isManual()) {
optionValue = new SimpleAnswerDto(
surveyAnswer.getAnswer().toString(),
surveyAnswer.getAnswer(),
null);
} else if (question.getType().isListType()) {
optionValue = (ListUtils.<String>convertList(surveyAnswer.getAnswer()).stream()
.map(optionId -> SimpleAnswerDto.of(getOption(question, optionId)))
.toList());
} else if (question.getType().isChoiceType()) {
Option option = getOption(question, surveyAnswer.getAnswer().toString());
optionValue = SimpleAnswerDto.of(option);
} else {
throw new ApplicationErrorException(ApplicationErrorType.INVALID_DATA_ARGUMENT, "선택형 질문이 아닙니다");
}
return optionName;
return optionValue;
}

private static Option getOption(Question question, String optionId) {
return question.getOption(optionId)
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_OPTION_ID));
}

}

public static GetSurveyResponse from(Survey survey, List<Question> questions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.dnd.namuiwiki.domain.survey.model.dto;

import com.dnd.namuiwiki.domain.option.entity.Option;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class SimpleAnswerDto {
private final String text;
private final Object value;
private final String optionName;

public static SimpleAnswerDto of(Option option) {
return new SimpleAnswerDto(option.getText(), option.getValue(), option.getName());
}
}

0 comments on commit 5b1c65c

Please sign in to comment.