From f527ca3f06edb6f2c76486b6917c1b88d48b2e48 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Thu, 7 Mar 2024 02:23:25 +0900 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20PopulationStatistic=EC=9D=98=20?= =?UTF-8?q?DashboareType=EC=97=90=20=EB=94=B0=EB=9D=BC=20DashboareComponen?= =?UTF-8?q?t=20=EC=83=9D=EC=84=B1=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95(#114)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/dashboard/DashboardService.java | 8 +-- .../dashboard/model/entity/Dashboard.java | 12 ++-- .../statistic/StatisticsRepository.java | 3 + .../domain/statistic/StatisticsService.java | 65 +++++++++---------- .../model/entity/PopulationStatistic.java | 7 ++ .../type/StatisticsCalculationType.java | 8 +++ .../domain/survey/model/entity/Answer.java | 30 +++++++++ 7 files changed, 88 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java index dd306bc0..22a69647 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java @@ -5,9 +5,7 @@ import com.dnd.namuiwiki.domain.dashboard.model.DashboardComponent; import com.dnd.namuiwiki.domain.dashboard.model.dto.DashboardDto; import com.dnd.namuiwiki.domain.dashboard.model.entity.Dashboard; -import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; import com.dnd.namuiwiki.domain.jwt.dto.TokenUserInfoDto; -import com.dnd.namuiwiki.domain.question.type.QuestionName; import com.dnd.namuiwiki.domain.statistic.StatisticsService; import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; import com.dnd.namuiwiki.domain.survey.model.entity.Answer; @@ -40,12 +38,12 @@ public DashboardDto getDashboard(TokenUserInfoDto tokenUserInfoDto, Period perio } Dashboard dashboard = optionalDashboard.get(); + List populationStatistics = statisticsService.getPopulationStatistics(period, relation); + List populationDashboards = dashboard.getPopulationDashboards(populationStatistics); List userDashboards = dashboard.getUserDashboards(); - PopulationStatistic populationStatistic = statisticsService.getPopulationStatistic(period, relation, QuestionName.BORROWING_LIMIT); - DashboardComponent populationDashboard = dashboard.getPopulationDashboard(populationStatistic, DashboardType.MONEY); List components = new ArrayList<>(userDashboards); - components.add(populationDashboard); + components.addAll(populationDashboards); return new DashboardDto(components); } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java index d706245d..4145c7c5 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java @@ -49,10 +49,14 @@ public List getUserDashboards() { .toList(); } - public DashboardComponent getPopulationDashboard(PopulationStatistic populationStatistic, DashboardType dashboardType) { - List statisticsByDashboardType = statistics.getStatisticsByDashboardType(dashboardType); - - return DashboardFactory.create(dashboardType, statisticsByDashboardType, populationStatistic); + public List getPopulationDashboards(List populationStatistics) { + return populationStatistics.stream() + .map(populationStatistic -> { + DashboardType dashboardType = populationStatistic.getDashboardType(); + List statisticsByDashboardType = statistics.getStatisticsByDashboardType(dashboardType); + return DashboardFactory.create(dashboardType, statisticsByDashboardType, populationStatistic); + }) + .toList(); } public static Dashboard createNew(User owner, Period period, Relation relation, List answers) { diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsRepository.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsRepository.java index f41a277c..5b947731 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsRepository.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsRepository.java @@ -6,8 +6,11 @@ import com.dnd.namuiwiki.domain.survey.type.Relation; import org.springframework.data.mongodb.repository.MongoRepository; +import java.util.List; import java.util.Optional; public interface StatisticsRepository extends MongoRepository { Optional findByPeriodAndRelationAndQuestionName(Period period, Relation relation, QuestionName name); + + List findAllByPeriodAndRelation(Period period, Relation relation); } diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java index 1ec2d994..1af895b9 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java @@ -1,11 +1,10 @@ package com.dnd.namuiwiki.domain.statistic; -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.dashboard.type.DashboardType; import com.dnd.namuiwiki.domain.question.type.QuestionName; import com.dnd.namuiwiki.domain.statistic.model.AverageEntireStatistic; import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; +import com.dnd.namuiwiki.domain.statistic.type.StatisticsCalculationType; 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.Period; @@ -24,14 +23,34 @@ public void updateStatistics(Survey survey) { Period period = survey.getPeriod(); Relation relation = survey.getRelation(); - var statisticalAnswers = survey.getAnswers().stream() - .filter(answer -> answer.getQuestion().getDashboardType().getStatisticsCalculationType().isNotNone()) - .toList(); + survey.getAnswers().stream() + .filter(answer -> answer.getQuestion().getDashboardType().getAnalysisType().isPopulation()) + .forEach(answer -> { + QuestionName questionName = answer.getQuestion().getName(); + DashboardType dashboardType = answer.getQuestion().getDashboardType(); + StatisticsCalculationType calculationType = dashboardType.getStatisticsCalculationType(); + if (calculationType.isAverage()) { + updateAverageStatistic(questionName, period, relation, answer); + } + }); + } + + private void updateAverageStatistic(QuestionName questionName, Period period, Relation relation, Answer answer) { + long newValue = answer.getAnswer(Long.class); - updateBorrowingLimitStatistic(period, relation, statisticalAnswers); + updateAverageStatisticByCategory(questionName, null, null, newValue); + updateAverageStatisticByCategory(questionName, period, null, newValue); + updateAverageStatisticByCategory(questionName, null, relation, newValue); } - public PopulationStatistic getPopulationStatistic(Period period, Relation relation, QuestionName questionName) { + private void updateAverageStatisticByCategory(QuestionName questionName, Period period, Relation relation, long newValue) { + PopulationStatistic populationStatistic = getPopulationStatistic(period, relation, questionName); + populationStatistic.updateStatistic(String.valueOf(newValue)); + + statisticsRepository.save(populationStatistic); + } + + private PopulationStatistic getPopulationStatistic(Period period, Relation relation, QuestionName questionName) { return statisticsRepository .findByPeriodAndRelationAndQuestionName(period, relation, questionName) .orElseGet(() -> PopulationStatistic.builder() @@ -42,34 +61,8 @@ public PopulationStatistic getPopulationStatistic(Period period, Relation relati .build()); } - private void updateBorrowingLimitStatistic(Period period, Relation relation, List answers) { - Answer borroingLimitAnswer = answers.stream() - .filter(answer -> answer.getQuestion().getName() == QuestionName.BORROWING_LIMIT) - .findFirst() - .orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_DATA_ARGUMENT)); - - long borrowingLimit; - if (borroingLimitAnswer.getType().isOption()) { - Option option = borroingLimitAnswer.getQuestion().getOptions().get(borroingLimitAnswer.getAnswer().toString()); - borrowingLimit = (long) option.getValue(); - } else { - borrowingLimit = (long) borroingLimitAnswer.getAnswer(); - } - - updateBorrowingLimitStatisticByCategory(null, null, borrowingLimit); - updateBorrowingLimitStatisticByCategory(period, null, borrowingLimit); - updateBorrowingLimitStatisticByCategory(null, relation, borrowingLimit); - } - - private void updateBorrowingLimitStatisticByCategory(Period period, Relation relation, long borrowingLimit) { - PopulationStatistic populationStatistic = getPopulationStatistic(period, relation, QuestionName.BORROWING_LIMIT); - - AverageEntireStatistic statistic = (AverageEntireStatistic) populationStatistic.getStatistic(); - statistic.updateStatistic(String.valueOf(borrowingLimit)); - - populationStatistic.setStatistic(statistic); - - statisticsRepository.save(populationStatistic); + public List getPopulationStatistics(Period period, Relation relation) { + return statisticsRepository.findAllByPeriodAndRelation(period, relation); } } diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/entity/PopulationStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/entity/PopulationStatistic.java index 6ba829cf..4144c429 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/entity/PopulationStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/entity/PopulationStatistic.java @@ -1,5 +1,6 @@ package com.dnd.namuiwiki.domain.statistic.model.entity; +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; import com.dnd.namuiwiki.domain.question.type.QuestionName; import com.dnd.namuiwiki.domain.statistic.model.EntireStatistic; import com.dnd.namuiwiki.domain.survey.type.Period; @@ -25,6 +26,12 @@ public class PopulationStatistic { private QuestionName questionName; + private DashboardType dashboardType; + private EntireStatistic statistic; + public void updateStatistic(String... args) { + statistic.updateStatistic(args); + } + } diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/type/StatisticsCalculationType.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/type/StatisticsCalculationType.java index bd8a742e..a8625ae9 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/type/StatisticsCalculationType.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/type/StatisticsCalculationType.java @@ -10,4 +10,12 @@ public boolean isNotNone() { return this != NONE; } + public boolean isRatio() { + return this == RATIO; + } + + public boolean isAverage() { + return this == AVERAGE; + } + } diff --git a/src/main/java/com/dnd/namuiwiki/domain/survey/model/entity/Answer.java b/src/main/java/com/dnd/namuiwiki/domain/survey/model/entity/Answer.java index ddf7b2f6..0a4ca8f3 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/survey/model/entity/Answer.java +++ b/src/main/java/com/dnd/namuiwiki/domain/survey/model/entity/Answer.java @@ -2,6 +2,7 @@ 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.QuestionType; import com.dnd.namuiwiki.domain.survey.type.AnswerType; @@ -42,6 +43,35 @@ public Answer(Question question, AnswerType type, Object answer, String reason) this.reason = reason; } + public T getAnswer(Class returnType) { + Object value; + + if (type.isOption()) { + Option option = question.getOptions().get(answer.toString()); + value = option.getValue(); + } else { + value = answer; + } + + if (value == null) { + throw new ApplicationErrorException(ApplicationErrorType.INTERNAL_ERROR); + } + + if (returnType == Long.class) { + if (isNumericAnswerNeeded()) { + return (T) value; + } else { + throw new ApplicationErrorException(ApplicationErrorType.NOT_INTEGER_ANSWER); + } + } + + return returnType.cast(value); + } + + private boolean isNumericAnswerNeeded() { + return question.getType().isNumericType() && type.isManual(); + } + private void validateBorrowingLimit(long longAnswer) { if (!(0 <= longAnswer && longAnswer <= 1_000_000_000)) { throw new ApplicationErrorException(ApplicationErrorType.INVALID_BORROWING_LIMIT); From 4f6d805039ea7ea772fc2220f68f1803dd9dacba Mon Sep 17 00:00:00 2001 From: eun-seong Date: Thu, 7 Mar 2024 02:57:29 +0900 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20EntireStatistic=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=EC=B2=B4=20=EC=83=9D=EC=84=B1=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80(#114)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/statistic/StatisticsService.java | 36 +++++++------------ .../model/AverageEntireStatistic.java | 2 +- .../statistic/model/EntireStatistic.java | 16 +++++++-- .../model/entity/PopulationStatistic.java | 12 +++++-- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java index 1af895b9..19be666d 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java @@ -1,10 +1,7 @@ package com.dnd.namuiwiki.domain.statistic; -import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import com.dnd.namuiwiki.domain.question.type.QuestionName; -import com.dnd.namuiwiki.domain.statistic.model.AverageEntireStatistic; +import com.dnd.namuiwiki.domain.question.entity.Question; import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; -import com.dnd.namuiwiki.domain.statistic.type.StatisticsCalculationType; 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.Period; @@ -26,39 +23,32 @@ public void updateStatistics(Survey survey) { survey.getAnswers().stream() .filter(answer -> answer.getQuestion().getDashboardType().getAnalysisType().isPopulation()) .forEach(answer -> { - QuestionName questionName = answer.getQuestion().getName(); - DashboardType dashboardType = answer.getQuestion().getDashboardType(); - StatisticsCalculationType calculationType = dashboardType.getStatisticsCalculationType(); + Question question = answer.getQuestion(); + var calculationType = question.getDashboardType().getStatisticsCalculationType(); if (calculationType.isAverage()) { - updateAverageStatistic(questionName, period, relation, answer); + updateAverageStatistic(question, period, relation, answer); } }); } - private void updateAverageStatistic(QuestionName questionName, Period period, Relation relation, Answer answer) { + private void updateAverageStatistic(Question question, Period period, Relation relation, Answer answer) { long newValue = answer.getAnswer(Long.class); - updateAverageStatisticByCategory(questionName, null, null, newValue); - updateAverageStatisticByCategory(questionName, period, null, newValue); - updateAverageStatisticByCategory(questionName, null, relation, newValue); + updateAverageStatisticByCategory(question, null, null, newValue); + updateAverageStatisticByCategory(question, period, null, newValue); + updateAverageStatisticByCategory(question, null, relation, newValue); } - private void updateAverageStatisticByCategory(QuestionName questionName, Period period, Relation relation, long newValue) { - PopulationStatistic populationStatistic = getPopulationStatistic(period, relation, questionName); + private void updateAverageStatisticByCategory(Question question, Period period, Relation relation, long newValue) { + PopulationStatistic populationStatistic = getPopulationStatistic(question, period, relation); populationStatistic.updateStatistic(String.valueOf(newValue)); statisticsRepository.save(populationStatistic); } - private PopulationStatistic getPopulationStatistic(Period period, Relation relation, QuestionName questionName) { - return statisticsRepository - .findByPeriodAndRelationAndQuestionName(period, relation, questionName) - .orElseGet(() -> PopulationStatistic.builder() - .statistic(new AverageEntireStatistic(0L, 0L)) - .period(period) - .questionName(questionName) - .relation(relation) - .build()); + private PopulationStatistic getPopulationStatistic(Question question, Period period, Relation relation) { + return statisticsRepository.findByPeriodAndRelationAndQuestionName(period, relation, question.getName()) + .orElseGet(() -> PopulationStatistic.from(period, relation, question.getName(), question.getDashboardType())); } public List getPopulationStatistics(Period period, Relation relation) { diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageEntireStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageEntireStatistic.java index 3e1d6cd2..bfb2dbbf 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageEntireStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageEntireStatistic.java @@ -6,7 +6,7 @@ @Getter @AllArgsConstructor -public class AverageEntireStatistic implements EntireStatistic { +public class AverageEntireStatistic extends EntireStatistic { private long entireAverage; private long peopleCount; diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/EntireStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/EntireStatistic.java index 6fbd2a55..61238167 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/EntireStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/EntireStatistic.java @@ -1,5 +1,17 @@ package com.dnd.namuiwiki.domain.statistic.model; -public interface EntireStatistic { - void updateStatistic(String... arguments); +import com.dnd.namuiwiki.domain.statistic.type.StatisticsCalculationType; + +public abstract class EntireStatistic { + public abstract void updateStatistic(String... arguments); + + public static EntireStatistic createEmpty(StatisticsCalculationType type) { + switch (type) { + case AVERAGE: + return new AverageEntireStatistic(0L, 0L); + default: + throw new IllegalArgumentException("Not supported type: " + type); + } + } + } diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/entity/PopulationStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/entity/PopulationStatistic.java index 4144c429..fd908b8d 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/entity/PopulationStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/entity/PopulationStatistic.java @@ -7,12 +7,10 @@ import com.dnd.namuiwiki.domain.survey.type.Relation; import lombok.Builder; import lombok.Getter; -import lombok.Setter; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Getter -@Setter @Builder @Document("statistics") public class PopulationStatistic { @@ -34,4 +32,14 @@ public void updateStatistic(String... args) { statistic.updateStatistic(args); } + public static PopulationStatistic from(Period period, Relation relation, QuestionName questionName, DashboardType dashboardType) { + return PopulationStatistic.builder() + .statistic(EntireStatistic.createEmpty(dashboardType.getStatisticsCalculationType())) + .dashboardType(dashboardType) + .period(period) + .questionName(questionName) + .relation(relation) + .build(); + } + } From ba04562f58cfdf6441282af8c4d7ecf123be33b4 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Thu, 7 Mar 2024 03:09:25 +0900 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20Answer=EB=A5=BC=20long=20?= =?UTF-8?q?=EA=B0=92=EC=9C=BC=EB=A1=9C=20=EA=B5=90=EC=B2=B4(#114)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dnd/namuiwiki/domain/statistic/StatisticsService.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java index 19be666d..0e710c21 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java @@ -2,7 +2,6 @@ import com.dnd.namuiwiki.domain.question.entity.Question; import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; -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.Period; import com.dnd.namuiwiki.domain.survey.type.Relation; @@ -26,14 +25,12 @@ public void updateStatistics(Survey survey) { Question question = answer.getQuestion(); var calculationType = question.getDashboardType().getStatisticsCalculationType(); if (calculationType.isAverage()) { - updateAverageStatistic(question, period, relation, answer); + updateAverageStatistic(question, period, relation, answer.getAnswer(Long.class)); } }); } - private void updateAverageStatistic(Question question, Period period, Relation relation, Answer answer) { - long newValue = answer.getAnswer(Long.class); - + private void updateAverageStatistic(Question question, Period period, Relation relation, long newValue) { updateAverageStatisticByCategory(question, null, null, newValue); updateAverageStatisticByCategory(question, period, null, newValue); updateAverageStatisticByCategory(question, null, relation, newValue); From 93fba972729ed78a17ae4dddece8615e59fa7211 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Tue, 12 Mar 2024 19:11:08 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20Answer=20casting=20=EC=9E=98?= =?UTF-8?q?=EB=AA=BB=EB=90=98=EC=97=88=EC=9D=84=20=EA=B2=BD=EC=9A=B0=20?= =?UTF-8?q?=EB=A1=9C=EA=B9=85=20=EC=B2=98=EB=A6=AC(#114)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/exception/ApplicationErrorType.java | 1 + .../domain/survey/model/entity/Answer.java | 16 +++++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/common/exception/ApplicationErrorType.java b/src/main/java/com/dnd/namuiwiki/common/exception/ApplicationErrorType.java index bbc53462..cfa34202 100644 --- a/src/main/java/com/dnd/namuiwiki/common/exception/ApplicationErrorType.java +++ b/src/main/java/com/dnd/namuiwiki/common/exception/ApplicationErrorType.java @@ -63,6 +63,7 @@ public enum ApplicationErrorType { QUESTION_ANSWER_COUNT_NOT_EQUAL(HttpStatus.INTERNAL_SERVER_ERROR, "문항과 답변의 개수가 일치하지 않습니다."), INVALID_BORROWING_LIMIT(HttpStatus.BAD_REQUEST, "빌릴 수 있는 돈은 0 이상 10억 이하입니다."), + INVALID_TYPE_CAST(HttpStatus.CONFLICT, "타입 캐스팅에 실패하였습니다."), /** * Filter Error Type diff --git a/src/main/java/com/dnd/namuiwiki/domain/survey/model/entity/Answer.java b/src/main/java/com/dnd/namuiwiki/domain/survey/model/entity/Answer.java index 0a4ca8f3..2a046491 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/survey/model/entity/Answer.java +++ b/src/main/java/com/dnd/namuiwiki/domain/survey/model/entity/Answer.java @@ -6,13 +6,13 @@ import com.dnd.namuiwiki.domain.question.entity.Question; import com.dnd.namuiwiki.domain.question.type.QuestionType; import com.dnd.namuiwiki.domain.survey.type.AnswerType; -import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.data.mongodb.core.mapping.DocumentReference; +@Slf4j @Getter -@Builder @NoArgsConstructor public class Answer { @@ -57,15 +57,13 @@ public T getAnswer(Class returnType) { throw new ApplicationErrorException(ApplicationErrorType.INTERNAL_ERROR); } - if (returnType == Long.class) { - if (isNumericAnswerNeeded()) { - return (T) value; - } else { - throw new ApplicationErrorException(ApplicationErrorType.NOT_INTEGER_ANSWER); - } + try { + return returnType.cast(value); + } catch (ClassCastException e) { + log.error("Answer.getAnswer value: {}, valueType: {}, returnType: {}", value, value.getClass(), returnType, e); + throw new ApplicationErrorException(ApplicationErrorType.INVALID_TYPE_CAST); } - return returnType.cast(value); } private boolean isNumericAnswerNeeded() {