Skip to content

Commit

Permalink
Merge pull request #199 from dnd-side-project/feat/#188
Browse files Browse the repository at this point in the history
RANK νƒ€μž… λŒ€μ‹œλ³΄λ“œ 응닡 νƒ€μž… μˆ˜μ •
  • Loading branch information
eun-seong authored Sep 10, 2024
2 parents 23bdb1e + 7110056 commit bad814f
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 99 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/dnd/namuiwiki/common/util/ListUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.dnd.namuiwiki.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class ListUtils {
public static <T> List<T> convertList(Object obj) {
if (obj.getClass().isArray()) {
return Arrays.asList((T[]) obj);
} else if (obj instanceof Collection) {
return new ArrayList<>((Collection<T>) obj);
} else {
throw new ClassCastException("Object is not a List.");
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dnd.namuiwiki.domain.question.entity;

import com.dnd.namuiwiki.common.exception.ApplicationErrorException;
import com.dnd.namuiwiki.common.exception.ApplicationErrorType;
import com.dnd.namuiwiki.common.model.BaseTimeEntity;
import com.dnd.namuiwiki.domain.dashboard.type.DashboardType;
import com.dnd.namuiwiki.domain.option.entity.Option;
Expand All @@ -13,7 +15,6 @@
import org.springframework.data.mongodb.core.mapping.DocumentReference;

import java.util.Map;
import java.util.Optional;

@Getter
@Builder
Expand All @@ -34,8 +35,11 @@ public class Question extends BaseTimeEntity {
@DocumentReference(collection = "options")
private Map<String, Option> options;

public Optional<Option> getOption(String optionId) {
return Optional.ofNullable(options.get(optionId));
public Option getOption(String optionId) {
if (!options.containsKey(optionId)) {
throw new ApplicationErrorException(ApplicationErrorType.INVALID_OPTION_ID);
}
return options.get(optionId);
}

}
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ private long getValueFromManualAnswer(Answer answer) {
private long getValueFromOption(Answer answer, Question question) {
long value;
String optionId = answer.getAnswer().toString();
Option option = question.getOption(optionId)
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_OPTION_ID));
Option option = question.getOption(optionId);
value = Long.parseLong(option.getValue().toString());
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ private void increaseOptionCount(Answer answer) {
String optionId = answer.getAnswer().toString();
Legend legend = getLegend(optionId)
.orElseGet(() -> {
Option option = question.getOption(optionId)
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_OPTION_ID));
Option option = question.getOption(optionId);
return new Legend(option.getId(), option.getText(), option.getValue(), 0L);
});
legend.increaseCount();
Expand Down
19 changes: 13 additions & 6 deletions 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 @@ -153,8 +154,7 @@ public GetSurveyResponse getSurvey(String surveyId, TokenUserInfoDto tokenUserIn
User user = getUserByWikiId(tokenUserInfoDto.getWikiId());
validateSurveyOwner(survey, user);

List<Question> questions = questionRepository.findAll();
return GetSurveyResponse.from(survey, questions);
return GetSurveyResponse.from(survey);
}

private void validateSurveyOwner(Survey survey, User user) {
Expand Down Expand Up @@ -185,9 +185,8 @@ public GetAnswersByQuestionResponse getAnswersByQuestion(
.period(survey.getPeriod())
.relation(survey.getRelation())
.createdAt(survey.getWrittenAt())
.answer(convertAnswer(question, answer))
.answer(answer.convertToObject())
.reason(answer.getReason())
.optionName(question, answer)
.wikiType(survey.getWikiType())
.build()).orElse(null));

Expand Down Expand Up @@ -225,8 +224,16 @@ private Object convertAnswer(Question question, Answer answer) {
return answer.getAnswer();
}

Option option = question.getOption(answer.getAnswer().toString())
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_OPTION_ID));
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());
if (question.getType().isNumericType()) {
return option.getValue();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
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.option.entity.Option;
import com.dnd.namuiwiki.domain.question.entity.Question;
import com.dnd.namuiwiki.domain.question.type.QuestionName;
import com.dnd.namuiwiki.domain.survey.model.entity.Answer;
Expand All @@ -14,10 +11,7 @@
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Getter
@AllArgsConstructor
Expand All @@ -33,70 +27,33 @@ 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);
static SingleQuestionAndAnswer from(Answer surveyAnswer) {
Question question = surveyAnswer.getQuestion();

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(),
surveyAnswer.convertToObject(),
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();
}
}
return optionName;
}
}

public static GetSurveyResponse from(Survey survey, List<Question> questions) {
var singleQuestionAndAnswers = pairQuestionAndAnswer(survey, questions);
return new GetSurveyResponse(survey.getSenderName(), survey.getWikiType(), survey.getPeriod(), survey.getRelation(), survey.getWrittenAt(), singleQuestionAndAnswers);
public static GetSurveyResponse from(Survey survey) {
return new GetSurveyResponse(
survey.getSenderName(),
survey.getWikiType(),
survey.getPeriod(),
survey.getRelation(),
survey.getWrittenAt(),
survey.getAnswers().stream().map(SingleQuestionAndAnswer::from).toList()
);
}

private static List<SingleQuestionAndAnswer> pairQuestionAndAnswer(Survey survey, List<Question> questions) {
Map<String, Question> questionMap = new HashMap<>();
questions.forEach(question -> questionMap.put(question.getId(), question));

var answers = survey.getAnswers();
List<SingleQuestionAndAnswer> questionAndAnswerList = new ArrayList<>();
answers.forEach(answer -> questionAndAnswerList.add(SingleQuestionAndAnswer.from(questionMap.get(answer.getQuestion().getId()), answer)));
return questionAndAnswerList;
}

}
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());
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
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 com.dnd.namuiwiki.domain.wiki.WikiType;
Expand All @@ -21,29 +17,5 @@ public class SingleAnswerWithSurveyDetailDto {
private LocalDateTime createdAt;
private Object answer;
private String reason;
private String optionName;
private WikiType wikiType;

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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

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.QuestionType;
import com.dnd.namuiwiki.domain.survey.model.dto.SimpleAnswerDto;
import com.dnd.namuiwiki.domain.survey.type.AnswerType;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -73,4 +76,24 @@ private void validateAnswerType(QuestionType questionType, AnswerType answerType
}
}

public Object convertToObject() {
Object optionValue;
if (getType().isManual()) {
optionValue = new SimpleAnswerDto(
getAnswer().toString(),
getAnswer(),
null);
} else if (question.getType().isListType()) {
optionValue = (ListUtils.<String>convertList(getAnswer()).stream()
.map(optionId -> SimpleAnswerDto.of(question.getOption(optionId)))
.toList());
} else if (question.getType().isChoiceType()) {
Option option = question.getOption(getAnswer().toString());
optionValue = SimpleAnswerDto.of(option);
} else {
throw new ApplicationErrorException(ApplicationErrorType.INVALID_DATA_ARGUMENT, "μ„ νƒν˜• 질문이 μ•„λ‹™λ‹ˆλ‹€");
}
return optionValue;
}

}

0 comments on commit bad814f

Please sign in to comment.