Skip to content

Commit

Permalink
refactor: DTO 정적 메서드 추가 및 로직 메서드 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
kckc0608 committed Oct 17, 2024
1 parent 9b03ac7 commit 5d665a0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ public StudyStatisticsResponse getStudyStatistics(Long studyId) {
long studyCompleteStudentCount =
studyHistories.stream().filter(StudyHistory::isComplete).count();

List<StudyWeekStatisticsResponse> studyWeekStatisticsResponses =
calculateStudyWeekStatistics(studyDetails, totalStudentCount);
List<StudyWeekStatisticsResponse> studyWeekStatisticsResponses = studyDetails.stream()
.map((studyDetail -> calculateWeekStatistics(studyDetail, totalStudentCount)))
.toList();

long averageAttendanceRate = calculateAverageWeekAttendanceRate(studyWeekStatisticsResponses);
long averageAssignmentSubmitRate = calculateAverageWeekAssignmentSubmitRate(studyWeekStatisticsResponses);
Expand All @@ -150,31 +151,30 @@ public StudyStatisticsResponse getStudyStatistics(Long studyId) {
studyWeekStatisticsResponses);
}

private List<StudyWeekStatisticsResponse> calculateStudyWeekStatistics(
List<StudyDetail> studyDetails, Long totalStudentCount) {
private StudyWeekStatisticsResponse calculateWeekStatistics(StudyDetail studyDetail, Long totalStudentCount) {
boolean isCanceledWeek = !studyDetail.getCurriculum().isOpen();
boolean isCanceledAssignment = !studyDetail.getAssignment().isOpen() || isCanceledWeek;

return studyDetails.stream()
.map((studyDetail -> {
boolean isCanceledWeek = !studyDetail.getCurriculum().isOpen();
boolean isCanceledAssignment = !studyDetail.getAssignment().isOpen() || isCanceledWeek;

if (totalStudentCount == 0) {
return StudyWeekStatisticsResponse.of(
studyDetail.getWeek(), 0L, 0L, isCanceledAssignment, isCanceledWeek);
}

long attendanceCount = attendanceRepository.countByStudyDetailId(studyDetail.getId());
long assignmentCount = assignmentHistoryRepository.countByStudyDetailIdAndSubmissionStatusEquals(
studyDetail.getId(), SUCCESS);

return StudyWeekStatisticsResponse.of(
studyDetail.getWeek(),
isCanceledWeek ? 0 : Math.round(attendanceCount / (double) totalStudentCount * 100),
isCanceledAssignment ? 0 : Math.round(assignmentCount / (double) totalStudentCount * 100),
isCanceledAssignment,
isCanceledWeek);
}))
.toList();
if (totalStudentCount == 0) {
return StudyWeekStatisticsResponse.emptyOf(studyDetail.getWeek(), isCanceledAssignment, isCanceledWeek);
}

if (isCanceledWeek) {
return StudyWeekStatisticsResponse.canceledWeekFrom(studyDetail.getWeek());
}

long attendanceCount = attendanceRepository.countByStudyDetailId(studyDetail.getId());
long attendanceRate = Math.round(attendanceCount / (double) totalStudentCount * 100);

if (isCanceledAssignment) {
return StudyWeekStatisticsResponse.canceledAssignmentOf(studyDetail.getWeek(), attendanceRate);
}

long assignmentCount =
assignmentHistoryRepository.countByStudyDetailIdAndSubmissionStatusEquals(studyDetail.getId(), SUCCESS);
long assignmentRate = Math.round(assignmentCount / (double) totalStudentCount * 100);

return StudyWeekStatisticsResponse.openedOf(studyDetail.getWeek(), attendanceRate, assignmentRate);
}

private long calculateAverageWeekAttendanceRate(List<StudyWeekStatisticsResponse> studyWeekStatisticsResponses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ public record StudyWeekStatisticsResponse(
@Schema(description = "과제 휴강 여부") boolean isCanceledAssignment,
@Schema(description = "스터디 휴강 여부") boolean isCanceledWeek) {

public static StudyWeekStatisticsResponse of(
Long studyWeek,
Long attendanceRate,
Long assignmentSubmitRate,
boolean isCanceledAssignment,
boolean isCanceledWeek) {
return new StudyWeekStatisticsResponse(
studyWeek, attendanceRate, assignmentSubmitRate, isCanceledAssignment, isCanceledWeek);
public static StudyWeekStatisticsResponse openedOf(Long week, Long attendanceRate, Long assignmentSubmitRate) {
return new StudyWeekStatisticsResponse(week, attendanceRate, assignmentSubmitRate, false, false);
}

public static StudyWeekStatisticsResponse emptyOf(Long week, boolean isCanceledAssignment, boolean isCanceledWeek) {
return new StudyWeekStatisticsResponse(week, 0L, 0L, isCanceledAssignment, isCanceledWeek);
}

public static StudyWeekStatisticsResponse canceledWeekFrom(Long week) {
return StudyWeekStatisticsResponse.emptyOf(week, true, true);
}

public static StudyWeekStatisticsResponse canceledAssignmentOf(Long week, Long attendanceRate) {
return new StudyWeekStatisticsResponse(week, attendanceRate, 0L, true, false);
}
}

0 comments on commit 5d665a0

Please sign in to comment.