Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solved quiz count data to response #195

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,41 @@ public class SkillService {
private final SkillRepository skillRepository;

/**
* μŠ€ν‚¬ μ €μž₯ 둜직.
* 기쑴에 μŠ€ν‚¬ 정보가 있으면 μ—…λ°μ΄νŠΈν•˜κ³ , μ—†μœΌλ©΄ μƒˆλ‘œ μ €μž₯.
* μŠ€ν‚¬ μ €μž₯ 둜직
*
* @param info μœ μ € 정보
* @param skillRequest μΉ΄ν…Œκ³ λ¦¬μ™€ 맞좘 문제 개수
*/
public void addSkill(AuthAccountInfo info, SkillRequest skillRequest) {
Skill skill = skillRepository.findByCategoryAndEmail(skillRequest.category(), info.email());
if (skill != null) {
skill.updateSkill(skillRequest.correctCount());
} else {
skill = Skill.builder()
.email(info.email())
.correctCount(skillRequest.correctCount())
.category(skillRequest.category())
.totalCount(5L)
.build();
}
Skill skill = findOrCreateSkill(info, skillRequest);
skillRepository.save(skill);
}

/**
* μœ μ €μ˜ μΉ΄ν…Œκ³ λ¦¬λ³„ μŠ€ν‚¬ 점수λ₯Ό κ³„μ‚°ν•˜κ³  λ°˜ν™˜.
* μœ μ €μ˜ μΉ΄ν…Œκ³ λ¦¬λ³„ μŠ€ν‚¬ 점수λ₯Ό 계산 및 λ°˜ν™˜
*
* @param info μœ μ € 정보
* @return μΉ΄ν…Œκ³ λ¦¬λ³„ μŠ€ν‚¬ 점수 응닡
*/
public SkillTotalResponse getSkillTotalScore(AuthAccountInfo info) {
List<Skill> skills = skillRepository.findByEmail(info.email());
Map<Category, SkillTotalScoreResponse> response = new HashMap<>();

int sum = 0;
Map<Category, SkillTotalScoreResponse> response = initializeCategoryScores();
int totalSum = calculateCategoryScores(info, response);
int totalAvgResponse = calculateAverageScore(response, totalSum);

for (Skill skill : skills) {
Long totalCount = calculateTotalScore(skill);
sum += totalCount;
response.put(skill.getCategory(), createSkillTotalScoreResponse(skill, totalCount));
}

sum /= 3;

SkillTotalResponse totalResponse = SkillTotalResponse.builder()
.totalAvgResponse(sum)
return SkillTotalResponse.builder()
.totalAvgResponse(totalAvgResponse)
.skillTotalScoreResponse(response)
.build();

return totalResponse;
}

/**
* μœ μ €μ˜ 총점에 따라 μƒμœ„ λͺ‡ νΌμ„ΌνŠΈμΈμ§€ 계산.
* μœ μ €μ˜ 총점에 따라 μƒμœ„ λͺ‡ νΌμ„ΌνŠΈμΈμ§€ 계산
*
* @param info μœ μ € 정보
* @return μœ μ €μ˜ μƒμœ„ νΌμ„ΌνŠΈ
*/
public Long getTotalMyScore(AuthAccountInfo info) {
List<Skill> skills = skillRepository.findByEmail(info.email());

Long userTotalScore = skills.stream()
.mapToLong(this::calculateTotalScore)
.sum();
Expand All @@ -89,40 +66,88 @@ public Long getTotalMyScore(AuthAccountInfo info) {
}

/**
* κ°œλ³„ μŠ€ν‚¬μ˜ μ •λ‹΅ λΉ„μœ¨μ„ κ³„μ‚°ν•˜λŠ” λ©”μ„œλ“œ.
*
* @param skill μŠ€ν‚¬ 정보
* @return μ •λ‹΅ λΉ„μœ¨ (총점)
* μŠ€ν‚¬ μ €μž₯ κ΄€λ ¨ λ©”μ„œλ“œ
*/
private Long calculateTotalScore(Skill skill) {
return (skill.getCorrectCount() == 0) ? 0 : (skill.getTotalCount() * 100) / skill.getCorrectCount();
private Skill findOrCreateSkill(AuthAccountInfo info, SkillRequest skillRequest) {
Skill skill = skillRepository.findByCategoryAndEmail(skillRequest.category(), info.email());
if (skill != null) {
skill.updateSkill(skillRequest.correctCount());
return skill;
}
return Skill.builder()
.email(info.email())
.correctCount(skillRequest.correctCount())
.category(skillRequest.category())
.totalCount(5L)
.build();
}

/**
* μŠ€ν‚¬ 응닡을 μƒμ„±ν•˜λŠ” λ©”μ„œλ“œ.
*
* @param skill μŠ€ν‚¬ 정보
* @param totalCount κ³„μ‚°λœ 총점
* @return μΉ΄ν…Œκ³ λ¦¬λ³„ μŠ€ν‚¬ 응닡 DTO
* μΉ΄ν…Œκ³ λ¦¬ μ΄ˆκΈ°ν™” λ©”μ„œλ“œ
*/
private SkillTotalScoreResponse createSkillTotalScoreResponse(Skill skill, Long totalCount) {
return SkillTotalScoreResponse.builder()
.totalScore(totalCount)
.currentCount(skill.getCorrectCount())
.totalCount(skill.getTotalCount())
.build();
private Map<Category, SkillTotalScoreResponse> initializeCategoryScores() {
Map<Category, SkillTotalScoreResponse> categoryScores = new HashMap<>();
for (Category category : Category.values()) {
if (category != Category.ALL) {
categoryScores.put(category, SkillTotalScoreResponse.builder()
.totalScore(0L)
.currentCount(0L)
.totalCount(5L)
.build());
}
}
return categoryScores;
}

/**
* 전체 μœ μ €μ˜ 평균 μŠ€ν‚¬ 점수λ₯Ό κ³„μ‚°ν•˜λŠ” λ©”μ„œλ“œ.
*
* @return 평균 μŠ€ν‚¬ 점수
* μΉ΄ν…Œκ³ λ¦¬λ³„ 점수 계산
*/
private int calculateCategoryScores(AuthAccountInfo info, Map<Category, SkillTotalScoreResponse> response) {
List<Skill> skills = skillRepository.findByEmail(info.email());
int totalSum = 0;

for (Skill skill : skills) {
Long totalScore = calculateTotalScore(skill);
totalSum += totalScore;
response.put(skill.getCategory(), createSkillTotalScoreResponse(skill, totalScore));
}
return totalSum;
}

/**
* 총 평균 점수 계산
*/
private int calculateAverageScore(Map<Category, SkillTotalScoreResponse> response, int totalSum) {
long validCategories = response.keySet().stream().filter(category -> category != Category.ALL).count();
return validCategories == 0 ? 0 : totalSum / (int) validCategories;
}

/**
* κ°œλ³„ μŠ€ν‚¬μ˜ μ •λ‹΅ λΉ„μœ¨ 계산
*/
private Long calculateTotalScore(Skill skill) {
return (skill.getCorrectCount() == 0) ? 0 : (skill.getCorrectCount() * 100) / skill.getTotalCount();
}

/**
* 전체 μœ μ € 평균 점수 계산
*/
private Long calculateAveragePeopleScore() {
List<Skill> allSkills = skillRepository.findAll();
Long totalPeopleScore = allSkills.stream()
.mapToLong(Skill::getCorrectCount)
.sum();
return (allSkills.isEmpty()) ? 0 : totalPeopleScore / allSkills.size();
return allSkills.isEmpty() ? 0 : totalPeopleScore / allSkills.size();
}

/**
* μΉ΄ν…Œκ³ λ¦¬λ³„ μŠ€ν‚¬ 점수 생성
*/
private SkillTotalScoreResponse createSkillTotalScoreResponse(Skill skill, Long totalScore) {
return SkillTotalScoreResponse.builder()
.totalScore(totalScore)
.currentCount(skill.getCorrectCount())
.totalCount(skill.getTotalCount())
.build();
}
}
Loading