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

feat : 정확도, 제출한 사람 수, 전체 멤버 수 GetSolutionResponse에 추가 #197

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
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 @@ -10,25 +10,32 @@
@SuperBuilder
@Getter
public class GetSolutionResponse {
private Long solutionId;
private String problemTitle;
private Integer problemLevel;
private String nickname;
private String profileImage;
private String solvedDateTime;
private String content;
private String result;
private Integer memoryUsage;
private Integer executionTime;
private String language;
private Integer codeLength;
private Long commentCount;
private final Long solutionId;
private final String problemTitle;
private final Integer problemLevel;
private final Integer accuracy;
private final Integer submitMemberCount;
private final Integer totalMemberCount;
private final String nickname;
private final String profileImage;
private final String solvedDateTime;
private final String content;
private final String result;
private final Integer memoryUsage;
private final Integer executionTime;
private final String language;
private final Integer codeLength;
private final Long commentCount;

public static GetSolutionResponse toDTO(Solution solution, Long commentCount) {
public static GetSolutionResponse toDTO(Solution solution, Integer accuracy, Integer submitMemberCount,
Integer totalMemberCount, Long commentCount) {
return GetSolutionResponse.builder()
.solutionId(solution.getId())
.problemTitle(solution.getProblem().getTitle())
.problemLevel(solution.getProblem().getLevel())
.accuracy(accuracy)
.submitMemberCount(submitMemberCount)
.totalMemberCount(totalMemberCount)
.nickname(solution.getUser().getNickname())
.profileImage(solution.getUser().getProfileImage())
.solvedDateTime(DateFormatUtil.formatDateTime(solution.getSolvedDateTime()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
public class GetSolutionWithGroupIdResponse extends GetSolutionResponse {
private final Long groupId;

public static GetSolutionWithGroupIdResponse toDTO(Solution solution, Long commentCount) {
public static GetSolutionWithGroupIdResponse toDTO(Solution solution, Integer accuracy, Integer submitMemberCount,
Integer totalMemberCount, Long commentCount) {
return GetSolutionWithGroupIdResponse.builder()
.solutionId(solution.getId())
.problemTitle(solution.getProblem().getTitle())
.problemLevel(solution.getProblem().getLevel())
.accuracy(accuracy)
.submitMemberCount(submitMemberCount)
.totalMemberCount(totalMemberCount)
.nickname(solution.getUser().getNickname())
.profileImage(solution.getUser().getProfileImage())
.solvedDateTime(DateFormatUtil.formatDateTime(solution.getSolvedDateTime()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ public Page<GetSolutionResponse> getSolutionList(User user, Long problemId, Stri
Page<Solution> solutions = solutionRepository.findAllFilteredSolutions(problem, nickname, language, result,
pageable);

return solutions.map(solution -> {
long commentCount = commentRepository.countCommentsBySolutionId(solution.getId());
return GetSolutionResponse.toDTO(solution, commentCount);
});
return solutions.map(this::getGetSolutionResponse);
}

public GetSolutionResponse getSolution(User user, Long solutionId) {
Expand All @@ -84,8 +81,7 @@ public GetSolutionResponse getSolution(User user, Long solutionId) {
StudyGroup group = solution.getProblem().getStudyGroup();

if (groupMemberRepository.existsByUserAndStudyGroup(user, group)) {
long commentCount = commentRepository.countCommentsBySolutionId(solution.getId());
return GetSolutionResponse.toDTO(solution, commentCount);
return getGetSolutionResponse(solution);
} else {
throw new UserValidationException("해당 풀이를 확인 할 권한이 없습니다.");
}
Expand All @@ -104,10 +100,7 @@ public Page<GetSolutionResponse> getMySolutionsInGroup(User user, Long groupId,
language,
result, pageable);

return solutions.map(solution -> {
long commentCount = commentRepository.countCommentsBySolutionId(solution.getId());
return GetSolutionResponse.toDTO(solution, commentCount);
});
return solutions.map(this::getGetSolutionResponse);
}

public Page<GetSolutionWithGroupIdResponse> getMySolutions(User user, Integer problemNumber, String language,
Expand All @@ -118,11 +111,31 @@ public Page<GetSolutionWithGroupIdResponse> getMySolutions(User user, Integer pr
result, pageable);

return solutions.map(solution -> {
Integer correctCount = getCorrectCount(solution);
Integer submitMemberCount = solutionRepository.countDistinctUsersByProblemId(solution.getProblem().getId());
Integer totalMemberCount = groupMemberRepository.countMembersByStudyGroupId(getGroupId(solution)) + 1;
Integer accuracy = calculateAccuracy(submitMemberCount, correctCount);
long commentCount = commentRepository.countCommentsBySolutionId(solution.getId());
return GetSolutionWithGroupIdResponse.toDTO(solution, commentCount);
return GetSolutionWithGroupIdResponse.toDTO(solution, accuracy, submitMemberCount, totalMemberCount,
commentCount);
});
}

private GetSolutionResponse getGetSolutionResponse(Solution solution) {
Integer correctCount = getCorrectCount(solution);
Integer submitMemberCount = solutionRepository.countDistinctUsersByProblemId(solution.getProblem().getId());
Integer totalMemberCount = groupMemberRepository.countMembersByStudyGroupId(getGroupId(solution)) + 1;
Integer accuracy = calculateAccuracy(submitMemberCount, correctCount);
long commentCount = commentRepository.countCommentsBySolutionId(solution.getId());
return GetSolutionResponse.toDTO(solution, accuracy, submitMemberCount, totalMemberCount, commentCount);
}

private Integer getCorrectCount(Solution solution) {
return solutionRepository.countDistinctUsersWithCorrectSolutionsByProblemId(
solution.getProblem().getId(),
BOJResultConstants.CORRECT);
}

public void createSolution(CreateSolutionRequest request) {

List<Problem> problems = problemRepository.findAllByNumber(request.problemNumber());
Expand Down Expand Up @@ -186,4 +199,18 @@ private void sendNewSolutionNotification(StudyGroup group, GroupMember solver) {
private boolean isCorrect(String result) {
return result.equals(BOJResultConstants.CORRECT) || result.endsWith("점");
}

private Integer calculateAccuracy(Integer submitMemberCount, Integer correctCount) {
if (submitMemberCount == 0)
return 0;

Double tempCorrectCount = correctCount.doubleValue();
Double tempSubmitMemberCount = submitMemberCount.doubleValue();
Double tempAccuracy = ((tempCorrectCount / tempSubmitMemberCount) * 100);
return tempAccuracy.intValue();
}

private static Long getGroupId(Solution solution) {
return solution.getProblem().getStudyGroup().getId();
}
}
Loading