Skip to content

Commit

Permalink
Merge pull request #106 from dnd-side-project/dev
Browse files Browse the repository at this point in the history
release v0.2.0
  • Loading branch information
eun-seong authored Mar 5, 2024
2 parents d82a8eb + e2adc61 commit 0b4b2d7
Show file tree
Hide file tree
Showing 24 changed files with 408 additions and 488 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public DashboardDto getDashboard(TokenUserInfoDto tokenUserInfoDto, Period perio
new BestWorthDashboardComponent(statistics, getQuestionIdByQuestionNAme(questions, QuestionName.CORE_VALUE)),
new HappyDashboardComponent(statistics, getQuestionIdByQuestionNAme(questions, QuestionName.HAPPY_BEHAVIOR)),
new SadDashboardComponent(statistics, getQuestionIdByQuestionNAme(questions, QuestionName.SAD_ANGRY_BEHAVIOR)),
new CharacterDashboardComponent(statistics, getQuestionIdByQuestionNAme(questions, QuestionName.CHARACTER_CELEBRITY_ASSOCIATION)),
new CharacterDashboardComponent(statistics),
getMoneyDashboardComponent(statistics, period, relation, getQuestionIdByQuestionNAme(questions, QuestionName.BORROWING_LIMIT))
);
return new DashboardDto(dashboardComponents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@

@Getter
public class BestWorthDashboardComponent extends DashboardComponent {
private final String questionId;
private List<RatioDto> rank;

public BestWorthDashboardComponent(Statistics statistics, String questionId) {
super(DashboardType.BEST_WORTH, questionId);
super(DashboardType.BEST_WORTH);
this.questionId = questionId;

calculate(statistics);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,57 @@
import com.dnd.namuiwiki.domain.statistic.model.Statistic;
import com.dnd.namuiwiki.domain.statistic.model.Statistics;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Getter
public class CharacterDashboardComponent extends DashboardComponent {
private List<Character> characters;

private boolean friendly;
private boolean similar;
private boolean mbti;
private boolean busy;

public CharacterDashboardComponent(Statistics statistics, String questionId) {
super(DashboardType.CHARACTER, questionId);
public CharacterDashboardComponent(Statistics statistics) {
super(DashboardType.CHARACTER);
this.characters = new ArrayList<>();
calculate(statistics);
}

@Override
public void calculate(Statistics statistics) {
List<Statistic> character = statistics.getStatisticsByDashboardType(this.dashboardType);
character.forEach(statistic -> {
this.characters = character.stream().map(Character::from).toList();
}

@RequiredArgsConstructor
@Getter
static class Character {
private final String name;
private final boolean value;
private final String questionId;

private static Character from(Statistic statistic) {
RatioStatistic ratioStatistic = (RatioStatistic) statistic;
List<Legend> legends = ratioStatistic.getLegends();
QuestionName questionName = ratioStatistic.getQuestionName();

switch (questionName) {
case FRIENDLINESS_LEVEL:
this.friendly = getLegendByValue(legends, true).getCount() >=
getLegendByValue(legends, false).getCount();
break;
case PERSONALITY_TYPE:
this.similar = getLegendByValue(legends, true).getCount() >=
getLegendByValue(legends, false).getCount();
break;
case MBTI_IMMERSION:
this.mbti = getLegendByValue(legends, true).getCount() >=
getLegendByValue(legends, false).getCount();
break;
case WEEKEND_COMMITMENTS:
this.busy = getLegendByValue(legends, true).getCount() >=
getLegendByValue(legends, false).getCount();
break;
default:
break;
}
});
}
return new Character(
questionName.name(),
getCharacterRatioResult(legends),
statistic.getQuestionId()
);
}

private static Legend getLegendByValue(List<Legend> legends, boolean value) {
return legends.stream()
.filter(legend -> value == (boolean) legend.getValue())
.findFirst()
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INTERNAL_ERROR));
}

private Legend getLegendByValue(List<Legend> legends, boolean value) {
return legends.stream()
.filter(legend -> value == (boolean) legend.getValue())
.findFirst().orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INTERNAL_ERROR));
private static boolean getCharacterRatioResult(List<Legend> legends) {
return getLegendByValue(legends, true).getCount() >=
getLegendByValue(legends, false).getCount();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
public abstract class DashboardComponent {

protected final DashboardType dashboardType;
protected final String questionId;

public abstract void calculate(Statistics statistics);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
@Getter
public class HappyDashboardComponent extends DashboardComponent {
private List<RatioDto> rank;
private final String questionId;

public HappyDashboardComponent(Statistics statistics, String questionId) {
super(DashboardType.HAPPY, questionId);
super(DashboardType.HAPPY);
this.questionId = questionId;

calculate(statistics);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

@Getter
public class MoneyDashboardComponent extends DashboardComponent {
private final String questionId;
private long peopleCount;
private long average;
private long entireAverage;

public MoneyDashboardComponent(Statistics statistics, long entireAverage, String questionId) {
super(DashboardType.MONEY, questionId);
calculate(statistics);
super(DashboardType.MONEY);
this.entireAverage = entireAverage;
this.questionId = questionId;

calculate(statistics);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@

@Getter
public class SadDashboardComponent extends DashboardComponent {
private final String questionId;
private List<RatioDto> rank;

public SadDashboardComponent(Statistics statistics, String questionId) {
super(DashboardType.SAD, questionId);
super(DashboardType.SAD);
this.questionId = questionId;

calculate(statistics);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.dnd.namuiwiki.common.model.BaseTimeEntity;
import com.dnd.namuiwiki.domain.statistic.model.Statistics;
import com.dnd.namuiwiki.domain.survey.model.entity.Survey;
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.user.entity.User;
Expand Down Expand Up @@ -31,7 +31,7 @@ public class Dashboard extends BaseTimeEntity {

private Statistics statistics;

public void updateStatistics(List<Survey.Answer> answer) {
public void updateStatistics(List<Answer> answer) {
statistics.updateStatistics(answer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.dnd.namuiwiki.domain.statistic.model.BorrowingLimitEntireStatistic;
import com.dnd.namuiwiki.domain.statistic.model.Statistics;
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;
Expand Down Expand Up @@ -49,29 +50,29 @@ public PopulationStatistic getPopulationStatistic(Period period, Relation relati
.build());
}

private void updateDashboards(User owner, Period period, Relation relation, List<Survey.Answer> statisticalAnswers) {
private void updateDashboards(User owner, Period period, Relation relation, List<Answer> statisticalAnswers) {
updateDashboardByCategory(owner, null, null, statisticalAnswers);
updateDashboardByCategory(owner, period, null, statisticalAnswers);
updateDashboardByCategory(owner, null, relation, statisticalAnswers);
}

private void updateDashboardByCategory(User owner, Period period, Relation relation, List<Survey.Answer> answers) {
private void updateDashboardByCategory(User owner, Period period, Relation relation, List<Answer> answers) {
Dashboard dashboard = dashboardRepository.findByUserAndPeriodAndRelation(owner, period, relation)
.orElseGet(() -> {
Dashboard newDashboard = Dashboard.builder()
.user(owner)
.period(period)
.relation(relation)
.statistics(Statistics.from(answers.stream().map(Survey.Answer::getQuestion).toList()))
.statistics(Statistics.from(answers.stream().map(Answer::getQuestion).toList()))
.build();
return dashboardRepository.save(newDashboard);
});
dashboard.updateStatistics(answers);
dashboardRepository.save(dashboard);
}

private void updateBorrowingLimitStatistic(Period period, Relation relation, List<Survey.Answer> answers) {
Survey.Answer borroingLimitAnswer = answers.stream()
private void updateBorrowingLimitStatistic(Period period, Relation relation, List<Answer> answers) {
Answer borroingLimitAnswer = answers.stream()
.filter(answer -> answer.getQuestion().getName() == QuestionName.BORROWING_LIMIT)
.findFirst()
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_DATA_ARGUMENT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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.Survey;
import com.dnd.namuiwiki.domain.survey.model.entity.Answer;
import lombok.Getter;

@Getter
Expand All @@ -26,7 +26,7 @@ public AverageStatistic(
}

@Override
public void updateStatistic(Survey.Answer answer) {
public void updateStatistic(Answer answer) {
long value;
Question question = answer.getQuestion();
switch (answer.getType()) {
Expand Down Expand Up @@ -57,13 +57,13 @@ public static AverageStatistic create(Question question) {
0L);
}

private long getValueFromManualAnswer(Survey.Answer answer) {
private long getValueFromManualAnswer(Answer answer) {
long value;
value = Long.parseLong(answer.getAnswer().toString());
return value;
}

private long getValueFromOption(Survey.Answer answer, Question question) {
private long getValueFromOption(Answer answer, Question question) {
long value;
String optionId = answer.getAnswer().toString();
Option option = question.getOption(optionId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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.Survey;
import com.dnd.namuiwiki.domain.survey.model.entity.Answer;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -47,7 +47,7 @@ public static RatioStatistic create(Question question) {
}

@Override
public void updateStatistic(Survey.Answer answer) {
public void updateStatistic(Answer answer) {
if (answer.getType().isManual()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.dnd.namuiwiki.domain.question.entity.Question;
import com.dnd.namuiwiki.domain.question.type.QuestionName;
import com.dnd.namuiwiki.domain.statistic.type.StatisticsType;
import com.dnd.namuiwiki.domain.survey.model.entity.Survey;
import com.dnd.namuiwiki.domain.survey.model.entity.Answer;
import lombok.AllArgsConstructor;
import lombok.Getter;

Expand All @@ -33,6 +33,6 @@ public static Statistic create(Question question, StatisticsType statisticsType)
}
}

public abstract void updateStatistic(Survey.Answer answer);
public abstract void updateStatistic(Answer answer);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.dnd.namuiwiki.domain.dashboard.type.DashboardType;
import com.dnd.namuiwiki.domain.question.entity.Question;
import com.dnd.namuiwiki.domain.statistic.type.StatisticsType;
import com.dnd.namuiwiki.domain.survey.model.entity.Survey;
import com.dnd.namuiwiki.domain.survey.model.entity.Answer;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

Expand All @@ -28,7 +28,7 @@ public Optional<Statistic> get(String questionId) {
return Optional.ofNullable(statistics.get(questionId));
}

public void updateStatistics(List<Survey.Answer> answers) {
public void updateStatistics(List<Answer> answers) {
answers.forEach(answer -> {
Question question = answer.getQuestion();
Statistic statistic = get(question.getId()).orElseGet(() -> createAndPut(question));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
@RequestMapping("/api/v1/surveys")
public class SurveyController {
private final SurveyService surveyService;
private final SurveyAnswerService surveyAnswerService;

@Operation(summary = "설문 생성", responses = {
@ApiResponse(responseCode = "201", description = "설문 생성 성공", content = @Content(schema = @Schema(implementation = CreateSurveyResponse.class)))
Expand All @@ -38,8 +37,7 @@ public ResponseEntity<?> createSurvey(
@Validated @RequestBody CreateSurveyRequest request,
@RequestHeader(required = false, value = "X-NAMUIWIKI-TOKEN") String accessToken
) {
var surveyAnswers = surveyAnswerService.getSurveyAnswers(request.getAnswers());
var response = surveyService.createSurvey(request, surveyAnswers, accessToken);
var response = surveyService.createSurvey(request, accessToken);
return ResponseDto.created(response);
}

Expand Down
Loading

0 comments on commit 0b4b2d7

Please sign in to comment.